1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use regex::Regex;

/// This filter is responsible how sensitive command will be
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Filter {
    /// Must be exactly same command
    Strict,
    /// Can have some spaces, and can be uppercase, but still must be same command
    Flexible,
    /// Can be triggered without any symbol, and also can be uppercase
    Sensitive,
}

pub fn matchit(message: &str, command: &str, filter: &Filter) -> bool {
    let pattern = match filter {
        Filter::Strict => format!(r"^{}$", regex::escape(command)),
        Filter::Flexible => format!(r"(?i)^\s*[^\w\s]?{}\s*$", regex::escape(command)),
        Filter::Sensitive => format!(
            r"(?i)(?:^|[\W_]){}(?:[\W_]|$)",
            regex::escape(&command.trim_start_matches(|c: char| !c.is_alphanumeric()))
        ),
    };
    Regex::new(&pattern).unwrap().is_match(message)
}