aboutsummaryrefslogtreecommitdiff
path: root/azalea-chat/src/base_component.rs
blob: 366904c78c331e3e1cb04de5215fc2fcafb6ba5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use serde::Serialize;

use crate::{FormattedText, style::Style};

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct BaseComponent {
    // implements mutablecomponent
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub siblings: Vec<FormattedText>,
    #[serde(flatten)]
    pub style: Box<Style>,
}

impl BaseComponent {
    pub fn new() -> Self {
        Self {
            siblings: Vec::new(),
            style: Default::default(),
        }
    }
    pub fn with_style(self, style: Style) -> Self {
        Self {
            style: Box::new(style),
            ..self
        }
    }
}

#[cfg(feature = "simdnbt")]
impl simdnbt::Serialize for BaseComponent {
    fn to_compound(self) -> simdnbt::owned::NbtCompound {
        let mut compound = simdnbt::owned::NbtCompound::new();
        if !self.siblings.is_empty() {
            compound.insert(
                "extra",
                simdnbt::owned::NbtList::from(
                    self.siblings
                        .into_iter()
                        .map(|component| component.to_compound())
                        .collect::<Vec<_>>(),
                ),
            );
        }
        compound.extend(self.style.to_compound());
        compound
    }
}

impl Default for BaseComponent {
    fn default() -> Self {
        Self::new()
    }
}