1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
pub(crate) mod function {
    use crate::{IdentityRef, SignatureRef};
    use bstr::ByteSlice;
    use gix_date::{time::Sign, OffsetInSeconds, SecondsSinceUnixEpoch, Time};
    use gix_utils::btoi::to_signed;
    use winnow::error::{ErrMode, ErrorKind};
    use winnow::stream::Stream;
    use winnow::{
        combinator::{alt, separated_pair, terminated},
        error::{AddContext, ParserError, StrContext},
        prelude::*,
        stream::AsChar,
        token::{take, take_until, take_while},
    };

    const SPACE: &[u8] = b" ";

    /// Parse a signature from the bytes input `i` using `nom`.
    pub fn decode<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
        i: &mut &'a [u8],
    ) -> PResult<SignatureRef<'a>, E> {
        separated_pair(
            identity,
            b" ",
            (
                terminated(take_until(0.., SPACE), take(1usize))
                    .verify_map(|v| to_signed::<SecondsSinceUnixEpoch>(v).ok())
                    .context(StrContext::Expected("<timestamp>".into())),
                alt((
                    take_while(1.., b'-').map(|_| Sign::Minus),
                    take_while(1.., b'+').map(|_| Sign::Plus),
                ))
                .context(StrContext::Expected("+|-".into())),
                take_while(2, AsChar::is_dec_digit)
                    .verify_map(|v| to_signed::<OffsetInSeconds>(v).ok())
                    .context(StrContext::Expected("HH".into())),
                take_while(1..=2, AsChar::is_dec_digit)
                    .verify_map(|v| to_signed::<OffsetInSeconds>(v).ok())
                    .context(StrContext::Expected("MM".into())),
                take_while(0.., AsChar::is_dec_digit).map(|v: &[u8]| v),
            )
                .map(|(time, sign, hours, minutes, trailing_digits)| {
                    let offset = if trailing_digits.is_empty() {
                        (hours * 3600 + minutes * 60) * if sign == Sign::Minus { -1 } else { 1 }
                    } else {
                        0
                    };
                    Time {
                        seconds: time,
                        offset,
                        sign,
                    }
                }),
        )
        .context(StrContext::Expected("<name> <<email>> <timestamp> <+|-><HHMM>".into()))
        .map(|(identity, time)| SignatureRef {
            name: identity.name,
            email: identity.email,
            time,
        })
        .parse_next(i)
    }

    /// Parse an identity from the bytes input `i` (like `name <email>`) using `nom`.
    pub fn identity<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
        i: &mut &'a [u8],
    ) -> PResult<IdentityRef<'a>, E> {
        let start = i.checkpoint();
        let eol_idx = i.find_byte(b'\n').unwrap_or(i.len());
        let right_delim_idx =
            i[..eol_idx]
                .rfind_byte(b'>')
                .ok_or(ErrMode::Cut(E::from_error_kind(i, ErrorKind::Eof).add_context(
                    i,
                    &start,
                    StrContext::Label("Closing '>' not found"),
                )))?;
        let i_name_and_email = &i[..right_delim_idx];
        let skip_from_right = i_name_and_email
            .iter()
            .rev()
            .take_while(|b| b.is_ascii_whitespace() || **b == b'>')
            .count();
        let left_delim_idx =
            i_name_and_email
                .find_byte(b'<')
                .ok_or(ErrMode::Cut(E::from_error_kind(i, ErrorKind::Eof).add_context(
                    &i_name_and_email,
                    &start,
                    StrContext::Label("Opening '<' not found"),
                )))?;
        let skip_from_left = i[left_delim_idx..]
            .iter()
            .take_while(|b| b.is_ascii_whitespace() || **b == b'<')
            .count();
        let mut name = i[..left_delim_idx].as_bstr();
        name = name.strip_suffix(b" ").unwrap_or(name).as_bstr();

        let email = i
            .get(left_delim_idx + skip_from_left..right_delim_idx - skip_from_right)
            .ok_or(ErrMode::Cut(E::from_error_kind(i, ErrorKind::Eof).add_context(
                &i_name_and_email,
                &start,
                StrContext::Label("Skipped parts run into each other"),
            )))?
            .as_bstr();
        *i = i.get(right_delim_idx + 1..).unwrap_or(&[]);
        Ok(IdentityRef { name, email })
    }
}
pub use function::identity;

#[cfg(test)]
mod tests {
    mod parse_signature {
        use bstr::ByteSlice;
        use gix_date::{time::Sign, OffsetInSeconds, SecondsSinceUnixEpoch};
        use gix_testtools::to_bstr_err;
        use winnow::prelude::*;

        use crate::{signature, SignatureRef, Time};

        fn decode<'i>(
            i: &mut &'i [u8],
        ) -> PResult<SignatureRef<'i>, winnow::error::TreeError<&'i [u8], winnow::error::StrContext>> {
            signature::decode.parse_next(i)
        }

        fn signature(
            name: &'static str,
            email: &'static str,
            seconds: SecondsSinceUnixEpoch,
            sign: Sign,
            offset: OffsetInSeconds,
        ) -> SignatureRef<'static> {
            SignatureRef {
                name: name.as_bytes().as_bstr(),
                email: email.as_bytes().as_bstr(),
                time: Time { seconds, offset, sign },
            }
        }

        #[test]
        fn tz_minus() {
            assert_eq!(
                decode
                    .parse_peek(b"Sebastian Thiel <byronimo@gmail.com> 1528473343 -0230")
                    .expect("parse to work")
                    .1,
                signature("Sebastian Thiel", "byronimo@gmail.com", 1528473343, Sign::Minus, -9000)
            );
        }

        #[test]
        fn tz_plus() {
            assert_eq!(
                decode
                    .parse_peek(b"Sebastian Thiel <byronimo@gmail.com> 1528473343 +0230")
                    .expect("parse to work")
                    .1,
                signature("Sebastian Thiel", "byronimo@gmail.com", 1528473343, Sign::Plus, 9000)
            );
        }

        #[test]
        fn negative_offset_0000() {
            assert_eq!(
                decode
                    .parse_peek(b"Sebastian Thiel <byronimo@gmail.com> 1528473343 -0000")
                    .expect("parse to work")
                    .1,
                signature("Sebastian Thiel", "byronimo@gmail.com", 1528473343, Sign::Minus, 0)
            );
        }

        #[test]
        fn negative_offset_double_dash() {
            assert_eq!(
                decode
                    .parse_peek(b"name <name@example.com> 1288373970 --700")
                    .expect("parse to work")
                    .1,
                signature("name", "name@example.com", 1288373970, Sign::Minus, -252000)
            );
        }

        #[test]
        fn empty_name_and_email() {
            assert_eq!(
                decode.parse_peek(b" <> 12345 -1215").expect("parse to work").1,
                signature("", "", 12345, Sign::Minus, -44100)
            );
        }

        #[test]
        fn invalid_signature() {
            assert_eq!(
                        decode.parse_peek(b"hello < 12345 -1215")
                            .map_err(to_bstr_err)
                            .expect_err("parse fails as > is missing")
                            .to_string(),
                        "in end of file at 'hello < 12345 -1215'\n  0: invalid Closing '>' not found at 'hello < 12345 -1215'\n  1: expected `<name> <<email>> <timestamp> <+|-><HHMM>` at 'hello < 12345 -1215'\n"
                    );
        }

        #[test]
        fn invalid_time() {
            assert_eq!(
                        decode.parse_peek(b"hello <> abc -1215")
                            .map_err(to_bstr_err)
                            .expect_err("parse fails as > is missing")
                            .to_string(),
                        "in predicate verification at 'abc -1215'\n  0: expected `<timestamp>` at 'abc -1215'\n  1: expected `<name> <<email>> <timestamp> <+|-><HHMM>` at 'hello <> abc -1215'\n"
                    );
        }
    }
}