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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::fmt;

/// Represents the path of an extern item, like extern fn
/// This path only supports a very limited set of characters for now.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct ExternItemPath(String);

impl ExternItemPath {
    /// A Regex of valid characters (See [Minecraft Wiki](https://minecraft.fandom.com/wiki/Resource_location#Legal_characters))
    /// Currently excludes "." to avoid confusion
    pub const VALID_CHARACTERS: &'static str = "[a-z0-9_-/]";

    pub fn new(path: String) -> Result<Self, ItemPathError> {
        let path = Self::verify(path)?;
        Ok(ExternItemPath(path))
    }

    pub fn verify(path: String) -> Result<String, ItemPathError> {
        if path.starts_with("__") {
            return Err(ItemPathError::InvalidDoubleUnderscore(path));
        }

        for chr in path.chars() {
            match chr {
                'a'..='z' | '0'..='9' | '_' | '-' | '/' => {}
                _ => return Err(ItemPathError::InvalidCharacter(path, chr)),
            }
        }
        Ok(path)
    }
}

impl AsRef<str> for ExternItemPath {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

pub enum ItemPathError {
    InvalidCharacter(String, char),
    InvalidDoubleUnderscore(String),
}

impl ItemPathError {
    pub fn path(self) -> String {
        match self {
            ItemPathError::InvalidCharacter(path, _)
            | ItemPathError::InvalidDoubleUnderscore(path) => path,
        }
    }
}

impl fmt::Display for ItemPathError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ItemPathError::InvalidCharacter(_, char) => write!(
                f,
                "Invalid character: '{char}' (Valid characters are {})",
                ExternItemPath::VALID_CHARACTERS
            ),
            ItemPathError::InvalidDoubleUnderscore(_) => write!(
                f,
                "Extern item paths are not allowed to start with double underscores"
            ),
        }
    }
}