Skip to main content

recognize2/
recognize2.rs

1use elyze::matcher::Match;
2use elyze::recognizer::Recognizable;
3use elyze::scanner::Scanner;
4
5// define a structure to implement the `Match` trait
6#[derive(Debug)]
7struct Hello;
8
9// implement the `Match` trait
10impl Match<u8> for Hello {
11    fn is_matching(&self, data: &[u8]) -> (bool, usize) {
12        // define the pattern to match
13        let pattern = b"hello";
14        // check if the subslice of data matches the pattern
15        (&data[..pattern.len()] == pattern, pattern.len())
16    }
17
18    fn size(&self) -> usize {
19        5
20    }
21}
22
23fn main() {
24    let mut scanner = Scanner::new(b"hello world");
25    let data = Hello.recognize(&mut scanner).expect("failed to parse");
26
27    if let Some(hello) = data {
28        println!("found: {hello:?}"); // found: "Hello"
29        print!(
30            "remaining: {:?}",
31            String::from_utf8_lossy(scanner.remaining())
32        ); // remaining: " world"
33    } else {
34        println!("not found");
35    }
36}