From a783b56772fe44111395e3045cfebbf986259495 Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Mon, 2 Sep 2024 09:31:23 +0300 Subject: New function: Add validate-custom-values. * Function to be used with a variable watcher to validate custom gnosis values. --- gnosis.el | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'gnosis.el') diff --git a/gnosis.el b/gnosis.el index 2e8af51..61ab5db 100644 --- a/gnosis.el +++ b/gnosis.el @@ -2017,6 +2017,35 @@ SUSPEND: Suspend note, 0 for unsuspend, 1 for suspend" (gnosis-update 'notes `(= ,field ',value) `(= id ,id))) (t (gnosis-update 'notes `(= ,field ,value) `(= id ,id)))))) +(defun gnosis-validate-custom-values (new-value) + "Validate the structure and values of NEW-VALUE for gnosis-custom-values." + (unless (listp new-value) + (error "GNOSIS-CUSTOM-VALUES should be a list of entries")) + (dolist (entry new-value) + (unless (and (listp entry) (= (length entry) 3) + (memq (nth 0 entry) '(:deck :tag)) + (stringp (nth 1 entry)) + (listp (nth 2 entry))) ; Ensure the third element is a plist + (error "Each entry should a :deck or :tag keyword, a string, and a plist of custom values")) + (let ((nested-plist (nth 2 entry)) + (proto (plist-get (nth 2 entry) :proto)) + (anagnosis (plist-get (nth 2 entry) :anagnosis)) + (epignosis (plist-get (nth 2 entry) :epignosis)) + (agnoia (plist-get (nth 2 entry) :agnoia)) + (amnesia (plist-get (nth 2 entry) :amnesia)) + (lethe (plist-get (nth 2 entry) :lethe))) + (unless (listp proto) + (error "Proto must be a list of interval integer values")) + (unless (or (null anagnosis) (integerp anagnosis)) + (error "Anagnosis should be an integer")) + (unless (or (null epignosis) (numberp epignosis)) + (error "Epignosis should be a number")) + (unless (or (null agnoia) (numberp agnoia)) + (error "Agnoia should be a number")) + (unless (or (null amnesia) (and (numberp amnesia) (<= amnesia 1) (>= amnesia 0))) + (error "Amnesia should be a number between 0 and 1")) + (unless (or (null lethe) (and (integerp lethe) (> lethe 0))) + (error "Lethe should be an integer greater than 0"))))) (defun gnosis-get-custom-values--validate (plist valid-keywords) "Verify that PLIST consists of VALID-KEYWORDS." (let ((keys (let (ks) -- cgit v1.2.3