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
use crate::{eyre, Buffer, Parse};

#[derive(Debug, Copy, Clone, PartialEq)]
/// `Tag` is a generic type that implements [`Parse`] to match the given string exactly
///
/// ```
/// use nommy::{Parse, IntoBuf, bytes::Tag};
/// let mut buffer = "foobarbaz".bytes().into_buf();
/// Tag::<b"foobar">::parse(&mut buffer).unwrap();
/// Tag::<b"baz">::parse(&mut buffer).unwrap();
/// ```
pub struct Tag<const TAG: &'static [u8]>;

impl<const TAG: &'static [u8]> Parse<u8> for Tag<TAG> {
    fn parse(input: &mut impl Buffer<u8>) -> eyre::Result<Self> {
        let b: Vec<u8> = input.take(TAG.len()).collect();
        if TAG == b {
            Ok(Self)
        } else {
            Err(eyre::eyre!("failed to parse tag {:?}, found {:?}", TAG, b))
        }
    }

    fn peek(input: &mut impl Buffer<u8>) -> bool {
        TAG.iter().cloned().eq(input.take(TAG.len()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{parse, IntoBuf, Parse};

    #[test]
    fn test_parse_matches() {
        let mut input = "(){}[]<>".bytes().into_buf();
        Tag::<b"(">::parse(&mut input).unwrap();
        Tag::<b")">::parse(&mut input).unwrap();
        Tag::<b"{">::parse(&mut input).unwrap();
        Tag::<b"}">::parse(&mut input).unwrap();
        Tag::<b"[">::parse(&mut input).unwrap();
        Tag::<b"]">::parse(&mut input).unwrap();
        Tag::<b"<">::parse(&mut input).unwrap();
        Tag::<b">">::parse(&mut input).unwrap();
        assert!(input.next().is_none())
    }

    #[test]
    fn test_parse_errors() {
        let res: Result<Tag<b"(">, _> = parse("1".bytes());
        assert_eq!(
            format!("{}", res.unwrap_err()),
            "failed to parse tag [40], found [49]"
        );

        let res: Result<Tag<b")">, _> = parse("1".bytes());
        assert_eq!(
            format!("{}", res.unwrap_err()),
            "failed to parse tag [41], found [49]"
        );
    }
}