twilight_command_parser/
casing.rs

1use unicase::UniCase;
2
3/// Case sensitivity of a command.
4#[derive(Clone, Debug, Eq, Hash, PartialEq)]
5pub enum CaseSensitivity {
6    /// A case-insensitive command. "ping" and "Ping" are equivalent.
7    Insensitive(UniCase<String>),
8    /// A case-sensitive command. "ping" and "Ping" are distinguished from each
9    /// other.
10    Sensitive(String),
11}
12
13impl CaseSensitivity {
14    pub const fn is_sensitive(&self) -> bool {
15        matches!(self, Self::Sensitive(_))
16    }
17}
18
19impl AsRef<str> for CaseSensitivity {
20    fn as_ref(&self) -> &str {
21        match self {
22            Self::Insensitive(u) => u.as_str(),
23            Self::Sensitive(s) => s.as_str(),
24        }
25    }
26}
27
28impl AsMut<str> for CaseSensitivity {
29    fn as_mut(&mut self) -> &mut str {
30        match self {
31            Self::Insensitive(u) => u.as_mut_str(),
32            Self::Sensitive(s) => s.as_mut_str(),
33        }
34    }
35}
36
37impl PartialEq<str> for CaseSensitivity {
38    fn eq(&self, other: &str) -> bool {
39        match self {
40            Self::Insensitive(u) => u == &UniCase::new(other),
41            Self::Sensitive(s) => s == other,
42        }
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::CaseSensitivity;
49    use static_assertions::assert_impl_all;
50    use std::{fmt::Debug, hash::Hash};
51
52    assert_impl_all!(
53        CaseSensitivity: AsRef<str>,
54        Clone,
55        Debug,
56        Eq,
57        Hash,
58        PartialEq,
59        PartialEq<str>,
60        Send,
61        Sync
62    );
63}