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

use super::OneOf;

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

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

impl<const CHARS: &'static str> Parse<char> for AnyOf<CHARS> {
    fn parse(input: &mut impl Buffer<char>) -> eyre::Result<Self> {
        let mut output = String::new();

        loop {
            let mut cursor = input.cursor();
            match OneOf::<CHARS>::parse(&mut cursor) {
                Ok(c) => output.push(c.into()),
                Err(_) => break,
            }
            cursor.fast_forward_parent();
        }

        Ok(Self(output))
    }

    fn peek(input: &mut impl Buffer<char>) -> bool {
        loop {
            let mut cursor = input.cursor();
            if !OneOf::<CHARS>::peek(&mut cursor) {
                break;
            }
            cursor.fast_forward_parent()
        }
        true
    }
}

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

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

impl<const CHARS: &'static str> Parse<char> for WhileNot1<CHARS> {
    fn parse(input: &mut impl Buffer<char>) -> eyre::Result<Self> {
        let mut output = String::new();

        while !OneOf::<CHARS>::peek(&mut input.cursor()) {
            match input.next() {
                None => break,
                Some(c) => output.push(c),
            }
        }

        if output.is_empty() {
            Err(eyre::eyre!("no characters found"))
        } else {
            Ok(Self(output))
        }
    }

    fn peek(input: &mut impl Buffer<char>) -> bool {
        if OneOf::<CHARS>::peek(input) {
            return false;
        }
        loop {
            let mut cursor = input.cursor();
            if OneOf::<CHARS>::peek(&mut cursor) {
                break;
            }
            cursor.fast_forward_parent()
        }
        true
    }
}

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

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

impl<const CHARS: &'static str> Parse<char> for AnyOf1<CHARS> {
    fn parse(input: &mut impl Buffer<char>) -> eyre::Result<Self> {
        let mut output = String::new();

        loop {
            let mut cursor = input.cursor();
            match OneOf::<CHARS>::parse(&mut cursor) {
                Ok(c) => output.push(c.into()),
                Err(_) => break,
            }
            cursor.fast_forward_parent();
        }

        if output.is_empty() {
            Err(eyre::eyre!("no characters found"))
        } else {
            Ok(Self(output))
        }
    }

    fn peek(input: &mut impl Buffer<char>) -> bool {
        if !OneOf::<CHARS>::peek(input) {
            return false;
        }
        loop {
            let mut cursor = input.cursor();
            if !OneOf::<CHARS>::peek(&mut cursor) {
                break;
            }
            cursor.fast_forward_parent()
        }
        true
    }
}

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

    #[test]
    fn any_of_peek() {
        let mut buffer = "1024$".chars().into_buf();
        let mut cursor = buffer.cursor();
        assert!(AnyOf::<"0123456789">::peek(&mut cursor));
        assert_eq!(cursor.next(), Some('$'));
    }
}