]>
Commit | Line | Data |
---|---|---|
1 | -module(proto). | |
2 | ||
3 | -export([parse/2]). | |
4 | ||
5 | parse(empty, "+OK") -> | |
6 | ok; | |
7 | parse(empty, "+PONG") -> | |
8 | pong; | |
9 | parse(empty, ":0") -> | |
10 | false; | |
11 | parse(empty, ":1") -> | |
12 | true; | |
13 | parse(empty, "-" ++ Message) -> | |
14 | {error, Message}; | |
15 | parse(empty, "$-1") -> | |
16 | {read, nil}; | |
17 | parse(empty, "*-1") -> | |
18 | {hold, nil}; | |
19 | parse(empty, "$" ++ BulkSize) -> | |
20 | {read, list_to_integer(BulkSize)}; | |
21 | parse(read, "$" ++ BulkSize) -> | |
22 | {read, list_to_integer(BulkSize)}; | |
23 | parse(empty, "*" ++ MultiBulkSize) -> | |
24 | {hold, list_to_integer(MultiBulkSize)}; | |
25 | parse(empty, Message) -> | |
26 | convert(Message). | |
27 | ||
28 | convert(":" ++ Message) -> | |
29 | list_to_integer(Message); | |
30 | % in case the message is not OK or PONG it's a | |
31 | % real value that we don't know how to convert | |
32 | % to an atom, so just pass it as is and remove | |
33 | % the + | |
34 | convert("+" ++ Message) -> | |
35 | Message; | |
36 | convert(Message) -> | |
37 | Message. | |
38 |