]> git.saurik.com Git - redis.git/blame - client-libraries/erlang/src/proto.erl
first commit
[redis.git] / client-libraries / erlang / src / proto.erl
CommitLineData
ed9b544e 1-module(proto).
2
3-export([parse/2, parse_int/2, parse_types/2,
4 parse_string/2, stateful_parser/2,
5 single_stateful_parser/2, parse_special/2]).
6
7
8parse(empty, "+OK") ->
9 ok;
10parse(empty, "+PONG") ->
11 pong;
12parse(empty, "0") ->
13 false;
14parse(empty, "1") ->
15 true;
16parse(empty, "-1") ->
17 {error, no_such_key};
18parse(empty, "-2") ->
19 {error, wrong_type};
20parse(empty, "-3") ->
21 {error, same_db};
22parse(empty, "-4") ->
23 {error, argument_out_of_range};
24parse(empty, "-" ++ Message) ->
25 {error, Message}.
26
27parse_special(empty, "-1") ->
28 parse(empty, "-1");
29parse_special(empty, "-2") ->
30 parse(empty, "-2");
31parse_special(empty, N) ->
32 list_to_integer(N).
33
34parse_int(empty, "-ERR " ++ Message) ->
35 {error, Message};
36parse_int(empty, Value) ->
37 list_to_integer(Value).
38
39parse_string(empty, Message) ->
40 Message.
41
42parse_types(empty, "none") -> none;
43parse_types(empty, "string") -> string;
44parse_types(empty, "list") -> list;
45parse_types(empty, "set") -> set.
46
47
48% I'm used when redis returns multiple results
49stateful_parser(empty, "nil") ->
50 nil;
51stateful_parser(error, "-ERR " ++ Error) ->
52 {error, Error};
53stateful_parser(empty, "-" ++ _ErrorLength) ->
54 error;
55stateful_parser(empty, NumberOfElements) ->
56 {hold, list_to_integer(NumberOfElements)};
57stateful_parser(read, ElementSize) ->
58 {read, list_to_integer(ElementSize)}.
59
60% I'm used when redis returns just one result
61single_stateful_parser(empty, "nil") ->
62 nil;
63single_stateful_parser(error, "-ERR " ++ Error) ->
64 {error, Error};
65single_stateful_parser(empty, "-" ++ _ErrorLength) ->
66 error;
67single_stateful_parser(empty, ElementSize) ->
68 {read, list_to_integer(ElementSize)}.