Skip to main content

appletheia_application/command/
command_name.rs

1use std::fmt::{self, Display};
2
3#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
4pub struct CommandName(&'static str);
5
6impl CommandName {
7    pub const MAX_LENGTH: usize = 100;
8
9    pub const fn new(value: &'static str) -> Self {
10        let bytes = value.as_bytes();
11        let len = bytes.len();
12        if len == 0 {
13            panic!("command name is empty");
14        }
15        if len > Self::MAX_LENGTH {
16            panic!("command name is too long");
17        }
18        let mut i = 0;
19        while i < len {
20            let b = bytes[i];
21            let is_lower = b >= b'a' && b <= b'z';
22            let is_digit = b >= b'0' && b <= b'9';
23            let is_underscore = b == b'_';
24
25            if !(is_lower || is_digit || is_underscore) {
26                panic!("command name must be snake_case ascii: [a-z0-9_]");
27            }
28
29            i += 1;
30        }
31        Self(value)
32    }
33
34    pub fn value(&self) -> &'static str {
35        self.0
36    }
37}
38
39impl Display for CommandName {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        write!(f, "{}", self.value())
42    }
43}