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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
use serde::{Serialize, ser::SerializeMap};
use crate::{FormattedText, style::Style};
#[derive(Clone, Debug, PartialEq)]
pub struct BaseComponent {
/// Components in the "extra" field.
pub siblings: Vec<FormattedText>,
pub style: Box<Style>,
}
impl BaseComponent {
pub fn serialize_map<S>(&self, state: &mut S::SerializeMap) -> Result<(), S::Error>
where
S: serde::Serializer,
{
if !self.siblings.is_empty() {
state.serialize_entry("extra", &self.siblings)?;
}
self.style.serialize_map::<S>(state)?;
Ok(())
}
}
impl Serialize for BaseComponent {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_map(None)?;
self.serialize_map::<S>(&mut state)?;
state.end()
}
}
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()
}
}
|