devout: recognise attr,attrs when parsing search term string

This commit is contained in:
Daniel Barlow 2024-06-01 23:48:05 +01:00
parent 37d7e20582
commit f091bbd706
2 changed files with 24 additions and 6 deletions

View File

@ -1,4 +1,4 @@
(local { : dirname } (require :anoia))
(local { : dirname : merge } (require :anoia))
(local ll (require :lualinux))
(local {
: AF_LOCAL
@ -101,7 +101,7 @@
(fn database [options]
(let [db {}
subscribers []
{ : sys-path } (or options {:sysfs-path "/sys" })]
{ : sys-path } (or options {:sys-path "/sys" })]
{
:find (fn [_ terms] (find-in-database db terms))
:add (fn [_ event-string]
@ -147,8 +147,16 @@
(values fd (if (> revent 0) revent nil)))))
(fn parse-terms [str]
(collect [n (string.gmatch (str:gsub "\n+$" "") "([^ ]+)")]
(string.match n "(.-)=(.+)")))
(let [keys {}
attr {}
attrs {}]
(each [term (string.gmatch (str:gsub "\n+$" "") "([^ ]+)")]
(let [(k v) (string.match term "(.-)=(.+)")]
(match (string.match k "(.+)%.(.+)")
("attrs" a) (tset attrs a v)
("attr" a) (tset attr a v)
nil (tset keys k v))))
(merge keys {: attr : attrs})))
(fn handle-client [db client]
(match (ll.read client)
@ -208,4 +216,7 @@
(ll.poll pollfds 5000)
(loop:feed (unpack-pollfds pollfds))))))
{ : database : run : event-loop : parse-event }
{ : database : run : event-loop : parse-event
: parse-terms
}

View File

@ -1,4 +1,4 @@
(local { : database : event-loop : parse-event : sysfs } (require :devout))
(local { : database : event-loop : parse-event : parse-terms } (require :devout))
(local { : view } (require :fennel))
(local ll (require :lualinux))
(import-macros { : expect : expect= } :anoia.assert)
@ -246,4 +246,11 @@ MINOR=17")
))
(example
"parse terms from string"
(expect= (parse-terms "foo=bar baz=quuz")
{:foo "bar" :baz "quuz" :attr {} :attrs {}})
(expect= (parse-terms "foo=bar attr.womble=0x1234")
{:foo "bar" :attr {:womble "0x1234"} :attrs {}}))
(if failed (os.exit 1) (print "OK"))