Skip to main content

azalea_chat/component/
base.rs

1use std::fmt::{self, Debug};
2
3use azalea_codec::Codec;
4
5use crate::{FormattedText, style::Style};
6
7#[derive(Clone, PartialEq, Codec)]
8#[codec(allow_unknown_keys)]
9pub struct BaseComponent {
10    /// Components in the "extra" field.
11    #[codec(optional, rename = "extra")]
12    pub siblings: Vec<FormattedText>,
13    #[codec(optional, flatten)]
14    pub style: Box<Style>,
15}
16
17impl BaseComponent {
18    pub fn new() -> Self {
19        Self {
20            siblings: Vec::new(),
21            style: Default::default(),
22        }
23    }
24    pub fn with_style(self, style: Style) -> Self {
25        Self {
26            style: Box::new(style),
27            ..self
28        }
29    }
30    /// Whether this base component has no siblings and the default style.
31    pub fn is_default(&self) -> bool {
32        self == &BaseComponent::new()
33    }
34}
35
36impl Default for BaseComponent {
37    fn default() -> Self {
38        Self::new()
39    }
40}
41impl Debug for BaseComponent {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        let mut s = f.debug_struct("BaseComponent");
44        if !self.siblings.is_empty() {
45            s.field("siblings", &self.siblings);
46        }
47        if !self.style.is_empty() {
48            s.field("style", &self.style);
49        }
50        s.finish()
51    }
52}