/
commands.clj
39 lines (34 loc) · 1014 Bytes
/
commands.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(ns jackdaw.test.commands
""
(:require
[jackdaw.test.commands.base :as base]
[jackdaw.test.commands.write :as write]
[jackdaw.test.commands.watch :as watch]))
(def base-commands base/command-map)
(def write-command write/command-map)
(def watch-command watch/command-map)
(def command-map
(merge base-commands
write-command
watch-command))
(defn command-handler
[machine cmd]
(let [[cmd & params] cmd
handler (get command-map cmd)]
(if handler
;; Happy
(let [result (handler machine params)]
(assoc {}
:result result
:cmd cmd
:params params))
;; else Sad
(throw (ex-info (format "Unknown command: %s" cmd)
{:cmd cmd
:error :unknown-command
:params params
:available-commands (keys command-map)})))))
(defn with-handler
[machine handler]
(assoc machine
:command-handler handler))