turbofish/
turbofish.rs

1use noa_parser::matcher::{Match, MatchSize};
2
3/// Pattern to match.
4const TURBOFISH: [char; 4] = [':', ':', '<', '>'];
5
6/// Handle turbofish operator.
7struct Turbofish;
8
9/// Match turbofish operator.
10impl Match<char> for Turbofish {
11    fn matcher(&self, data: &[char]) -> (bool, usize) {
12        let pattern = &TURBOFISH;
13        if data.len() < pattern.len() {
14            return (false, 0);
15        }
16        if &data[..pattern.len()] == pattern {
17            return (true, pattern.len());
18        }
19        (false, 0)
20    }
21}
22
23/// Return the size of the turbofish operator.
24impl MatchSize for Turbofish {
25    fn size(&self) -> usize {
26        TURBOFISH.len()
27    }
28}
29
30fn main() {
31    let data = [':', ':', '<', '>', 'b'];
32    let scanner = noa_parser::scanner::Scanner::new(&data);
33    let result = Turbofish.matcher(&scanner);
34    println!("{:?}", result); // ( true, 4 ) because the turbofish operator is 4 char
35
36    let data = ['a', ':', ':', '<', '>', 'b'];
37    let scanner = noa_parser::scanner::Scanner::new(&data);
38    let result = Turbofish.matcher(&scanner);
39    println!("{:?}", result); // ( false, 0 ) because doesn't match the turbofish operator
40}