Clean up validation of play arguments

This commit is contained in:
Ray Miller 2025-01-08 09:35:22 +00:00
parent 5360e73d60
commit 7f73a9b7de
Signed by: ray
GPG key ID: 043F786C4CD681B8

View file

@ -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 <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 <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)