From 40b881dc4740463c038c329b5a472ae0a98eb0a3 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 30 Nov 2023 07:21:49 -0800 Subject: macrolet of validate --- terrafirma.lisp | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/terrafirma.lisp b/terrafirma.lisp index 16938ee..62d063c 100644 --- a/terrafirma.lisp +++ b/terrafirma.lisp @@ -46,26 +46,25 @@ (defvar *type*) (defvar *instance*) -(defmacro validate (check msg &rest args) - (let ((suberr (gensym))) - `(prog1 t - (let (,suberr) - (assert (handler-case ,check (validation-error (sub) (setf ,suberr sub) nil)) - () 'terrafirma::validation-error - :type terrafirma::*type* - :instance terrafirma::*instance* - :reason (format nil ,msg ,@args) - :suberror ,suberr))))) - - (defmacro defvalidator ((var type &key name) &body body) (assert (and var (symbolp var)) (var) "VAR must be a symbol.") (let ((validator-name (cond ((and name (symbolp name)) name) - ((symbolp type) (intern (format nil "VALIDATE-~a" type))) + ((symbolp type) (intern (format nil "VALIDATED-~a" type))) (t (error "Validator Name: Either TYPE must be a symbol or a NAME must be provided."))))) `(defun ,validator-name (,var) - (let ((terrafirma::*type* ',type) - (terrafirma::*instance* ',var)) - ,@body)))) + (macrolet ((validate (check msg &rest args) + (let ((suberr (gensym))) + `(prog1 t + (let (,suberr) + (assert (handler-case ,check (validation-error (sub) (setf ,suberr sub) nil)) + () 'terrafirma::validation-error + :type terrafirma::*type* + :instance terrafirma::*instance* + :reason (format nil ,msg ,@args) + :suberror ,suberr)))))) + + (let ((terrafirma::*type* ',type) + (terrafirma::*instance* ,var)) + ,@body))))) -- cgit v1.2.3