liminix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 762ce7b6b8ce13eb418d6922b09f999d8dfd01bc
parent b1c0560f4f8a599bd7a63754d9fa5a10c611dd8c
Author: Daniel Barlow <dan@telent.net>
Date:   Sat, 20 Apr 2024 22:48:00 +0100

cut/paste devout implementation into a real module

Diffstat:
Mpkgs/devout/devout.fnl | 34+++++++++++++++++++++++++++++++++-
Mpkgs/devout/test.fnl | 34+---------------------------------
2 files changed, 34 insertions(+), 34 deletions(-)

diff --git 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 @@ -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)]