diff options
| author | EightFactorial <29801334+EightFactorial@users.noreply.github.com> | 2023-03-11 14:00:10 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-11 16:00:10 -0600 |
| commit | c57c68ddf8cb9e4e8d27cf3e08f267a8a020c1c0 (patch) | |
| tree | 3209e7c0f93f81c09d48fb13cd3a240c04aaa3b8 /azalea-core | |
| parent | f28efd5637cf3e129af018066421c090cb73ad50 (diff) | |
| download | azalea-drasl-c57c68ddf8cb9e4e8d27cf3e08f267a8a020c1c0.tar.xz | |
Add RegistryHolder struct and serde features (#81)
* Make RegistryHolder struct
* Update deps
* Move RegistryHolder to azalea-protocol
* Convert bytes to bools and back
* Rename and shuffle logic
* Move logic into trait, rename methods
* Final touchups
* Ah, merge mistakes
* Add serde support for ResourceLocation
* Reuse structs
* Error when serde skips values in debug mode
Add missing attributes
* Strict_registry feature, require packet feature
* Add test
* Move into packets
* Docs and touchups
* Reword docs
* Move into module inside ClientboundLoginPacket
* Add azalea-nbt serde feature
* remove duplicate comment and type_ -> kind
---------
Co-authored-by: mat <github@matdoes.dev>
Diffstat (limited to 'azalea-core')
| -rw-r--r-- | azalea-core/Cargo.toml | 2 | ||||
| -rwxr-xr-x | azalea-core/src/resource_location.rs | 36 |
2 files changed, 37 insertions, 1 deletions
diff --git a/azalea-core/Cargo.toml b/azalea-core/Cargo.toml index e31351e8..8b92e845 100644 --- a/azalea-core/Cargo.toml +++ b/azalea-core/Cargo.toml @@ -13,7 +13,9 @@ azalea-buf = { path = "../azalea-buf", version = "^0.6.0" } azalea-chat = { path = "../azalea-chat", version = "^0.6.0" } azalea-nbt = { path = "../azalea-nbt", version = "^0.6.0" } bevy_ecs = { version = "0.10.0", default-features = false, optional = true } +serde = {version = "^1.0.152", optional = true} uuid = "^1.1.2" [features] bevy_ecs = ["dep:bevy_ecs"] +serde = ["dep:serde"]
\ No newline at end of file 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::*; |
