azalea_chat/
base_component.rs1use serde::{Serialize, ser::SerializeMap};
2
3use crate::{FormattedText, style::Style};
4
5#[derive(Clone, Debug, PartialEq)]
6pub struct BaseComponent {
7 pub siblings: Vec<FormattedText>,
9 pub style: Box<Style>,
10}
11
12impl BaseComponent {
13 pub fn serialize_map<S>(&self, state: &mut S::SerializeMap) -> Result<(), S::Error>
14 where
15 S: serde::Serializer,
16 {
17 if !self.siblings.is_empty() {
18 state.serialize_entry("extra", &self.siblings)?;
19 }
20 self.style.serialize_map::<S>(state)?;
21 Ok(())
22 }
23}
24impl Serialize for BaseComponent {
25 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
26 where
27 S: serde::Serializer,
28 {
29 let mut state = serializer.serialize_map(None)?;
30 self.serialize_map::<S>(&mut state)?;
31 state.end()
32 }
33}
34
35impl BaseComponent {
36 pub fn new() -> Self {
37 Self {
38 siblings: Vec::new(),
39 style: Default::default(),
40 }
41 }
42 pub fn with_style(self, style: Style) -> Self {
43 Self {
44 style: Box::new(style),
45 ..self
46 }
47 }
48}
49
50#[cfg(feature = "simdnbt")]
51impl simdnbt::Serialize for BaseComponent {
52 fn to_compound(self) -> simdnbt::owned::NbtCompound {
53 let mut compound = simdnbt::owned::NbtCompound::new();
54 if !self.siblings.is_empty() {
55 compound.insert(
56 "extra",
57 simdnbt::owned::NbtList::from(
58 self.siblings
59 .into_iter()
60 .map(|component| component.to_compound())
61 .collect::<Vec<_>>(),
62 ),
63 );
64 }
65 compound.extend(self.style.to_compound());
66 compound
67 }
68}
69
70impl Default for BaseComponent {
71 fn default() -> Self {
72 Self::new()
73 }
74}