azalea_chat/
base_component.rs

1use serde::Serialize;
2
3use crate::{FormattedText, style::Style};
4
5#[derive(Clone, Debug, PartialEq, Serialize)]
6pub struct BaseComponent {
7    // implements mutablecomponent
8    /// Components in the "extra" field.
9    #[serde(skip_serializing_if = "Vec::is_empty")]
10    pub siblings: Vec<FormattedText>,
11    #[serde(flatten)]
12    pub style: Box<Style>,
13}
14
15impl BaseComponent {
16    pub fn new() -> Self {
17        Self {
18            siblings: Vec::new(),
19            style: Default::default(),
20        }
21    }
22    pub fn with_style(self, style: Style) -> Self {
23        Self {
24            style: Box::new(style),
25            ..self
26        }
27    }
28}
29
30#[cfg(feature = "simdnbt")]
31impl simdnbt::Serialize for BaseComponent {
32    fn to_compound(self) -> simdnbt::owned::NbtCompound {
33        let mut compound = simdnbt::owned::NbtCompound::new();
34        if !self.siblings.is_empty() {
35            compound.insert(
36                "extra",
37                simdnbt::owned::NbtList::from(
38                    self.siblings
39                        .into_iter()
40                        .map(|component| component.to_compound())
41                        .collect::<Vec<_>>(),
42                ),
43            );
44        }
45        compound.extend(self.style.to_compound());
46        compound
47    }
48}
49
50impl Default for BaseComponent {
51    fn default() -> Self {
52        Self::new()
53    }
54}