cut/paste devout implementation into a real module

main
Daniel Barlow 2024-04-20 22:48:00 +01:00
parent b1c0560f4f
commit 762ce7b6b8
2 changed files with 34 additions and 34 deletions

View File

@ -1,2 +1,34 @@
(fn parse-uevent [s]
(let [at (string.find s "@" 1 true)
(nl nxt) (string.find s "\0" 1 true)]
(doto
(collect [k v (string.gmatch
(string.sub s (+ 1 nxt))
"(%g-)=(%g+)")]
(k:lower) v)
(tset :path (string.sub s (+ at 1) (- nl 1))))))
{ }
(fn event-matches? [e terms]
(accumulate [match? true
name value (pairs terms)]
(and match? (= value (. e name)))))
(fn find-in-database [db terms]
(accumulate [found []
_ e (pairs db)]
(if (event-matches? e terms)
(doto found (table.insert e))
found)))
(fn database []
(let [db {}]
{
:find (fn [_ terms] (find-in-database db terms))
:add (fn [_ event-string]
(let [e (parse-uevent event-string)]
(tset db e.path e)))
:at-path (fn [_ path] (. db path))
}))
{ : database }

View File

@ -1,38 +1,7 @@
(local { : database } (require :devout))
(local { : view } (require :fennel))
(import-macros { : expect= } :anoia.assert)
(fn parse-uevent [s]
(let [at (string.find s "@" 1 true)
(nl nxt) (string.find s "\0" 1 true)]
(doto
(collect [k v (string.gmatch
(string.sub s (+ 1 nxt))
"(%g-)=(%g+)")]
(k:lower) v)
(tset :path (string.sub s (+ at 1) (- nl 1))))))
(fn event-matches? [e terms]
(accumulate [match? true
name value (pairs terms)]
(and match? (= value (. e name)))))
(fn find-in-database [db terms]
(accumulate [found []
_ e (pairs db)]
(if (event-matches? e terms)
(doto found (table.insert e))
found)))
(fn database []
(let [db {}]
{
:find (fn [_ terms] (find-in-database db terms))
:add (fn [_ event-string]
(let [e (parse-uevent event-string)]
(tset db e.path e)))
:at-path (fn [_ path] (. db path))
}))
(var failed false)
(fn fail [d msg] (set failed true) (print :FAIL d (.. "\n" msg)))
@ -81,7 +50,6 @@ SEQNUM=1527")
(expect= m.devname "sda")
(expect= m.major "8"))))
(example
"when I search on multiple terms it uses all of them"
(let [db (database)]