From 762ce7b6b8ce13eb418d6922b09f999d8dfd01bc Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sat, 20 Apr 2024 22:48:00 +0100 Subject: [PATCH] cut/paste devout implementation into a real module --- pkgs/devout/devout.fnl | 34 +++++++++++++++++++++++++++++++++- pkgs/devout/test.fnl | 34 +--------------------------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/pkgs/devout/devout.fnl b/pkgs/devout/devout.fnl index d712721..b3fec14 100644 --- a/pkgs/devout/devout.fnl +++ b/pkgs/devout/devout.fnl @@ -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 } diff --git a/pkgs/devout/test.fnl b/pkgs/devout/test.fnl index 245960f..e128fb3 100644 --- a/pkgs/devout/test.fnl +++ b/pkgs/devout/test.fnl @@ -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)]