use super::{normalize_string, Filter};
struct NormalizedMatcher {
pattern: Option<String>,
}
impl NormalizedMatcher {
fn new() -> Self {
Self { pattern: None }
}
fn with_pattern(mut self, pattern: &str) -> Self {
self.pattern = Some(normalize_string(&pattern.to_lowercase()));
self
}
fn matches(&self, subject: &str) -> bool {
let Some(pattern) = self.pattern.as_ref() else { return true };
let subject = normalize_string(&subject.to_lowercase());
subject.contains(pattern)
}
}
pub fn new_filter(pattern: &str) -> impl Filter {
let searcher = NormalizedMatcher::new().with_pattern(pattern);
move |room| -> bool {
let Some(room_name) = room.cached_display_name() else { return false };
searcher.matches(&room_name)
}
}
#[cfg(test)]
mod tests {
use std::ops::Not;
use super::*;
#[test]
fn test_no_pattern() {
let matcher = NormalizedMatcher::new();
assert!(matcher.matches("hello"));
}
#[test]
fn test_empty_pattern() {
let matcher = NormalizedMatcher::new();
assert!(matcher.matches("hello"));
}
#[test]
fn test_literal() {
let matcher = NormalizedMatcher::new();
let matcher = matcher.with_pattern("matrix");
assert!(matcher.matches("matrix"));
let matcher = matcher.with_pattern("matrxi");
assert!(matcher.matches("matrix").not());
}
#[test]
fn test_ignore_case() {
let matcher = NormalizedMatcher::new();
let matcher = matcher.with_pattern("matrix");
assert!(matcher.matches("MaTrIX"));
let matcher = matcher.with_pattern("matrxi");
assert!(matcher.matches("MaTrIX").not());
}
#[test]
fn test_normalization() {
let matcher = NormalizedMatcher::new();
let matcher = matcher.with_pattern("un été");
assert_eq!(matcher.pattern, Some("un ete".to_owned()));
assert!(matcher.matches("un été magnifique"));
let matcher = matcher.with_pattern("stefan");
assert!(matcher.matches("Ștefan"));
}
}