1 | ///! Utility macros |
2 | |
3 | macro_rules! next { |
4 | ($bytes:ident) => ({ |
5 | match $bytes.next() { |
6 | Some(b) => b, |
7 | None => return Ok(Status::Partial) |
8 | } |
9 | }) |
10 | } |
11 | |
12 | macro_rules! expect { |
13 | ($bytes:ident.next() == $pat:pat => $ret:expr) => { |
14 | expect!(next!($bytes) => $pat |? $ret) |
15 | }; |
16 | ($e:expr => $pat:pat |? $ret:expr) => { |
17 | match $e { |
18 | v@$pat => v, |
19 | _ => return $ret |
20 | } |
21 | }; |
22 | } |
23 | |
24 | macro_rules! complete { |
25 | ($e:expr) => { |
26 | match $e? { |
27 | Status::Complete(v) => v, |
28 | Status::Partial => return Ok(Status::Partial) |
29 | } |
30 | } |
31 | } |
32 | |
33 | macro_rules! byte_map { |
34 | ($($flag:expr,)*) => ([ |
35 | $($flag != 0,)* |
36 | ]) |
37 | } |
38 | |
39 | macro_rules! space { |
40 | ($bytes:ident or $err:expr) => ({ |
41 | expect!($bytes.next() == b' ' => Err($err)); |
42 | $bytes.slice(); |
43 | }) |
44 | } |
45 | |
46 | macro_rules! newline { |
47 | ($bytes:ident) => ({ |
48 | match next!($bytes) { |
49 | b' \r' => { |
50 | expect!($bytes.next() == b' \n' => Err(Error::NewLine)); |
51 | $bytes.slice(); |
52 | }, |
53 | b' \n' => { |
54 | $bytes.slice(); |
55 | }, |
56 | _ => return Err(Error::NewLine) |
57 | } |
58 | }) |
59 | } |
60 | |