]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/redis/response-parser/response-parser.factor
Update redis vocabulary to use the modern protocol
[factor.git] / extra / redis / response-parser / response-parser.factor
index 3d92d553b033c683a0b4d0a41e182ed5db7bb4f9..5b4f0b328324b0d26f9e66459611c7dc538439f1 100644 (file)
@@ -3,17 +3,28 @@
 USING: combinators io kernel math math.parser sequences ;
 IN: redis.response-parser
 
+DEFER: read-response
+
+TUPLE: redis-response message ;
+ERROR: redis-error message ;
+
+: <redis-response> ( message -- redis-response )
+    redis-response boa ;
+
 <PRIVATE
 
-: read-bulk ( n -- bytes ) dup 0 < [ drop f ] [ read 2 read drop ] if ;
-: (read-multi-bulk) ( -- bytes ) readln rest string>number read-bulk ;
+: read-bulk ( n -- bytes )
+    dup 0 < [ drop f ] [ read 2 read drop ] if ;
 : read-multi-bulk ( n -- seq/f )
-    dup 0 < [ drop f ] [
-        iota [ drop (read-multi-bulk) ] map
-    ] if ;
+    dup 0 <
+    [ drop f ]
+    [ [ read-response ] replicate ] if ;
 
-: handle-response ( string -- string ) ; ! TODO
-: handle-error ( string -- string ) ; ! TODO
+: handle-response ( string -- string )
+    <redis-response> ;
+
+: handle-error ( string -- * )
+    redis-error ;
 
 PRIVATE>
 
@@ -25,3 +36,6 @@ PRIVATE>
         { CHAR: * [ string>number read-multi-bulk ] }
         { CHAR: - [ handle-error ] }
     } case ;
+
+: check-response ( -- )
+    read-response drop ;