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

#[derive(Debug, Copy, Clone, PartialEq)]
/// `OneOf` is a generic type that implements [`Parse`] to match one character within the given string
///
/// ```
/// use nommy::{Parse, IntoBuf, text::OneOf};
/// let mut buffer = "-".chars().into_buf();
/// let c: char = OneOf::<"-_">::parse(&mut buffer).unwrap().into();
/// assert_eq!(c, '-');
/// ```
pub struct OneOf<const CHARS: &'static str>(char);

impl<const CHARS: &'static str> From<OneOf<CHARS>> for char {
    fn from(v: OneOf<CHARS>) -> Self {
        v.0
    }
}

impl<const CHARS: &'static str> Parse<char> for OneOf<CHARS> {
    fn parse(input: &mut impl Buffer<char>) -> eyre::Result<Self> {
        match input.next() {
            Some(c) => {
                if CHARS.contains(c) {
                    Ok(Self(c))
                } else {
                    Err(eyre::eyre!(
                        "error parsing one of {:?}, found {:?}",
                        CHARS,
                        c
                    ))
                }
            }
            None => Err(eyre::eyre!("error parsing one of {:?}, reached EOF", CHARS)),
        }
    }

    fn peek(input: &mut impl Buffer<char>) -> bool {
        match input.next() {
            Some(c) => CHARS.contains(c),
            None => false,
        }
    }
}