diff options
Diffstat (limited to 'azalea-core/src')
| -rwxr-xr-x | azalea-core/src/resource_location.rs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/azalea-core/src/resource_location.rs b/azalea-core/src/resource_location.rs index 4e25d00e..09a35efd 100755 --- a/azalea-core/src/resource_location.rs +++ b/azalea-core/src/resource_location.rs @@ -3,7 +3,10 @@ use azalea_buf::{BufReadError, McBufReadable, McBufWritable}; use std::io::{Cursor, Write}; -// TODO: make a `resourcelocation!("minecraft:overwolrd")` macro that checks if +#[cfg(feature = "serde")] +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +// TODO: make a `resourcelocation!("minecraft:overworld")` macro that checks if // it's correct at compile-time. #[derive(Hash, Clone, PartialEq, Eq)] @@ -60,6 +63,37 @@ impl McBufWritable for ResourceLocation { } } +#[cfg(feature = "serde")] +impl Serialize for ResourceLocation { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_str(&self.to_string()) + } +} + +#[cfg(feature = "serde")] +impl<'de> Deserialize<'de> for ResourceLocation { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + if s.contains(':') { + match ResourceLocation::new(&s) { + Ok(r) => Ok(r), + Err(e) => Err(de::Error::custom(e)), + } + } else { + Err(de::Error::invalid_value( + de::Unexpected::Str(&s), + &"a valid ResourceLocation", + )) + } + } +} + #[cfg(test)] mod tests { use super::*; |
