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/text_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/text_component.rs')
| -rwxr-xr-x | azalea-chat/src/text_component.rs | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/azalea-chat/src/text_component.rs b/azalea-chat/src/text_component.rs index 0d88ca05..6715c93e 100755 --- a/azalea-chat/src/text_component.rs +++ b/azalea-chat/src/text_component.rs @@ -1,6 +1,6 @@ -use std::fmt::Display; - use crate::{base_component::BaseComponent, style::ChatFormatting, Component}; +use serde::{ser::SerializeMap, Serialize, Serializer, __private::ser::FlatMapSerializer}; +use std::fmt::Display; /// A component that contains text that's the same in all locales. #[derive(Clone, Debug, Default, PartialEq)] @@ -9,6 +9,21 @@ pub struct TextComponent { pub text: String, } +impl Serialize for TextComponent { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + let mut state = serializer.serialize_map(None)?; + state.serialize_entry("text", &self.text)?; + Serialize::serialize(&self.base, FlatMapSerializer(&mut state))?; + if !self.base.siblings.is_empty() { + state.serialize_entry("extra", &self.base.siblings)?; + } + state.end() + } +} + const LEGACY_FORMATTING_CODE_SYMBOL: char = 'ยง'; /// Convert a legacy color code string into a Component |
