sew/
pat.rs

1use crate::constr::ConStr;
2use alloc::string::String;
3use alloc::vec::Vec;
4
5/// Trait to search for matches in strings.
6pub trait PatternSearcher {
7    /// Checks if self matches a given character.
8    fn matches_char(&self, c: char) -> bool;
9    /// Checks if self matches a given string.
10    fn matches_str(&self, s: &str) -> bool;
11}
12
13impl<F> PatternSearcher for F
14where
15    F: Fn(char) -> bool,
16{
17    fn matches_char(&self, c: char) -> bool {
18        self(c)
19    }
20
21    fn matches_str(&self, s: &str) -> bool {
22        self(s.chars().collect::<Vec<char>>()[0])
23    }
24}
25
26impl PatternSearcher for char {
27    fn matches_char(&self, c: char) -> bool {
28        *self == c
29    }
30    fn matches_str(&self, s: &str) -> bool {
31        s.contains(*self)
32    }
33}
34
35impl PatternSearcher for &str {
36    fn matches_char(&self, c: char) -> bool {
37        self.contains(c)
38    }
39
40    fn matches_str(&self, s: &str) -> bool {
41        self.contains(s)
42    }
43}
44
45impl PatternSearcher for String {
46    fn matches_char(&self, c: char) -> bool {
47        self.contains(c)
48    }
49
50    fn matches_str(&self, s: &str) -> bool {
51        self.contains(s)
52    }
53}
54
55impl<const N: usize> PatternSearcher for ConStr<N> {
56    fn matches_char(&self, c: char) -> bool {
57        self.contains(&c)
58    }
59
60    fn matches_str(&self, s: &str) -> bool {
61        self.contains(&s)
62    }
63}