diff options
| author | mat <git@matdoes.dev> | 2026-01-19 16:48:49 -1100 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2026-01-19 16:48:49 -1100 |
| commit | b7ad0e60f7b0a5f5a1f6a2c80abc865fdb250ee5 (patch) | |
| tree | 683c9ea01e0be320f76cd817fbf9da4f9c5d76d8 | |
| parent | 7c1be0aa47cfee6ffcb114860ccb715dbad109a5 (diff) | |
| download | azalea-drasl-b7ad0e60f7b0a5f5a1f6a2c80abc865fdb250ee5.tar.xz | |
deserialize ClickEvent from nbt
| -rw-r--r-- | Cargo.lock | 30 | ||||
| -rw-r--r-- | azalea-chat/src/click_event.rs | 138 | ||||
| -rw-r--r-- | azalea-chat/src/lib.rs | 12 | ||||
| -rw-r--r-- | azalea-chat/src/style.rs | 24 |
4 files changed, 126 insertions, 78 deletions
@@ -1301,9 +1301,9 @@ dependencies = [ [[package]] name = "crypto-common" -version = "0.2.0-rc.9" +version = "0.2.0-rc.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41b8986f836d4aeb30ccf4c9d3bd562fd716074cfd7fc4a2948359fbd21ed809" +checksum = "0f4fc0003068acd7e9cb6659fd956dc4d671f102a06cc115990b9e7bb5745c25" dependencies = [ "hybrid-array", ] @@ -1391,13 +1391,13 @@ dependencies = [ [[package]] name = "digest" -version = "0.11.0-rc.5" +version = "0.11.0-rc.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebf9423bafb058e4142194330c52273c343f8a5beb7176d052f0e73b17dd35b9" +checksum = "2778ee7344f47967d6701053913962accf9bfdb0caa4b6d921b7c4a615f658d0" dependencies = [ "block-buffer 0.11.0", "const-oid", - "crypto-common 0.2.0-rc.9", + "crypto-common 0.2.0-rc.10", ] [[package]] @@ -2600,9 +2600,9 @@ dependencies = [ [[package]] name = "pkcs8" -version = "0.11.0-rc.8" +version = "0.11.0-rc.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77089aec8290d0b7bb01b671b091095cf1937670725af4fd73d47249f03b12c0" +checksum = "80f8fa6196ede5a9f9ee95b44ca134bddc9b70e8913f9297bd58c909f5889a09" dependencies = [ "der", "spki", @@ -2956,7 +2956,7 @@ dependencies = [ "const-oid", "crypto-bigint", "crypto-primes", - "digest 0.11.0-rc.5", + "digest 0.11.0-rc.6", "pkcs1", "pkcs8", "rand_core 0.10.0-rc-3", @@ -3211,7 +3211,7 @@ checksum = "aa1ae819b9870cadc959a052363de870944a1646932d274a4e270f64bf79e5ef" dependencies = [ "cfg-if", "cpufeatures", - "digest 0.11.0-rc.5", + "digest 0.11.0-rc.6", ] [[package]] @@ -3222,7 +3222,7 @@ checksum = "19d43dc0354d88b791216bb5c1bfbb60c0814460cc653ae0ebd71f286d0bd927" dependencies = [ "cfg-if", "cpufeatures", - "digest 0.11.0-rc.5", + "digest 0.11.0-rc.6", ] [[package]] @@ -3256,7 +3256,7 @@ version = "3.0.0-rc.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a0251c9d6468f4ba853b6352b190fb7c1e405087779917c238445eb03993826" dependencies = [ - "digest 0.11.0-rc.5", + "digest 0.11.0-rc.6", "rand_core 0.10.0-rc-3", ] @@ -3279,7 +3279,7 @@ dependencies = [ [[package]] name = "simdnbt" version = "0.9.0" -source = "git+https://github.com/azalea-rs/simdnbt#3bdc57cd04f291fbd557d9f6f3481bea75894c53" +source = "git+https://github.com/azalea-rs/simdnbt#30ef78fa8c03cb13d7d131af5cbd13cae974a466" dependencies = [ "byteorder", "flate2", @@ -3292,7 +3292,7 @@ dependencies = [ [[package]] name = "simdnbt-derive" version = "0.9.0" -source = "git+https://github.com/azalea-rs/simdnbt#3bdc57cd04f291fbd557d9f6f3481bea75894c53" +source = "git+https://github.com/azalea-rs/simdnbt#30ef78fa8c03cb13d7d131af5cbd13cae974a466" dependencies = [ "proc-macro2", "quote", @@ -4460,6 +4460,6 @@ checksum = "40990edd51aae2c2b6907af74ffb635029d5788228222c4bb811e9351c0caad3" [[package]] name = "zmij" -version = "1.0.15" +version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94f63c051f4fe3c1509da62131a678643c5b6fbdc9273b2b79d4378ebda003d2" +checksum = "dfcd145825aace48cff44a8844de64bf75feec3080e0aa5cdbde72961ae51a65" diff --git a/azalea-chat/src/click_event.rs b/azalea-chat/src/click_event.rs index a61e2561..01b196ff 100644 --- a/azalea-chat/src/click_event.rs +++ b/azalea-chat/src/click_event.rs @@ -1,78 +1,102 @@ use serde::Serialize; #[cfg(feature = "simdnbt")] -use simdnbt::owned::{Nbt, NbtCompound, NbtTag}; +use simdnbt::{ + DeserializeError, + owned::{Nbt, NbtCompound}, +}; -#[derive(Clone, Debug, PartialEq, Serialize)] -#[serde(rename_all = "snake_case", tag = "action")] -pub enum ClickEvent { - OpenUrl { +#[cfg(feature = "simdnbt")] +use crate::get_in_compound; + +macro_rules! define_click_event_struct { + ( + $( + $action_name:ident : $action_variant:ident { + $( + $(#[$meta:meta])* $field:ident : $type:ty + ),* + $(,)? + } + ),* + $(,)? + ) => { + #[derive(Clone, Debug, PartialEq, Serialize)] + #[serde(rename_all = "snake_case", tag = "action")] + pub enum ClickEvent { + $( + $action_variant { + $( + $(#[$meta])* + $field: $type + ),* + } + ),* + } + + #[cfg(feature = "simdnbt")] + impl simdnbt::Serialize for ClickEvent { + fn to_compound(self) -> NbtCompound { + let mut compound = NbtCompound::new(); + match self { + $( + Self::$action_variant { $($field),* } => { + compound.insert("action", stringify!($action_name)); + $( + compound.insert(stringify!($field), $field); + )* + } + )* + }; + compound + } + } + + #[cfg(feature = "simdnbt")] + impl simdnbt::Deserialize for ClickEvent { + fn from_compound( + compound: simdnbt::borrow::NbtCompound, + ) -> Result<Self, simdnbt::DeserializeError> { + let action = get_in_compound::<String>(&compound, "action")?; + Ok(match action.as_str() { + $( + stringify!($action_name) => Self::$action_variant { + $( + $field: get_in_compound(&compound, stringify!($field))? + ),* + }, + )* + _ => return Err(DeserializeError::MismatchedFieldType(action.to_owned())), + }) + } + } + + } +} + +define_click_event_struct! { + open_url: OpenUrl { url: String, }, - OpenFile { + open_file: OpenFile { path: String, }, - RunCommand { + run_command: RunCommand { command: String, }, - SuggestCommand { + suggest_command: SuggestCommand { command: String, }, // TODO: this uses Dialog.CODEC - ShowDialog, - ChangePage { + show_dialog: ShowDialog {}, + change_page: ChangePage { page: i32, }, - CopyToClipboard { + copy_to_clipboard: CopyToClipboard { value: String, }, - Custom { + custom: Custom { id: String, #[cfg(feature = "simdnbt")] payload: Nbt, }, } - -#[cfg(feature = "simdnbt")] -impl simdnbt::Serialize for ClickEvent { - fn to_compound(self) -> NbtCompound { - let mut compound = NbtCompound::new(); - let mut action = |s: &str| { - compound.insert("action", s); - }; - match self { - ClickEvent::OpenUrl { url } => { - action("open_url"); - compound.insert("url", url); - } - ClickEvent::OpenFile { path } => { - action("open_file"); - compound.insert("path", path); - } - ClickEvent::RunCommand { command } => { - action("run_command"); - compound.insert("command", command); - } - ClickEvent::SuggestCommand { command } => { - action("suggest_command"); - compound.insert("command", command); - } - ClickEvent::ShowDialog => { - action("show_dialog"); - } - ClickEvent::ChangePage { page } => { - action("change_page"); - compound.insert("page", NbtTag::Int(page)); - } - ClickEvent::CopyToClipboard { value } => { - action("copy_to_clipboard"); - compound.insert("value", value); - } - ClickEvent::Custom { id, payload } => { - action("custom"); - compound.insert("id", id); - compound.insert("payload", (**payload).clone()); - } - } - compound - } -} diff --git a/azalea-chat/src/lib.rs b/azalea-chat/src/lib.rs index 4e0c38bf..fef2697e 100644 --- a/azalea-chat/src/lib.rs +++ b/azalea-chat/src/lib.rs @@ -11,3 +11,15 @@ pub mod text_component; pub mod translatable_component; pub use component::{DEFAULT_STYLE, FormattedText}; +#[cfg(feature = "simdnbt")] +use simdnbt::{DeserializeError, FromNbtTag}; + +// TODO: remove this after simdnbt refactor +#[cfg(feature = "simdnbt")] +pub(crate) fn get_in_compound<T: FromNbtTag>( + compound: &simdnbt::borrow::NbtCompound, + key: &str, +) -> Result<T, DeserializeError> { + let value = compound.get(key).ok_or(DeserializeError::MissingField)?; + T::from_nbt_tag(value).ok_or(DeserializeError::MissingField) +} diff --git a/azalea-chat/src/style.rs b/azalea-chat/src/style.rs index 245e95c1..59518eb6 100644 --- a/azalea-chat/src/style.rs +++ b/azalea-chat/src/style.rs @@ -585,22 +585,34 @@ impl simdnbt::Deserialize for Style { fn from_compound( compound: simdnbt::borrow::NbtCompound, ) -> Result<Self, simdnbt::DeserializeError> { - let bold = compound.byte("bold").map(|v| v != 0); - let italic = compound.byte("italic").map(|v| v != 0); - let underlined = compound.byte("underlined").map(|v| v != 0); - let strikethrough = compound.byte("strikethrough").map(|v| v != 0); - let obfuscated = compound.byte("obfuscated").map(|v| v != 0); + use crate::get_in_compound; + let color: Option<TextColor> = compound .string("color") .and_then(|v| TextColor::parse(&v.to_str())); + let shadow_color = get_in_compound(&compound, "shadow_color").ok(); + let bold = get_in_compound(&compound, "bold").ok(); + let italic = get_in_compound(&compound, "italic").ok(); + let underlined = get_in_compound(&compound, "underlined").ok(); + let strikethrough = get_in_compound(&compound, "strikethrough").ok(); + let obfuscated = get_in_compound(&compound, "obfuscated").ok(); + let click_event = get_in_compound(&compound, "click_event").ok(); + // TODO + // let hover_event = get_in_compound(&compound, "hover_event")?; + let insertion = get_in_compound(&compound, "insertion").ok(); + let font = get_in_compound(&compound, "font").ok(); Ok(Style { color, + shadow_color, bold, italic, underlined, strikethrough, obfuscated, - ..Style::default() + click_event, + hover_event: None, + insertion, + font, }) } } |
