From 7f73a9b7de217fc54cfaa7e10187180e6910c169 Mon Sep 17 00:00:00 2001 From: Ray Miller Date: Wed, 8 Jan 2025 09:35:22 +0000 Subject: [PATCH] Clean up validation of play arguments --- modules/ordo/play.scm | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/modules/ordo/play.scm b/modules/ordo/play.scm index 9bb8639..8e5e606 100644 --- a/modules/ordo/play.scm +++ b/modules/ordo/play.scm @@ -19,12 +19,19 @@ (tasks play-tasks) (handlers play-handlers)) -(define* (play description #:key connection tasks (vars '()) (handlers '())) - (unless connection (error "connection is required")) - (unless (is-a? connection ) (error (format #f "invalid connection: ~a" connection))) - (unless (and tasks (not (null? tasks))) (error "tasks are required")) - (unless (every task? tasks) (error "invalid tasks")) - (unless (every (compose handler? cdr) handlers) (error "invalid handlers")) +(define (validate-connection connection) + (unless (and connection (is-a? connection )) + (error (format #f "invalid connection: ~a" connection)))) + +(define (validate-tasks tasks) + (unless (and tasks (not (null? tasks)) (every task? tasks)) + (error (format #f "invalid tasks: ~a" tasks)))) + +(define (validate-handlers handlers) + (unless (every (lambda (h) (and (pair? h) (handler? (cdr h)))) handlers) + (error (format #f "invalid handlers: ~a" handlers)))) + +(define (validate-triggers tasks handlers) (for-each (lambda (task) (for-each (lambda (trigger) (unless (assoc-ref handlers trigger) @@ -32,7 +39,13 @@ (task-description task) trigger)))) (task-triggers task))) - tasks) + tasks)) + +(define* (play description #:key connection tasks (vars '()) (handlers '())) + (validate-connection connection) + (validate-tasks tasks) + (validate-handlers handlers) + (validate-triggers tasks handlers) (make-play description connection (fold (match-lambda* (((k . v) accum) (alist-cons k v accum))) '() vars) tasks handlers)) (define (run-play play)