azalea_core/
resource_location.rs

1//! A resource, like minecraft:stone
2
3use std::{
4    fmt,
5    io::{Cursor, Write},
6    str::FromStr,
7};
8
9use azalea_buf::{AzaleaRead, AzaleaWrite, BufReadError};
10#[cfg(feature = "serde")]
11use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
12use simdnbt::{owned::NbtTag, FromNbtTag, ToNbtTag};
13
14#[derive(Hash, Clone, PartialEq, Eq)]
15pub struct ResourceLocation {
16    pub namespace: String,
17    pub path: String,
18}
19
20static DEFAULT_NAMESPACE: &str = "minecraft";
21// static REALMS_NAMESPACE: &str = "realms";
22
23impl ResourceLocation {
24    pub fn new(resource_string: &str) -> ResourceLocation {
25        let sep_byte_position_option = resource_string.chars().position(|c| c == ':');
26        let (namespace, path) = if let Some(sep_byte_position) = sep_byte_position_option {
27            if sep_byte_position == 0 {
28                (DEFAULT_NAMESPACE, &resource_string[1..])
29            } else {
30                (
31                    &resource_string[..sep_byte_position],
32                    &resource_string[sep_byte_position + 1..],
33                )
34            }
35        } else {
36            (DEFAULT_NAMESPACE, resource_string)
37        };
38        ResourceLocation {
39            namespace: namespace.to_string(),
40            path: path.to_string(),
41        }
42    }
43}
44
45impl fmt::Display for ResourceLocation {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        write!(f, "{}:{}", self.namespace, self.path)
48    }
49}
50impl fmt::Debug for ResourceLocation {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        write!(f, "{}:{}", self.namespace, self.path)
53    }
54}
55impl FromStr for ResourceLocation {
56    type Err = &'static str;
57
58    fn from_str(s: &str) -> Result<Self, Self::Err> {
59        Ok(ResourceLocation::new(s))
60    }
61}
62
63impl AzaleaRead for ResourceLocation {
64    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
65        let location_string = String::azalea_read(buf)?;
66        Ok(ResourceLocation::new(&location_string))
67    }
68}
69impl AzaleaWrite for ResourceLocation {
70    fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
71        self.to_string().azalea_write(buf)
72    }
73}
74
75#[cfg(feature = "serde")]
76impl Serialize for ResourceLocation {
77    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
78    where
79        S: Serializer,
80    {
81        serializer.serialize_str(&self.to_string())
82    }
83}
84
85#[cfg(feature = "serde")]
86impl<'de> Deserialize<'de> for ResourceLocation {
87    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
88    where
89        D: Deserializer<'de>,
90    {
91        let s = String::deserialize(deserializer)?;
92        if s.contains(':') {
93            Ok(ResourceLocation::new(&s))
94        } else {
95            Err(de::Error::invalid_value(
96                de::Unexpected::Str(&s),
97                &"a valid ResourceLocation",
98            ))
99        }
100    }
101}
102
103impl FromNbtTag for ResourceLocation {
104    fn from_nbt_tag(tag: simdnbt::borrow::NbtTag) -> Option<Self> {
105        tag.string().and_then(|s| s.to_str().parse().ok())
106    }
107}
108
109impl ToNbtTag for ResourceLocation {
110    fn to_nbt_tag(self) -> NbtTag {
111        NbtTag::String(self.to_string().into())
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn basic_resource_location() {
121        let r = ResourceLocation::new("abcdef:ghijkl");
122        assert_eq!(r.namespace, "abcdef");
123        assert_eq!(r.path, "ghijkl");
124    }
125    #[test]
126    fn no_namespace() {
127        let r = ResourceLocation::new("azalea");
128        assert_eq!(r.namespace, "minecraft");
129        assert_eq!(r.path, "azalea");
130    }
131    #[test]
132    fn colon_start() {
133        let r = ResourceLocation::new(":azalea");
134        assert_eq!(r.namespace, "minecraft");
135        assert_eq!(r.path, "azalea");
136    }
137    #[test]
138    fn colon_end() {
139        let r = ResourceLocation::new("azalea:");
140        assert_eq!(r.namespace, "azalea");
141        assert_eq!(r.path, "");
142    }
143
144    #[test]
145    fn azbuf_resource_location() {
146        let mut buf = Vec::new();
147        ResourceLocation::new("minecraft:dirt")
148            .azalea_write(&mut buf)
149            .unwrap();
150
151        let mut buf = Cursor::new(&buf[..]);
152
153        assert_eq!(
154            ResourceLocation::azalea_read(&mut buf).unwrap(),
155            ResourceLocation::new("minecraft:dirt")
156        );
157    }
158}