Subject: [PATCH v2 09/12] import: cran: Refactor format-inputs and use maybe-list-field.

From: Herman Rimm <[email-masked]>
Date: Wed, 4 Sep 2024 22:33:03 +0200

* guix/import/cran.scm (format-inputs): Split and move procedure ...
(maybe-inputs): ... to here, and use maybe-list-field procedure.

Change-Id: I5148afcebdac1f7fa0f8cce9e82e8cebb56c36c8
---
 guix/import/cran.scm | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/guix/import/cran.scm b/guix/import/cran.scm
index 6ae00cae96..bdac059053 100644
--- a/guix/import/cran.scm
+++ b/guix/import/cran.scm
@@ -6,6 +6,7 @@
 ;;; Copyright © 2021 Sarah Morgensen <[email-masked]>
 ;;; Copyright © 2021 Simon Tournier <[email-masked]>
 ;;; Copyright © 2022 Hartmut Goebel <[email-masked]>
+;;; Copyright © 2024 Herman Rimm <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -85,21 +86,6 @@ (define-module (guix import cran)
 (define %input-style
   (make-parameter 'variable)) ; or 'specification
 
-(define (format-inputs inputs)
-  "Generate a sorted list of package inputs from a list of upstream inputs."
-  (map (lambda (input)
-         (case (%input-style)
-           ((specification)
-            `(specification->package ,(upstream-input-downstream-name input)))
-           (else
-            ((compose string->symbol
-                       upstream-input-downstream-name)
-             input))))
-       (sort inputs
-             (lambda (a b)
-               (string-ci<? (upstream-input-name a)
-                            (upstream-input-name b))))))
-
 (define (string->licenses license-string license-prefix)
   (let ((licenses
          (map string-trim-both
@@ -188,11 +174,21 @@ (define (description->alist description)
 (define* (maybe-inputs package-inputs #:optional (input-type 'inputs))
   "Given a list of PACKAGE-INPUTS, tries to generate the TYPE field of a
 package definition."
-  (match package-inputs
-    (()
-     '())
-    ((package-inputs ...)
-     `((,input-type (list ,@(format-inputs package-inputs)))))))
+  (define (format-input input)
+    (case (%input-style)
+      ((specification)
+       `(specification->package ,(upstream-input-downstream-name input)))
+      (else
+       ((compose string->symbol
+                  upstream-input-downstream-name)
+        input))))
+
+  (define (upstream-input-name<? i1 i2)
+    (string-ci<? (upstream-input-name i1)
+                 (upstream-input-name i2)))
+
+  (maybe-list-field input-type
+    (map format-input (sort package-inputs upstream-input-name<?))))
 
 (define %cran-url "https://cloud.r-project.org/web/packages/")
 (define %cran-canonical-url "https://cran.r-project.org/package=")
-- 
2.45.2