diff options
| author | EightFactorial <murphkev000@gmail.com> | 2022-12-03 17:08:05 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-03 19:08:05 -0600 |
| commit | 3b93fc641250c4d01ab7f0764b7d5faec2f8ae5b (patch) | |
| tree | 727b94c9f90bba59f8571ecebeab1a33e0f4af07 /azalea-chat/src/translatable_component.rs | |
| parent | 661c3622bebc111a1523bc80792dc90d9d571b24 (diff) | |
| download | azalea-drasl-3b93fc641250c4d01ab7f0764b7d5faec2f8ae5b.tar.xz | |
Serialize Component (#47)
* Serializing ClientboundStatusResponsePacket
Enable serialization of ClientboundStatusResponsePacket
* Update clientbound_status_response_packet.rs
Add options previewsChat and enforcesSecureChat
* Serialize Style and TextColor
* Serialize BaseComponent
* Serialize TextComponent
* Fix Style
* Serialize Component
* Fix multiple formats per message, fix reset tag
* Fix Style, again
* Use FlatMapSerializer
* Forgot italics
* Count struct fields, reorganize logic
* Serialize TranslatableComponent
* Rewrite TextComponent Serializer
* Fix using TextColor::Parse
* Code Cleanup
* Add default attribute, just in case
* Clippy
* use serde derive feature + preferred formatting choices
Co-authored-by: BuildTools <unconfigured@null.spigotmc.org>
Co-authored-by: mat <github@matdoes.dev>
Diffstat (limited to 'azalea-chat/src/translatable_component.rs')
| -rwxr-xr-x | azalea-chat/src/translatable_component.rs | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/azalea-chat/src/translatable_component.rs b/azalea-chat/src/translatable_component.rs index 28725c44..7819d5ff 100755 --- a/azalea-chat/src/translatable_component.rs +++ b/azalea-chat/src/translatable_component.rs @@ -3,8 +3,10 @@ use std::fmt::{self, Display, Formatter}; use crate::{ base_component::BaseComponent, style::Style, text_component::TextComponent, Component, }; +use serde::{ser::SerializeMap, Serialize, Serializer, __private::ser::FlatMapSerializer}; -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize)] +#[serde(untagged)] pub enum StringOrComponent { String(String), Component(Component), @@ -18,6 +20,19 @@ pub struct TranslatableComponent { pub args: Vec<StringOrComponent>, } +impl Serialize for TranslatableComponent { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + let mut state = serializer.serialize_map(None)?; + state.serialize_entry("translate", &self.key)?; + Serialize::serialize(&self.base, FlatMapSerializer(&mut state))?; + state.serialize_entry("with", &self.args)?; + state.end() + } +} + impl TranslatableComponent { pub fn new(key: String, args: Vec<StringOrComponent>) -> Self { Self { |
