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