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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// McFunction-Debugger is a debugger for Minecraft's *.mcfunction files that does not require any
// Minecraft mods.
//
// © Copyright (C) 2021-2023 Adrodoc <adrodoc55@googlemail.com> & skess42 <skagaros@gmail.com>
//
// This file is part of McFunction-Debugger.
//
// McFunction-Debugger is free software: you can redistribute it and/or modify it under the terms of
// the GNU General Public License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// McFunction-Debugger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with McFunction-Debugger.
// If not, see <http://www.gnu.org/licenses/>.

use std::{cmp::Ordering, convert::TryFrom, fmt::Display, hash::Hash};

pub type ResourceLocation = ResourceLocationRef<String>;

#[derive(Clone, Debug)]
pub struct ResourceLocationRef<S: AsRef<str>> {
    string: S,
    namespace_len: usize,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum InvalidResourceLocation {
    InvalidNamespace,
    InvalidPath,
}

impl<'l> TryFrom<&'l str> for ResourceLocationRef<&'l str> {
    type Error = InvalidResourceLocation;

    fn try_from(string: &'l str) -> Result<Self, Self::Error> {
        let (path, namespace_len) = if let Some((namespace, path)) = string.split_once(':') {
            if !namespace.chars().all(is_valid_namespace_char) {
                return Err(InvalidResourceLocation::InvalidNamespace);
            }
            (path, namespace.len())
        } else {
            (string, usize::MAX)
        };

        if !path.chars().all(is_valid_path_char) {
            Err(InvalidResourceLocation::InvalidPath)
        } else {
            Ok(ResourceLocationRef {
                string,
                namespace_len,
            })
        }
    }
}

fn is_valid_namespace_char(c: char) -> bool {
    c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c == '-' || c == '.' || c == '_'
}

fn is_valid_path_char(c: char) -> bool {
    c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c == '-' || c == '.' || c == '/' || c == '_'
}

impl<S: AsRef<str>> ResourceLocationRef<S> {
    pub fn new(namespace: &str, path: &str) -> ResourceLocation {
        ResourceLocationRef {
            string: format!("{}:{}", namespace, path),
            namespace_len: namespace.len(),
        }
    }

    pub fn namespace(&self) -> &str {
        if self.namespace_len == usize::MAX {
            "minecraft"
        } else {
            &self.string.as_ref()[..self.namespace_len]
        }
    }

    pub fn path(&self) -> &str {
        if self.namespace_len == usize::MAX {
            self.string.as_ref()
        } else {
            &self.string.as_ref()[self.namespace_len + 1..]
        }
    }

    pub fn to_owned(&self) -> ResourceLocation {
        ResourceLocation {
            string: self.string.as_ref().to_owned(),
            namespace_len: self.namespace_len,
        }
    }

    pub fn mcfunction_path(&self) -> String {
        format!("{}/functions/{}.mcfunction", self.namespace(), self.path())
            .replace('/', &std::path::MAIN_SEPARATOR.to_string())
    }
}

impl ResourceLocation {
    pub fn to_ref(&self) -> ResourceLocationRef<&str> {
        ResourceLocationRef {
            string: &self.string,
            namespace_len: self.namespace_len,
        }
    }
}

impl<S: AsRef<str>> PartialEq for ResourceLocationRef<S> {
    fn eq(&self, other: &Self) -> bool {
        self.namespace() == other.namespace() && self.path() == other.path()
    }
}

impl<S: AsRef<str>> Eq for ResourceLocationRef<S> {}

impl<S: AsRef<str>> PartialOrd for ResourceLocationRef<S> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<S: AsRef<str>> Ord for ResourceLocationRef<S> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.namespace()
            .cmp(other.namespace())
            .then_with(|| self.path().cmp(other.path()))
    }
}

impl<S: AsRef<str>> Hash for ResourceLocationRef<S> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.namespace().hash(state);
        self.path().hash(state);
    }
}

impl<'l> ResourceLocationRef<&'l str> {
    pub fn parse(string: &'l str) -> Result<(Self, usize), String> {
        const INVALID_ID: &str = "Invalid ID";

        let len = string
            .find(|c| !Self::is_allowed_in_resource_location(c))
            .unwrap_or(string.len());
        let resource_location = &string[..len];

        let resource_location =
            ResourceLocationRef::try_from(resource_location).map_err(|_| INVALID_ID.to_string())?;
        Ok((resource_location, len))
    }

    fn is_allowed_in_resource_location(c: char) -> bool {
        return c >= '0' && c <= '9'
            || c >= 'a' && c <= 'z'
            || c == '-'
            || c == '.'
            || c == '/'
            || c == ':'
            || c == '_';
    }
}

impl<S: AsRef<str>> Display for ResourceLocationRef<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.string.as_ref().fmt(f)
    }
}