okkhor/
models.rs

1use std::collections::BTreeMap;
2
3#[derive(Clone)]
4pub(crate) enum MatchType {
5    Vowel,
6    Consonant,
7    Punctuation,
8    Number,
9    Char(char),
10}
11
12#[derive(Clone)]
13pub(crate) enum Match {
14    PrefixIs(MatchType),
15    SuffixIs(MatchType),
16    PrefixIsNot(MatchType),
17    SuffixIsNot(MatchType),
18}
19
20pub(crate) struct Rule {
21    pub when_matches: &'static [Match],
22    pub replace_with: &'static str,
23}
24
25pub(crate) struct Pattern {
26    pub find: &'static str,
27    pub rules: &'static [Rule],
28    pub default_replacement: &'static str,
29}
30
31const NO_RULES: &[Rule] = &[];
32
33impl Pattern {
34    pub const fn simple_replace(find: &'static str, replace: &'static str) -> Pattern {
35        Pattern {
36            find,
37            rules: NO_RULES,
38            default_replacement: replace,
39        }
40    }
41}
42
43pub(crate) struct Patterns(BTreeMap<&'static str, &'static Pattern>);
44
45impl Patterns {
46    pub(crate) fn new(patterns_input: &'static [Pattern]) -> Self {
47        let patterns = patterns_input
48            .iter()
49            .map(|p| (p.find, p))
50            .collect();
51        Self(patterns)
52    }
53
54    pub(crate) fn find_pattern(&self, input: &str) -> Option<&Pattern> {
55        self.0
56            .range(..=input)
57            .rfind(|(&k, _)| input.starts_with(k))
58            .map(|(_, &p)| p)
59    }
60}