Skip to main content

last/
last.rs

1use elyze::errors::ParseResult;
2use elyze::matcher::Match;
3use elyze::peek::{peek, DefaultPeekableImplementation, Last, PeekableImplementation};
4use elyze::scanner::Scanner;
5
6#[derive(Default)]
7struct CloseParentheses;
8
9impl Match<u8> for CloseParentheses {
10    fn is_matching(&self, data: &[u8]) -> (bool, usize) {
11        if data[0] == b')' {
12            (true, 1)
13        } else {
14            (false, 0)
15        }
16    }
17
18    fn size(&self) -> usize {
19        1
20    }
21}
22
23impl PeekableImplementation for CloseParentheses {
24    type Type = DefaultPeekableImplementation;
25}
26
27fn main() -> ParseResult<()> {
28    let data = b"8 / ( 7 * ( 1 + 2 ) )";
29    let mut scanner = Scanner::new(data);
30    // consumes : "8 / ( " to reach the start of the enclosed data
31    scanner.bump_by(b"8 / (".len());
32    let result = peek(Last::new(CloseParentheses), &scanner)?;
33    if let Some(peeking) = result {
34        println!(
35            "{:?}",
36            // the peek_slice method returns the all enclosed data
37            String::from_utf8_lossy(peeking.peeked_slice()) //  7 * ( 1 + 2 )
38        );
39    }
40    Ok(())
41}