summaryrefslogtreecommitdiff
path: root/guix/records.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2013-07-10 16:54:17 +0200
committerLudovic Courtès <ludo@gnu.org>2013-07-10 17:01:08 +0200
commitfdc1bf659d9834fce6c78d31680b580eab3f4235 (patch)
tree647c4dc2bee902cffd64099db95b3fb57fe2986d /guix/records.scm
parentc0edcc3c1926497919e6eefed32dbe5fdc55d045 (diff)
downloadgnu-guix-fdc1bf659d9834fce6c78d31680b580eab3f4235.tar
gnu-guix-fdc1bf659d9834fce6c78d31680b580eab3f4235.tar.gz
records: Add `recutils->alist' for public consumption.
* guix/records.scm (%recutils-field-rx): New variable. (recutils->alist): New procedure, formerly known as `fields->alist'. * guix/scripts/substitute-binary.scm (fields->alist): Use it. * tests/records.scm ("recutils->alist"): New test.
Diffstat (limited to 'guix/records.scm')
-rw-r--r--guix/records.scm25
1 files changed, 24 insertions, 1 deletions
diff --git a/guix/records.scm b/guix/records.scm
index 54e1c17752..64581f1be2 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -21,9 +21,12 @@
#: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 @@ PORT, according to FIELDS. FIELDS must be a list of field name/getter pairs."
(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