-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathid.rs
More file actions
213 lines (184 loc) · 5.75 KB
/
id.rs
File metadata and controls
213 lines (184 loc) · 5.75 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use crate::DEFAULT_NAMESPACE;
use serde::{de, Deserialize, Serialize};
use smartstring::{LazyCompact, SmartString};
use std::{
fmt::{self, Display},
str::FromStr,
};
/// A namespaced identifier, also known as a "resource location"
/// in Forge. See <https://minecraft.gamepedia.com/Namespaced_ID>.
///
/// Namespaced IDs can be parsed using the `FromStr` implementation,
/// and they can be formatted using the `Display` impl or by calling `to_string()`.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct NamespacedId {
// Smart (inlineable) strings are used to reduce heap
// fragmentation and memory usage.
namespace: SmartString<LazyCompact>,
name: SmartString<LazyCompact>,
}
impl NamespacedId {
/// Returns the namespace for this ID.
pub fn namespace(&self) -> &str {
&self.namespace
}
/// Returns the name for this ID.
pub fn name(&self) -> &str {
&self.name
}
}
/// Error returned when a namespaced ID was formatted incorrectly.
#[derive(Debug, thiserror::Error, PartialEq)]
pub enum ParseError {
#[error("'{0}' is not a valid character for namespaces")]
InvalidNamespaceChar(char),
#[error("'{0}' is not a valid character for namespaced ID names")]
InvalidNameChar(char),
}
impl FromStr for NamespacedId {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
// Determine the namespace and name components.
let mut parts = s.split(':');
let part1 = parts.next().unwrap_or("");
let part2 = parts.next();
let (namespace, name) = if let Some(part2) = part2 {
(part1, part2)
} else {
(DEFAULT_NAMESPACE, part1)
};
// Ensure that the namespace and name are legal.
validate_namespace(namespace)?;
validate_name(name)?;
Ok(NamespacedId {
namespace: SmartString::from(namespace),
name: SmartString::from(name),
})
}
}
fn validate_namespace(namespace: &str) -> Result<(), ParseError> {
for c in namespace.chars() {
if !is_valid_namespace_char(c) {
return Err(ParseError::InvalidNamespaceChar(c));
}
}
Ok(())
}
fn validate_name(name: &str) -> Result<(), ParseError> {
for c in name.chars() {
if !is_valid_name_char(c) {
return Err(ParseError::InvalidNameChar(c));
}
}
Ok(())
}
fn is_valid_namespace_char(c: char) -> bool {
c.is_ascii_alphanumeric() || c == '_' || c == '-'
}
fn is_valid_name_char(c: char) -> bool {
// Names can have some extra characters in addition
// to those allowed for namespaces.
is_valid_namespace_char(c) || c == '/' || c == '.'
}
impl<'de> Deserialize<'de> for NamespacedId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let string = SmartString::<LazyCompact>::deserialize(deserializer)?;
NamespacedId::from_str(&string).map_err(|e| de::Error::custom(e.to_string()))
}
}
impl Display for NamespacedId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}", self.namespace, self.name)
}
}
impl Serialize for NamespacedId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.to_string().serialize(serializer)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn legal_namespace_chars() {
for c in ('a'..='z').chain('A'..='Z').chain('0'..='9') {
assert!(is_valid_name_char(c));
assert!(is_valid_namespace_char(c));
}
}
#[test]
fn legal_name_chars() {
for &c in &['/', '.'] {
assert!(is_valid_name_char(c));
assert!(!is_valid_namespace_char(c));
}
}
#[test]
fn illegal_namespace_chars() {
for &c in &['/', '.', '\\', '\n', '@', '%', '?'] {
assert!(!is_valid_namespace_char(c));
}
}
#[test]
fn illegal_name_chars() {
for &c in &['\\', '\n', '@', '%', '?'] {
assert!(!is_valid_namespace_char(c));
}
}
#[test]
fn parse_id_with_namespace() {
let id = NamespacedId::from_str("namespace-caelunshun_66:folder/file.ext").unwrap();
assert_eq!(&id.name, "folder/file.ext");
assert_eq!(&id.namespace, "namespace-caelunshun_66");
}
#[test]
fn parse_id_with_default_namespace() {
let id = NamespacedId::from_str("acacia_leaves-2").unwrap();
assert_eq!(&id.name, "acacia_leaves-2");
assert_eq!(&id.namespace, DEFAULT_NAMESPACE);
}
#[test]
fn parse_empty_id() {
let id = NamespacedId::from_str("").unwrap();
assert_eq!(id.name(), "");
assert_eq!(id.namespace(), DEFAULT_NAMESPACE);
}
#[test]
fn parse_id_with_invalid_namespace() {
assert_eq!(
NamespacedId::from_str("ewhi@iho:name"),
Err(ParseError::InvalidNamespaceChar('@'))
);
assert_eq!(
NamespacedId::from_str("dir1/dir2:file"),
Err(ParseError::InvalidNamespaceChar('/'))
);
}
#[test]
fn parse_id_with_invalid_name() {
assert_eq!(
NamespacedId::from_str("name^"),
Err(ParseError::InvalidNameChar('^'))
);
assert_eq!(
NamespacedId::from_str("spaces galore"),
Err(ParseError::InvalidNameChar(' '))
);
}
#[test]
fn formatting() {
let id = NamespacedId::from_str("namespace:name").unwrap();
assert_eq!(id.to_string(), "namespace:name");
}
#[test]
fn formatting_default_namespace() {
let id = NamespacedId::from_str("name").unwrap();
assert_eq!(id.to_string(), "minecraft:name");
}
}