records: Factorize error-reporting macro.

* guix/records.scm (record-error): New macro.
  (define-record-type*)[error*]: Remove.
  Use 'record-error' instead.
This commit is contained in:
Ludovic Courtès 2014-07-17 16:42:19 +02:00
parent 23e9a68088
commit b1353e7a6b

View file

@ -1,5 +1,5 @@
;;; GNU Guix --- Functional package management for GNU ;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org> ;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
;;; ;;;
;;; This file is part of GNU Guix. ;;; This file is part of GNU Guix.
;;; ;;;
@ -34,6 +34,14 @@ (define-module (guix records)
;;; ;;;
;;; Code: ;;; Code:
(define-syntax record-error
(syntax-rules ()
"Report a syntactic error in use of CONSTRUCTOR."
((_ constructor form fmt args ...)
(syntax-violation constructor
(format #f fmt args ...)
form))))
(define-syntax define-record-type* (define-syntax define-record-type*
(lambda (s) (lambda (s)
"Define the given record type such that an additional \"syntactic "Define the given record type such that an additional \"syntactic
@ -107,25 +115,21 @@ (define (field-value f)
#`(lambda () #,value) #`(lambda () #,value)
value)))) value))))
(let-syntax ((error* (let ((fields (append fields (map car dflt))))
(syntax-rules () (cond ((lset= eq? fields 'expected)
((_ fmt args (... ...)) #`(let* #,(field-bindings
(syntax-violation 'name #'((field value) (... ...)))
(format #f fmt args (ctor #,@(map field-value 'expected))))
(... ...)) ((pair? (lset-difference eq? fields 'expected))
s))))) (record-error 'name s
(let ((fields (append fields (map car dflt)))) "extraneous field initializers ~a"
(cond ((lset= eq? fields 'expected) (lset-difference eq? fields
#`(let* #,(field-bindings 'expected)))
#'((field value) (... ...))) (else
(ctor #,@(map field-value 'expected)))) (record-error 'name s
((pair? (lset-difference eq? fields 'expected)) "missing field initializers ~a"
(error* "extraneous field initializers ~a" (lset-difference eq? 'expected
(lset-difference eq? fields 'expected))) fields))))))))))))
(else
(error* "missing field initializers ~a"
(lset-difference eq? 'expected
fields)))))))))))))
(define (field-default-value s) (define (field-default-value s)
(syntax-case s (default) (syntax-case s (default)