1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use regex::Regex;

pub trait Needle {
    fn is_match(&self, haystack: &str) -> bool;
}

pub trait NeedleIter: Needle {
    fn is_match_in<'a, I, S>(&self, haystacks: &mut I) -> bool
    where
        I: Iterator<Item = S>,
        S: Into<&'a str>,
    {
        haystacks.any(|h| self.is_match(h.into()))
    }
}

#[derive(Debug, Clone)]
pub enum StringMatchLength {
    /// Needle string must match the whole haystack string.
    Full,
    /// Needle string can be any substring within the haystack string.
    Partial,
    /// Needle string will only match strings within the haystack surrounded by spaces or
    /// a string boundary.
    Word,
}

#[derive(Debug, Clone)]
pub struct StringMatch {
    text: String,
    /// The match length to use. Default is StringMatchLength::Full, which means the needle
    /// string must match the entire haystack.
    match_length: StringMatchLength,
    /// If true, use a case-sensitive match. Default is true.
    case_sensitive: bool,
}

impl<S> From<S> for StringMatch
where
    S: Into<String>,
{
    fn from(text: S) -> Self {
        Self {
            text: text.into(),
            match_length: StringMatchLength::Full,
            case_sensitive: true,
        }
    }
}

impl StringMatch {
    pub fn new<S>(text: S) -> Self
    where
        S: Into<String>,
    {
        Self::from(text)
    }

    pub fn is_full_match(&self) -> bool {
        matches!(self.match_length, StringMatchLength::Full)
    }

    pub fn is_partial_match(&self) -> bool {
        matches!(self.match_length, StringMatchLength::Partial)
    }

    pub fn is_word_match(&self) -> bool {
        matches!(self.match_length, StringMatchLength::Word)
    }

    pub fn is_case_sensitive(&self) -> bool {
        self.case_sensitive
    }

    pub fn partial(mut self) -> Self {
        self.match_length = StringMatchLength::Partial;
        self
    }

    pub fn full(mut self) -> Self {
        self.match_length = StringMatchLength::Full;
        self
    }

    pub fn word(mut self) -> Self {
        self.match_length = StringMatchLength::Word;
        self
    }

    pub fn case_insensitive(mut self) -> Self {
        self.case_sensitive = false;
        self
    }

    pub fn case_sensitive(mut self) -> Self {
        self.case_sensitive = true;
        self
    }
}

fn needle_in_haystack(needle: &str, haystack: &str, match_length: &StringMatchLength) -> bool {
    match match_length {
        StringMatchLength::Full => haystack == needle,
        StringMatchLength::Partial => haystack.contains(needle),
        StringMatchLength::Word => format!(" {} ", haystack).contains(&format!(" {} ", needle)),
    }
}

impl Needle for StringMatch {
    fn is_match(&self, haystack: &str) -> bool {
        match self.case_sensitive {
            true => needle_in_haystack(&self.text, haystack, &self.match_length),
            false => {
                let hs = haystack.to_lowercase();
                let needle = self.text.to_lowercase();
                needle_in_haystack(&needle, &hs, &self.match_length)
            }
        }
    }
}

impl Needle for Regex {
    fn is_match(&self, haystack: &str) -> bool {
        self.is_match(haystack)
    }
}

impl Needle for &str {
    fn is_match(&self, haystack: &str) -> bool {
        self == &haystack
    }
}

impl Needle for String {
    fn is_match(&self, haystack: &str) -> bool {
        self == haystack
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_stringmatch() {
        assert!(StringMatch::from("a").is_full_match());
        assert!(!StringMatch::from("a").is_partial_match());
        assert!(StringMatch::from("a").is_case_sensitive());
        assert!(StringMatch::from("a").is_match("a"));
        assert!(!StringMatch::from("a").is_match(""));
        assert!(!StringMatch::from("a").is_match("b"));
        assert!(!StringMatch::from("a").is_match("A"));

        assert!(StringMatch::from("a").partial().is_partial_match());
        assert!(!StringMatch::from("a").partial().is_full_match());
        assert!(!StringMatch::from("a").partial().is_word_match());
        assert!(StringMatch::from("a").partial().is_case_sensitive());
        assert!(StringMatch::from("a").partial().is_match("a"));
        assert!(StringMatch::from("a").partial().is_match("aa"));
        assert!(StringMatch::from("a").partial().is_match("dad"));
        assert!(StringMatch::from("a").partial().is_match("ba"));
        assert!(!StringMatch::from("A").partial().is_match("a"));
        assert!(!StringMatch::from("a").partial().is_match("A"));

        assert!(!StringMatch::from("a").case_insensitive().is_case_sensitive());
        assert!(!StringMatch::from("a").case_insensitive().is_partial_match());
        assert!(StringMatch::from("a").case_insensitive().is_match("a"));
        assert!(StringMatch::from("a").case_insensitive().is_match("A"));
        assert!(!StringMatch::from("a").case_insensitive().is_match("aa"));

        assert!(StringMatch::from("a").partial().case_insensitive().is_partial_match());
        assert!(!StringMatch::from("a").partial().case_insensitive().is_case_sensitive());
        assert!(StringMatch::from("a").partial().case_insensitive().is_match("a"));
        assert!(StringMatch::from("a").partial().case_insensitive().is_match("aa"));
        assert!(StringMatch::from("a").partial().case_insensitive().is_match("A"));
        assert!(StringMatch::from("a").partial().case_insensitive().is_match("AA"));
        assert!(StringMatch::from("aA").partial().case_insensitive().is_match("Aa"));
        assert!(StringMatch::from("aA").partial().case_insensitive().is_match("Aaa"));

        assert!(StringMatch::from("a").word().is_word_match());
        assert!(!StringMatch::from("a").word().is_partial_match());
        assert!(!StringMatch::from("a").word().is_full_match());
        assert!(StringMatch::from("a").word().is_case_sensitive());
        assert!(StringMatch::from("a").word().is_match("a"));
        assert!(!StringMatch::from("a").word().is_match("aa"));
        assert!(!StringMatch::from("a").word().is_match("dad"));
        assert!(!StringMatch::from("a").word().is_match("ba"));
        assert!(StringMatch::from("a").word().is_match("aa a aa"));
        assert!(StringMatch::from("a").word().is_match("a aa"));
        assert!(StringMatch::from("a").word().is_match("aa a"));
        assert!(!StringMatch::from("A").word().is_match("a"));
        assert!(!StringMatch::from("a").word().is_match("A"));
        assert!(StringMatch::from("aaa aa").word().case_insensitive().is_match("aaa aa"));
        assert!(StringMatch::from("aaa aa").word().case_insensitive().is_match("aa aaa aa aaa"));
        assert!(StringMatch::from("aaa aa").word().case_insensitive().is_match("aaa aa aaa"));
        assert!(StringMatch::from("aaa aa").word().case_insensitive().is_match("aa aaa aa"));
        assert!(!StringMatch::from("aaa aa").word().case_insensitive().is_match("aa aaa aaa"));

        assert!(StringMatch::from("a").word().case_insensitive().is_word_match());
        assert!(!StringMatch::from("a").word().case_insensitive().is_case_sensitive());
        assert!(StringMatch::from("a").word().case_insensitive().is_match("a"));
        assert!(!StringMatch::from("a").word().case_insensitive().is_match("aa"));
        assert!(StringMatch::from("a").word().case_insensitive().is_match("A"));
        assert!(StringMatch::from("a").word().case_insensitive().is_match("AA A AA"));
        assert!(StringMatch::from("aA").word().case_insensitive().is_match("Aa"));
        assert!(StringMatch::from("aA").word().case_insensitive().is_match("aa"));
        assert!(StringMatch::from("A").word().case_insensitive().is_match("aa a aa"));
        assert!(StringMatch::from("A").word().case_insensitive().is_match("a aa"));
        assert!(StringMatch::from("A").word().case_insensitive().is_match("aa a"));
        assert!(StringMatch::from("AAA AA").word().case_insensitive().is_match("aaa aa"));
        assert!(StringMatch::from("AAA AA").word().case_insensitive().is_match("aa aaa aa aaa"));
        assert!(StringMatch::from("AAA AA").word().case_insensitive().is_match("aaa aa aaa"));
        assert!(StringMatch::from("AAA AA").word().case_insensitive().is_match("aa aaa aa"));
        assert!(!StringMatch::from("AAA AA").word().case_insensitive().is_match("aa aaa aaa"));
    }

    fn needle_is_match<N>(needle: N) -> bool
    where
        N: Needle,
    {
        needle.is_match("Test")
    }

    #[test]
    fn test_needle() {
        assert!(needle_is_match("Test"));
        assert!(!needle_is_match("test")); // Strings are case-sensitive.
        assert!(!needle_is_match("Te")); // Strings always match whole haystack.

        assert!(needle_is_match(String::from("Test")));
        assert!(!needle_is_match(String::from("test"))); // Strings are case-sensitive.
        assert!(!needle_is_match(String::from("Te"))); // Strings always match whole haystack.

        assert!(needle_is_match(StringMatch::from("Test")));
        assert!(!needle_is_match(StringMatch::from("test")));
        assert!(needle_is_match(StringMatch::from("test").case_insensitive()));
        assert!(needle_is_match(StringMatch::from("Te").partial()));
        assert!(!needle_is_match(StringMatch::from("te").partial()));
        assert!(needle_is_match(StringMatch::from("te").partial().case_insensitive()));

        assert!(needle_is_match(Regex::new("Test").unwrap()));
        assert!(needle_is_match(Regex::new("Te").unwrap())); // Regex is partial by default unless ^$ specified.
        assert!(!needle_is_match(Regex::new("te").unwrap())); // Regex is case-sensitive by default.
        assert!(needle_is_match(Regex::new(r"(?i)te").unwrap())); // Case insensitive.
        assert!(needle_is_match(Regex::new(r"\w+").unwrap()));
        assert!(needle_is_match(Regex::new(r"\w").unwrap()));
        assert!(!needle_is_match(Regex::new(r"^T$").unwrap()));
        assert!(!needle_is_match(Regex::new(r"^est").unwrap()));
        assert!(!needle_is_match(Regex::new(r"Te$").unwrap()));
        assert!(needle_is_match(Regex::new(r"^T.+t$").unwrap()));
    }

    fn dynamic_dispatched_needle(needle: &dyn Needle) -> bool {
        needle.is_match("Test")
    }

    #[test]
    fn test_dyn_needle() {
        assert!(dynamic_dispatched_needle(&"Test"));
        assert!(!dynamic_dispatched_needle(&"test")); // Strings are case-sensitive.
        assert!(!dynamic_dispatched_needle(&"Te")); // Strings always match whole haystack.

        assert!(dynamic_dispatched_needle(&String::from("Test")));
        assert!(!dynamic_dispatched_needle(&String::from("test"))); // Strings are case-sensitive.
        assert!(!dynamic_dispatched_needle(&String::from("Te"))); // Strings always match whole haystack.

        assert!(dynamic_dispatched_needle(&StringMatch::from("Test")));
        assert!(!dynamic_dispatched_needle(&StringMatch::from("test")));
        assert!(dynamic_dispatched_needle(&StringMatch::from("test").case_insensitive()));
        assert!(dynamic_dispatched_needle(&StringMatch::from("Te").partial()));
        assert!(!dynamic_dispatched_needle(&StringMatch::from("te").partial()));
        assert!(dynamic_dispatched_needle(&StringMatch::from("te").partial().case_insensitive()));

        assert!(dynamic_dispatched_needle(&Regex::new("Test").unwrap()));
        assert!(dynamic_dispatched_needle(&Regex::new("Te").unwrap())); // Regex is partial by default unless ^$ specified.
        assert!(!dynamic_dispatched_needle(&Regex::new("te").unwrap())); // Regex is case-sensitive by default.
        assert!(dynamic_dispatched_needle(&Regex::new(r"(?i)te").unwrap())); // Case insensitive.
        assert!(dynamic_dispatched_needle(&Regex::new(r"\w+").unwrap()));
        assert!(dynamic_dispatched_needle(&Regex::new(r"\w").unwrap()));
        assert!(!dynamic_dispatched_needle(&Regex::new(r"^T$").unwrap()));
        assert!(!dynamic_dispatched_needle(&Regex::new(r"^est").unwrap()));
        assert!(!dynamic_dispatched_needle(&Regex::new(r"Te$").unwrap()));
        assert!(dynamic_dispatched_needle(&Regex::new(r"^T.+t$").unwrap()));
    }
}