azalea_registry/
identifier.rs

1//! An arbitrary identifier or resource location.
2
3use std::{
4    fmt::{self, Debug, Display},
5    hash::{Hash, Hasher},
6    io::{self, Cursor, Write},
7    num::NonZeroUsize,
8    str::FromStr,
9};
10
11use azalea_buf::{AzaleaRead, AzaleaWrite, BufReadError};
12#[cfg(feature = "serde")]
13use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
14use simdnbt::{FromNbtTag, ToNbtTag, owned::NbtTag};
15
16/// An identifier, like `minecraft:stone` or `brigadier:number`.
17///
18/// All registry variants can be converted to an identifier.
19///
20/// This was formerly called a `ResourceLocation`.
21#[doc(alias = "ResourceLocation")]
22#[derive(Clone, Default, Eq)]
23pub struct Identifier {
24    // empty namespaces aren't allowed so NonZero is fine.
25    colon_index: Option<NonZeroUsize>,
26    inner: Box<str>,
27}
28
29static DEFAULT_NAMESPACE: &str = "minecraft";
30// static REALMS_NAMESPACE: &str = "realms";
31
32impl Identifier {
33    pub fn new(resource_string: impl Into<String>) -> Identifier {
34        let mut resource_string = resource_string.into();
35
36        let colon_index = resource_string.find(':');
37        let colon_index = if let Some(colon_index) = colon_index {
38            if colon_index == 0 {
39                resource_string = resource_string.split_off(1);
40            }
41            NonZeroUsize::new(colon_index)
42        } else {
43            None
44        };
45
46        Self {
47            colon_index,
48            inner: resource_string.into(),
49        }
50    }
51
52    pub fn namespace(&self) -> &str {
53        if let Some(colon_index) = self.colon_index {
54            &self.inner[0..colon_index.get()]
55        } else {
56            DEFAULT_NAMESPACE
57        }
58    }
59    pub fn path(&self) -> &str {
60        if let Some(colon_index) = self.colon_index {
61            &self.inner[(colon_index.get() + 1)..]
62        } else {
63            &self.inner
64        }
65    }
66}
67impl PartialEq for Identifier {
68    fn eq(&self, other: &Self) -> bool {
69        self.namespace() == other.namespace() && self.path() == other.path()
70    }
71}
72impl Hash for Identifier {
73    fn hash<H: Hasher>(&self, state: &mut H) {
74        let namespace = self.namespace();
75        if namespace != DEFAULT_NAMESPACE {
76            namespace.hash(state);
77        }
78        self.path().hash(state);
79    }
80}
81
82impl Display for Identifier {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        if self.colon_index.is_some() {
85            write!(f, "{}", self.inner)
86        } else {
87            write!(f, "{DEFAULT_NAMESPACE}:{}", self.inner)
88        }
89    }
90}
91impl Debug for Identifier {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        write!(f, "{self}")
94    }
95}
96impl FromStr for Identifier {
97    type Err = &'static str;
98
99    fn from_str(s: &str) -> Result<Self, Self::Err> {
100        Ok(Identifier::new(s))
101    }
102}
103impl From<&str> for Identifier {
104    fn from(s: &str) -> Self {
105        Identifier::new(s)
106    }
107}
108
109impl AzaleaRead for Identifier {
110    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
111        let location_string = String::azalea_read(buf)?;
112        Ok(Identifier::new(&location_string))
113    }
114}
115impl AzaleaWrite for Identifier {
116    fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> {
117        self.to_string().azalea_write(buf)
118    }
119}
120
121#[cfg(feature = "serde")]
122impl Serialize for Identifier {
123    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
124    where
125        S: Serializer,
126    {
127        serializer.serialize_str(&self.to_string())
128    }
129}
130
131#[cfg(feature = "serde")]
132impl<'de> Deserialize<'de> for Identifier {
133    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
134    where
135        D: Deserializer<'de>,
136    {
137        let s = String::deserialize(deserializer)?;
138        if s.contains(':') {
139            Ok(Identifier::new(&s))
140        } else {
141            Err(de::Error::invalid_value(
142                de::Unexpected::Str(&s),
143                &"a valid Identifier",
144            ))
145        }
146    }
147}
148
149impl FromNbtTag for Identifier {
150    fn from_nbt_tag(tag: simdnbt::borrow::NbtTag) -> Option<Self> {
151        tag.string().and_then(|s| s.to_str().parse().ok())
152    }
153}
154
155impl ToNbtTag for Identifier {
156    fn to_nbt_tag(self) -> NbtTag {
157        NbtTag::String(self.to_string().into())
158    }
159}
160
161#[cfg(test)]
162mod tests {
163    use super::*;
164
165    #[test]
166    fn basic_identifier() {
167        let r = Identifier::new("abcdef:ghijkl");
168        assert_eq!(r.namespace(), "abcdef");
169        assert_eq!(r.path(), "ghijkl");
170    }
171    #[test]
172    fn no_namespace() {
173        let r = Identifier::new("azalea");
174        assert_eq!(r.namespace(), "minecraft");
175        assert_eq!(r.path(), "azalea");
176    }
177    #[test]
178    fn colon_start() {
179        let r = Identifier::new(":azalea");
180        assert_eq!(r.namespace(), "minecraft");
181        assert_eq!(r.path(), "azalea");
182    }
183    #[test]
184    fn colon_end() {
185        let r = Identifier::new("azalea:");
186        assert_eq!(r.namespace(), "azalea");
187        assert_eq!(r.path(), "");
188    }
189
190    #[test]
191    fn azbuf_identifier() {
192        let mut buf = Vec::new();
193        Identifier::new("minecraft:dirt")
194            .azalea_write(&mut buf)
195            .unwrap();
196
197        let mut buf = Cursor::new(&buf[..]);
198
199        assert_eq!(
200            Identifier::azalea_read(&mut buf).unwrap(),
201            Identifier::new("minecraft:dirt")
202        );
203    }
204}