]> gitweb.factorcode.org Git - factor.git/blob - extra/redis/response-parser/response-parser.factor
change ERROR: words from throw-foo back to foo.
[factor.git] / extra / redis / response-parser / response-parser.factor
1 ! Copyright (C) 2009 Bruno Deferrari
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators io kernel math math.parser sequences ;
4 IN: redis.response-parser
5
6 DEFER: read-response
7
8 TUPLE: redis-response message ;
9 ERROR: redis-error message ;
10
11 : <redis-response> ( message -- redis-response )
12     redis-response boa ;
13
14 <PRIVATE
15
16 : read-bulk ( n -- bytes )
17     dup 0 < [ drop f ] [ read 2 read drop ] if ;
18 : read-multi-bulk ( n -- seq/f )
19     dup 0 <
20     [ drop f ]
21     [ [ read-response ] replicate ] if ;
22
23 : handle-response ( string -- string )
24     <redis-response> ;
25
26 : handle-error ( string -- * )
27     redis-error ;
28
29 PRIVATE>
30
31 : read-response ( -- response )
32     readln unclip {
33         { CHAR: : [ string>number ] }
34         { CHAR: + [ handle-response ] }
35         { CHAR: $ [ string>number read-bulk ] }
36         { CHAR: * [ string>number read-multi-bulk ] }
37         { CHAR: - [ handle-error ] }
38     } case ;
39
40 : check-response ( -- )
41     read-response drop ;