harper_core/patterns/
modal_verb.rs

1use super::{Pattern, WordSet};
2
3pub struct ModalVerb {
4    inner: WordSet,
5    include_common_errors: bool,
6}
7
8impl Default for ModalVerb {
9    fn default() -> Self {
10        let (words, include_common_errors) = Self::init(false);
11        Self {
12            inner: words,
13            include_common_errors,
14        }
15    }
16}
17
18impl ModalVerb {
19    fn init(include_common_errors: bool) -> (WordSet, bool) {
20        let modals = [
21            "can", "can't", "could", "may", "might", "must", "shall", "shan't", "should", "will",
22            "won't", "would", "ought", "dare",
23        ];
24
25        let mut words = WordSet::new(&modals);
26        modals.iter().for_each(|word| {
27            words.add(&format!("{word}n't"));
28            if include_common_errors {
29                words.add(&format!("{word}nt"));
30            }
31        });
32        words.add("cannot");
33        (words, include_common_errors)
34    }
35
36    pub fn with_common_errors() -> Self {
37        let (words, _) = Self::init(true);
38        Self {
39            inner: words,
40            include_common_errors: true,
41        }
42    }
43}
44
45impl Pattern for ModalVerb {
46    fn matches(&self, tokens: &[crate::Token], source: &[char]) -> Option<usize> {
47        self.inner.matches(tokens, source)
48    }
49}