summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Barksdale <amatus.amongus@gmail.com>2010-10-30 15:03:13 -0700
committerDavid Barksdale <amatus.amongus@gmail.com>2010-10-30 15:03:13 -0700
commit16e5faba51de49ea00f394790c91f16999768ac6 (patch)
tree0c58b03f6b0f649284640593599b96e936d1a5db /src
parent828e4dd606370c58fc5299179b2702664347295e (diff)
Use domonad for our other complex parsers, it makes them easier to read.
Diffstat (limited to 'src')
-rw-r--r--src/org/gnu/clojure/gnunet/crypto.clj28
-rw-r--r--src/org/gnu/clojure/gnunet/message.clj16
2 files changed, 23 insertions, 21 deletions
diff --git a/src/org/gnu/clojure/gnunet/crypto.clj b/src/org/gnu/clojure/gnunet/crypto.clj
index acbdca7..3fab82d 100644
--- a/src/org/gnu/clojure/gnunet/crypto.clj
+++ b/src/org/gnu/clojure/gnunet/crypto.clj
@@ -148,20 +148,20 @@
the contained signed material. The produced parser will fail if the given
parser does not successfully consume the entire signed material."
[signed-material-parser]
- (fn
- [input]
- (when-let [[[size purpose inner-material] residue]
- ((domonad parser-m [size parse-uint32
- :when (<= 8 size)
- purpose parse-uint32
- inner-material (items (- size 8))]
- [size purpose inner-material]) input)]
- (when-let [[parsed inner-residue]
- (signed-material-parser inner-material)]
- (if (empty? inner-residue)
- [{:purpose purpose
- :signed-material (take size input)
- :parsed parsed} residue])))))
+ (domonad parser-m
+ [encoded-size (items 4)
+ :let [size (long (decode-uint encoded-size))]
+ :when (<= 8 size)
+ encoded-purpose (items 4)
+ :let [purpose (long (decode-uint encoded-purpose))]
+ inner-material (items (- size 8))
+ :let [result (signed-material-parser inner-material)]
+ :when result
+ :let [[parsed residue] result]
+ :when (empty? residue)]
+ {:purpose purpose
+ :signed-material (concat encoded-size encoded-purpose inner-material)
+ :parsed parsed}))
(defn generate-rsa-keypair!
"Generate a 2048 bit RSA keypair."
diff --git a/src/org/gnu/clojure/gnunet/message.clj b/src/org/gnu/clojure/gnunet/message.clj
index 771972c..ccf15d7 100644
--- a/src/org/gnu/clojure/gnunet/message.clj
+++ b/src/org/gnu/clojure/gnunet/message.clj
@@ -104,13 +104,15 @@
:bytes message}))
(defn parse-message-types
- "Produces a parser for messages of the given types.
+ "Produces a parser for a message of the given types.
The parser does not fail if the message-type specific parser does not consume
the entire message."
[parser-map]
- (fn [s]
- (when-let [xs (parse-message s)]
- (let [[{message-type :message-type message :bytes} ss] xs]
- (when (contains? parser-map message-type)
- (when-let [xs ((get parser-map message-type) message)]
- [{:message-type message-type :message (first xs)} ss]))))))
+ (domonad parser-m
+ [message parse-message
+ :let [parser (parser-map (:message-type message))]
+ :when parser
+ :let [parsed (first (parser (:bytes message)))]
+ :when parsed]
+ {:message-type (:message-type message)
+ :message parsed}))