|
1 |
| -use bstr::ByteSlice; |
2 |
| -use btoi::btoi; |
3 |
| -use nom::{ |
4 |
| - branch::alt, |
5 |
| - bytes::complete::{tag, take, take_until, take_while_m_n}, |
6 |
| - character::is_digit, |
7 |
| - error::{context, ContextError, ParseError}, |
8 |
| - sequence::{terminated, tuple}, |
9 |
| - IResult, |
10 |
| -}; |
11 |
| - |
12 |
| -use crate::{Sign, SignatureRef, Time}; |
13 |
| - |
14 |
| -const SPACE: &[u8] = b" "; |
15 |
| - |
16 |
| -/// Parse a signature from the bytes input `i` using `nom`. |
17 |
| -pub fn decode<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>( |
18 |
| - i: &'a [u8], |
19 |
| -) -> IResult<&'a [u8], SignatureRef<'a>, E> { |
20 |
| - let (i, (name, email, time, tzsign, hours, minutes)) = context( |
21 |
| - "<name> <<email>> <timestamp> <+|-><HHMM>", |
22 |
| - tuple(( |
23 |
| - context("<name>", terminated(take_until(&b" <"[..]), take(2usize))), |
24 |
| - context("<email>", terminated(take_until(&b"> "[..]), take(2usize))), |
25 |
| - context("<timestamp>", |i| { |
26 |
| - terminated(take_until(SPACE), take(1usize))(i).and_then(|(i, v)| { |
27 |
| - btoi::<u32>(v) |
28 |
| - .map(|v| (i, v)) |
29 |
| - .map_err(|_| nom::Err::Error(E::from_error_kind(i, nom::error::ErrorKind::MapRes))) |
30 |
| - }) |
31 |
| - }), |
32 |
| - context("+|-", alt((tag(b"-"), tag(b"+")))), |
33 |
| - context("HH", |i| { |
34 |
| - take_while_m_n(2usize, 2, is_digit)(i).and_then(|(i, v)| { |
35 |
| - btoi::<i32>(v) |
36 |
| - .map(|v| (i, v)) |
37 |
| - .map_err(|_| nom::Err::Error(E::from_error_kind(i, nom::error::ErrorKind::MapRes))) |
38 |
| - }) |
39 |
| - }), |
40 |
| - context("MM", |i| { |
41 |
| - take_while_m_n(2usize, 2, is_digit)(i).and_then(|(i, v)| { |
42 |
| - btoi::<i32>(v) |
43 |
| - .map(|v| (i, v)) |
44 |
| - .map_err(|_| nom::Err::Error(E::from_error_kind(i, nom::error::ErrorKind::MapRes))) |
45 |
| - }) |
46 |
| - }), |
47 |
| - )), |
48 |
| - )(i)?; |
49 |
| - |
50 |
| - debug_assert!(tzsign[0] == b'-' || tzsign[0] == b'+', "parser assure it's +|- only"); |
51 |
| - let sign = if tzsign[0] == b'-' { Sign::Minus } else { Sign::Plus }; // |
52 |
| - let offset = (hours * 3600 + minutes * 60) * if sign == Sign::Minus { -1 } else { 1 }; |
53 |
| - |
54 |
| - Ok(( |
55 |
| - i, |
56 |
| - SignatureRef { |
57 |
| - name: name.as_bstr(), |
58 |
| - email: email.as_bstr(), |
59 |
| - time: Time { |
60 |
| - seconds_since_unix_epoch: time, |
61 |
| - offset_in_seconds: offset, |
62 |
| - sign, |
| 1 | +pub(crate) mod function { |
| 2 | + use bstr::ByteSlice; |
| 3 | + use btoi::btoi; |
| 4 | + use nom::{ |
| 5 | + branch::alt, |
| 6 | + bytes::complete::{tag, take, take_until, take_while_m_n}, |
| 7 | + character::is_digit, |
| 8 | + error::{context, ContextError, ParseError}, |
| 9 | + sequence::{terminated, tuple}, |
| 10 | + IResult, |
| 11 | + }; |
| 12 | + |
| 13 | + use crate::{IdentityRef, Sign, SignatureRef, Time}; |
| 14 | + |
| 15 | + const SPACE: &[u8] = b" "; |
| 16 | + |
| 17 | + /// Parse a signature from the bytes input `i` using `nom`. |
| 18 | + pub fn decode<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>( |
| 19 | + i: &'a [u8], |
| 20 | + ) -> IResult<&'a [u8], SignatureRef<'a>, E> { |
| 21 | + let (i, (identity, _, time, tzsign, hours, minutes)) = context( |
| 22 | + "<name> <<email>> <timestamp> <+|-><HHMM>", |
| 23 | + tuple(( |
| 24 | + identity, |
| 25 | + tag(b" "), |
| 26 | + context("<timestamp>", |i| { |
| 27 | + terminated(take_until(SPACE), take(1usize))(i).and_then(|(i, v)| { |
| 28 | + btoi::<u32>(v) |
| 29 | + .map(|v| (i, v)) |
| 30 | + .map_err(|_| nom::Err::Error(E::from_error_kind(i, nom::error::ErrorKind::MapRes))) |
| 31 | + }) |
| 32 | + }), |
| 33 | + context("+|-", alt((tag(b"-"), tag(b"+")))), |
| 34 | + context("HH", |i| { |
| 35 | + take_while_m_n(2usize, 2, is_digit)(i).and_then(|(i, v)| { |
| 36 | + btoi::<i32>(v) |
| 37 | + .map(|v| (i, v)) |
| 38 | + .map_err(|_| nom::Err::Error(E::from_error_kind(i, nom::error::ErrorKind::MapRes))) |
| 39 | + }) |
| 40 | + }), |
| 41 | + context("MM", |i| { |
| 42 | + take_while_m_n(2usize, 2, is_digit)(i).and_then(|(i, v)| { |
| 43 | + btoi::<i32>(v) |
| 44 | + .map(|v| (i, v)) |
| 45 | + .map_err(|_| nom::Err::Error(E::from_error_kind(i, nom::error::ErrorKind::MapRes))) |
| 46 | + }) |
| 47 | + }), |
| 48 | + )), |
| 49 | + )(i)?; |
| 50 | + |
| 51 | + debug_assert!(tzsign[0] == b'-' || tzsign[0] == b'+', "parser assure it's +|- only"); |
| 52 | + let sign = if tzsign[0] == b'-' { Sign::Minus } else { Sign::Plus }; // |
| 53 | + let offset = (hours * 3600 + minutes * 60) * if sign == Sign::Minus { -1 } else { 1 }; |
| 54 | + |
| 55 | + Ok(( |
| 56 | + i, |
| 57 | + SignatureRef { |
| 58 | + name: identity.name, |
| 59 | + email: identity.email, |
| 60 | + time: Time { |
| 61 | + seconds_since_unix_epoch: time, |
| 62 | + offset_in_seconds: offset, |
| 63 | + sign, |
| 64 | + }, |
| 65 | + }, |
| 66 | + )) |
| 67 | + } |
| 68 | + |
| 69 | + /// Parse an identity from the bytes input `i` (like `name <email>`) using `nom`. |
| 70 | + pub fn identity<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>( |
| 71 | + i: &'a [u8], |
| 72 | + ) -> IResult<&'a [u8], IdentityRef<'a>, E> { |
| 73 | + let (i, (name, email)) = context( |
| 74 | + "<name> <<email>>", |
| 75 | + tuple(( |
| 76 | + context("<name>", terminated(take_until(&b" <"[..]), take(2usize))), |
| 77 | + context("<email>", terminated(take_until(&b">"[..]), take(1usize))), |
| 78 | + )), |
| 79 | + )(i)?; |
| 80 | + |
| 81 | + Ok(( |
| 82 | + i, |
| 83 | + IdentityRef { |
| 84 | + name: name.as_bstr(), |
| 85 | + email: email.as_bstr(), |
63 | 86 | },
|
64 |
| - }, |
65 |
| - )) |
| 87 | + )) |
| 88 | + } |
66 | 89 | }
|
| 90 | +pub use function::identity; |
67 | 91 |
|
68 | 92 | #[cfg(test)]
|
69 | 93 | mod tests {
|
@@ -141,7 +165,7 @@ mod tests {
|
141 | 165 | .map_err(to_bstr_err)
|
142 | 166 | .expect_err("parse fails as > is missing")
|
143 | 167 | .to_string(),
|
144 |
| - "Parse error:\nTakeUntil at: 12345 -1215\nin section '<email>', at: 12345 -1215\nin section '<name> <<email>> <timestamp> <+|-><HHMM>', at: hello < 12345 -1215\n" |
| 168 | + "Parse error:\nTakeUntil at: 12345 -1215\nin section '<email>', at: 12345 -1215\nin section '<name> <<email>>', at: hello < 12345 -1215\nin section '<name> <<email>> <timestamp> <+|-><HHMM>', at: hello < 12345 -1215\n" |
145 | 169 | );
|
146 | 170 | }
|
147 | 171 |
|
|
0 commit comments