diff --git a/guix/records.scm b/guix/records.scm index 54e1c1775283a18810b9a3674722452483684f0c..64581f1be2c107347b309570c46c59e367c380de 100644 --- a/guix/records.scm +++ b/guix/records.scm @@ -21,9 +21,12 @@ (define-module (guix records) #:use-module (srfi srfi-9) #:use-module (srfi srfi-26) #:use-module (ice-9 match) + #:use-module (ice-9 regex) + #:use-module (ice-9 rdelim) #:export (define-record-type* alist->record - object->fields)) + object->fields + recutils->alist)) ;;; Commentary: ;;; @@ -211,4 +214,24 @@ (define (object->fields object fields port) (format port "~a: ~a~%" field (get object)) (loop rest))))) +(define %recutils-field-rx + (make-regexp "^([[:graph:]]+): (.*)$")) + +(define (recutils->alist port) + "Read a recutils-style record from PORT and return it as a list of key/value +pairs. Stop upon an empty line (after consuming it) or EOF." + (let loop ((line (read-line port)) + (result '())) + (cond ((or (eof-object? line) (string-null? line)) + (reverse result)) + ((regexp-exec %recutils-field-rx line) + => + (lambda (match) + (loop (read-line port) + (alist-cons (match:substring match 1) + (match:substring match 2) + result)))) + (else + (error "unmatched line" line))))) + ;;; records.scm ends here diff --git a/guix/scripts/substitute-binary.scm b/guix/scripts/substitute-binary.scm index 24e5d68c4f8f90fb75146c64371fda8c314962e8..fb2eb4dbe8180b88b571922fe9a05ecf0d19ce7a 100755 --- a/guix/scripts/substitute-binary.scm +++ b/guix/scripts/substitute-binary.scm @@ -102,23 +102,8 @@ (define string->uri (define (fields->alist port) "Read recutils-style record from PORT and return them as a list of key/value pairs." - (define field-rx - (make-regexp "^([[:graph:]]+): (.*)$")) - - (let loop ((line (read-line port)) - (result '())) - (cond ((eof-object? line) - (reverse result)) - ((with-mutex %regexp-exec-mutex - (regexp-exec field-rx line)) - => - (lambda (match) - (loop (read-line port) - (alist-cons (match:substring match 1) - (match:substring match 2) - result)))) - (else - (error "unmatched line" line))))) + (with-mutex %regexp-exec-mutex + (recutils->alist port))) (define %fetch-timeout ;; Number of seconds after which networking is considered "slow". diff --git a/tests/records.scm b/tests/records.scm index 9e524b670c34c147bb207711b63dc8acaba2cd0f..470644451c7f0a77472f80189a534948f925118f 100644 --- a/tests/records.scm +++ b/tests/records.scm @@ -131,6 +131,23 @@ (define-record-type* <foo> foo make-foo (parameterize ((mark (cons 'a 'b))) (eq? (foo-baz y) (mark)))))))) +(test-equal "recutils->alist" + '((("Name" . "foo") + ("Version" . "0.1") + ("Synopsis" . "foo bar") + ("Something_else" . "chbouib")) + (("Name" . "bar") + ("Version" . "1.5"))) + (let ((p (open-input-string "Name: foo +Version: 0.1 +Synopsis: foo bar +Something_else: chbouib + +Name: bar +Version: 1.5"))) + (list (recutils->alist p) + (recutils->alist p)))) + (test-end)