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
use unicase::UniCase;

/// Case sensitivity of a command.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum CaseSensitivity {
    /// A case-insensitive command. "ping" and "Ping" are equivalent.
    Insensitive(UniCase<String>),
    /// A case-sensitive command. "ping" and "Ping" are distinguished from each
    /// other.
    Sensitive(String),
}

impl AsRef<str> for CaseSensitivity {
    fn as_ref(&self) -> &str {
        match self {
            Self::Insensitive(u) => u.as_str(),
            Self::Sensitive(s) => s.as_str(),
        }
    }
}

impl PartialEq<str> for CaseSensitivity {
    fn eq(&self, other: &str) -> bool {
        match self {
            Self::Insensitive(u) => u == &UniCase::new(other),
            Self::Sensitive(s) => s == other,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::CaseSensitivity;
    use static_assertions::assert_impl_all;
    use std::{fmt::Debug, hash::Hash};

    assert_impl_all!(
        CaseSensitivity: AsRef<str>,
        Clone,
        Debug,
        Eq,
        Hash,
        PartialEq,
        PartialEq<str>,
        Send,
        Sync
    );
}