azalea_chat/
base_component.rs

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