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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use serde::{Deserialize, Serialize};
use crate::color::{Color, DefaultColor};
use crate::identifier::Identifier;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case", tag = "action", content = "value")]
pub enum ClickEvent {
OpenUrl(String),
RunCommand(String),
SuggestCommand(String),
ChangePage(usize),
CopyToClipboard(String),
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "snake_case", tag = "action", content = "value")]
pub enum HoverEvent {
ShowText(Box<ComponentType>),
ShowItem(String),
ShowEntity(String),
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum ComponentType {
Text(TextComponent),
Translation(TranslationComponent),
KeyBind(KeyBindComponent),
Score(ScoreComponent),
Selector(SelectorComponent),
Base(BaseComponent),
}
pub trait Component {
fn get_base(&self) -> &BaseComponent;
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct BaseComponent {
#[serde(skip_serializing_if = "Option::is_none")]
pub bold: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub italic: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub underlined: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub strikethrough: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub obfuscated: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub font: Option<Identifier>,
#[serde(skip_serializing_if = "Option::is_none")]
pub color: Option<Color>,
#[serde(skip_serializing_if = "Option::is_none")]
pub insertion: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub click_event: Option<ClickEvent>,
#[serde(skip_serializing_if = "Option::is_none")]
pub hover_event: Option<HoverEvent>,
#[serde(skip_serializing_if = "Vec::is_empty", default = "Vec::new")]
pub extra: Vec<ComponentType>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct TextComponent {
pub text: String,
#[serde(flatten)]
pub base: BaseComponent,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct TranslationComponent {
pub translate: String,
#[serde(skip_serializing_if = "Vec::is_empty", default = "Vec::new")]
pub with: Vec<ComponentType>,
#[serde(flatten)]
pub base: BaseComponent,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct KeyBindComponent {
pub keybind: String,
#[serde(flatten)]
pub base: BaseComponent,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ScoreComponent {
pub score: Score,
#[serde(flatten)]
pub base: BaseComponent,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Score {
pub name: String,
pub objective: String,
pub value: String,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct SelectorComponent {
pub selector: String,
#[serde(flatten)]
pub base: BaseComponent,
}
macro_rules! component {
($name: ident) => {
impl Component for $name {
fn get_base(&self) -> &BaseComponent {
&self.base
}
}
impl $name {
pub fn reset(&mut self) {
self.base.bold = Some(false);
self.base.italic = Some(false);
self.base.underlined = Some(false);
self.base.strikethrough = Some(false);
self.base.obfuscated = Some(false);
self.base.color = Some(Color::Default(DefaultColor::White));
}
}
};
($($name: ident)*) => {
$(component!($name);)*
}
}
impl Component for BaseComponent {
fn get_base(&self) -> &BaseComponent {
self
}
}
component!(TextComponent TranslationComponent KeyBindComponent ScoreComponent SelectorComponent);
impl TextComponent {
pub fn new(text: String) -> Self {
TextComponent {
text,
base: BaseComponent::default(),
}
}
}
impl TranslationComponent {
pub fn new(translate: String) -> Self {
TranslationComponent {
translate,
with: Vec::new(),
base: BaseComponent::default(),
}
}
}
impl KeyBindComponent {
pub fn new(keybind: String) -> Self {
KeyBindComponent {
keybind,
base: BaseComponent::default(),
}
}
}
impl ScoreComponent {
pub fn new(score: Score) -> Self {
ScoreComponent {
score,
base: BaseComponent::default(),
}
}
}
impl SelectorComponent {
pub fn new(selector: String) -> Self {
SelectorComponent {
selector,
base: BaseComponent::default(),
}
}
}
impl From<TextComponent> for ComponentType {
fn from(component: TextComponent) -> Self {
ComponentType::Text(component)
}
}
impl From<TranslationComponent> for ComponentType {
fn from(component: TranslationComponent) -> Self {
ComponentType::Translation(component)
}
}
impl From<KeyBindComponent> for ComponentType {
fn from(component: KeyBindComponent) -> Self {
ComponentType::KeyBind(component)
}
}
impl From<ScoreComponent> for ComponentType {
fn from(component: ScoreComponent) -> Self {
ComponentType::Score(component)
}
}
impl From<SelectorComponent> for ComponentType {
fn from(component: SelectorComponent) -> Self {
ComponentType::Selector(component)
}
}
#[cfg(test)]
mod tests {
use crate::color::HexColor;
use super::*;
#[test]
pub fn color_ser_test() {
assert_eq!(serde_json::ser::to_string(&Color::Default(DefaultColor::Black)).unwrap(), "\"black\"");
assert_eq!(serde_json::ser::to_string(&Color::Default(DefaultColor::Aqua)).unwrap(), "\"aqua\"");
assert_eq!(serde_json::ser::to_string(&Color::Default(DefaultColor::LightPurple)).unwrap(), "\"light_purple\"");
}
#[test]
pub fn color_de_test() {
assert_eq!(serde_json::de::from_str::<'_, DefaultColor>("\"black\"").unwrap(), DefaultColor::Black);
assert_eq!(serde_json::de::from_str::<'_, DefaultColor>("\"light_purple\"").unwrap(), DefaultColor::LightPurple);
}
#[test]
pub fn click_event_ser_test() {
assert_eq!(
serde_json::ser::to_string(&ClickEvent::OpenUrl("http://google.com".into())).unwrap(),
"{\"action\":\"open_url\",\"value\":\"http://google.com\"}"
);
assert_eq!(
serde_json::ser::to_string(&ClickEvent::ChangePage(100)).unwrap(),
"{\"action\":\"change_page\",\"value\":100}"
)
}
#[test]
pub fn component_ser_test() {
let mut component = TextComponent::new("hello".into());
component.base.bold = Some(true);
component.base.color = Some(Color::Default(DefaultColor::Aqua));
assert_eq!(
serde_json::ser::to_string(&ComponentType::Text(component.clone())).unwrap(),
"{\"text\":\"hello\",\"bold\":true,\"color\":\"aqua\"}"
);
component.base.color = Some(Color::Hex(HexColor::try_from("#ffffff".to_string()).unwrap()));
assert_eq!(
serde_json::ser::to_string(&ComponentType::Text(component.clone())).unwrap(),
"{\"text\":\"hello\",\"bold\":true,\"color\":\"#ffffff\"}"
);
}
#[test]
pub fn component_de_test() {
let json_component: ComponentType = serde_json::de::from_str(
"{\"text\":\"hi\",\"color\":\"red\",\"bold\":true,\"extra\":[{\"text\":\"bye\",\"color\":\"white\",\"bold\":false}]}"
).unwrap();
let mut component = TextComponent::new("hi".into());
component.base.bold = Some(true);
component.base.color = Some(DefaultColor::Red.into());
component.base.extra = vec![
{
let mut component = TextComponent::new("bye".into());
component.base.color = Some(DefaultColor::White.into());
component.base.bold = Some(false);
component.into()
}
];
assert_eq!(json_component, component.into());
}
}