azalea_chat/
text_component.rs1use std::fmt::{self, Display};
2
3use serde::{Serialize, Serializer, ser::SerializeMap};
4
5use crate::{
6 FormattedText,
7 base_component::BaseComponent,
8 style::{ChatFormatting, Style, TextColor},
9};
10
11#[derive(Clone, Debug, Default, PartialEq)]
13pub struct TextComponent {
14 pub base: BaseComponent,
15 pub text: String,
16}
17impl Serialize for TextComponent {
18 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
19 where
20 S: Serializer,
21 {
22 if self.base == BaseComponent::default() {
23 return serializer.serialize_str(&self.text);
24 }
25
26 let mut state = serializer.serialize_map(None)?;
27 state.serialize_entry("text", &self.text)?;
28
29 self.base.serialize_map::<S>(&mut state)?;
30
31 state.end()
32 }
33}
34
35#[cfg(feature = "simdnbt")]
36impl simdnbt::Serialize for TextComponent {
37 fn to_compound(self) -> simdnbt::owned::NbtCompound {
38 let mut compound = simdnbt::owned::NbtCompound::new();
39 compound.insert("text", self.text);
40 compound.extend(self.base.style.to_compound());
41 if !self.base.siblings.is_empty() {
42 compound.insert(
43 "extra",
44 simdnbt::owned::NbtList::from(
45 self.base
46 .siblings
47 .into_iter()
48 .map(|component| component.to_compound())
49 .collect::<Vec<_>>(),
50 ),
51 );
52 }
53 compound
54 }
55}
56
57pub const LEGACY_FORMATTING_CODE_SYMBOL: char = '§';
58
59pub fn legacy_color_code_to_text_component(legacy_color_code: &str) -> TextComponent {
63 if legacy_color_code.is_empty() {
64 return TextComponent::new("");
65 }
66
67 let mut components: Vec<TextComponent> = Vec::with_capacity(1);
68 let mut cur_component = TextComponent::new("");
73
74 let mut i = 0;
77 while i < legacy_color_code.chars().count() {
78 if legacy_color_code.chars().nth(i).unwrap() == LEGACY_FORMATTING_CODE_SYMBOL {
79 let formatting_code = legacy_color_code.chars().nth(i + 1);
80 let Some(formatting_code) = formatting_code else {
81 i += 1;
82 continue;
83 };
84 if formatting_code == '#' {
85 let color = legacy_color_code
86 .chars()
87 .skip(i + 1)
88 .take(7)
90 .collect::<String>();
91
92 if !cur_component.text.is_empty() {
93 components.push(cur_component.clone());
95 cur_component.text = "".to_owned();
96 };
97 cur_component.base.style.color = TextColor::parse(&color);
98
99 i += 6;
100 } else if let Some(formatter) = ChatFormatting::from_code(formatting_code) {
101 if !cur_component.text.is_empty() || formatter == ChatFormatting::Reset {
102 components.push(cur_component.clone());
104 cur_component.text = "".to_owned();
105 };
106 cur_component.base.style.apply_formatting(&formatter);
107 }
108 i += 1;
109 } else {
110 cur_component
111 .text
112 .push(legacy_color_code.chars().nth(i).unwrap());
113 };
114 i += 1;
115 }
116
117 components.push(cur_component);
118
119 let mut final_component = TextComponent::new("");
121 for component in components {
122 final_component.base.siblings.push(component.get());
123 }
124
125 final_component
126}
127
128impl TextComponent {
129 pub fn new(text: impl Into<String>) -> Self {
130 let text = text.into();
131 if text.contains(LEGACY_FORMATTING_CODE_SYMBOL) {
133 legacy_color_code_to_text_component(&text)
134 } else {
135 Self {
136 base: BaseComponent::new(),
137 text,
138 }
139 }
140 }
141
142 fn get(self) -> FormattedText {
143 FormattedText::Text(self)
144 }
145 pub fn with_style(mut self, style: Style) -> Self {
146 *self.base.style = style;
147 self
148 }
149}
150
151impl Display for TextComponent {
152 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153 for component in FormattedText::Text(self.clone()).into_iter() {
155 let component_text = match &component {
156 FormattedText::Text(c) => c.text.to_string(),
157 FormattedText::Translatable(c) => c.read()?.to_string(),
158 };
159
160 f.write_str(&component_text)?;
161 }
162
163 Ok(())
164 }
165}
166
167impl From<TextComponent> for FormattedText {
168 fn from(c: TextComponent) -> Self {
169 FormattedText::Text(c)
170 }
171}
172
173#[cfg(test)]
174mod tests {
175 use super::*;
176 use crate::style::Ansi;
177
178 #[test]
179 fn test_hypixel_motd_ansi() {
180 let component =
181 TextComponent::new("§aHypixel Network §c[1.8-1.18]\n§b§lHAPPY HOLIDAYS".to_owned())
182 .get();
183 assert_eq!(
184 component.to_ansi(),
185 format!(
186 "{GREEN}Hypixel Network {RED}[1.8-1.18]\n{BOLD}{AQUA}HAPPY HOLIDAYS{RESET}",
187 GREEN = Ansi::rgb(ChatFormatting::Green.color().unwrap()),
188 RED = Ansi::rgb(ChatFormatting::Red.color().unwrap()),
189 AQUA = Ansi::rgb(ChatFormatting::Aqua.color().unwrap()),
190 BOLD = Ansi::BOLD,
191 RESET = Ansi::RESET
192 )
193 );
194 }
195
196 #[test]
197 fn test_hypixel_motd_html() {
198 let component =
199 TextComponent::new("§aHypixel Network §c[1.8-1.18]\n§b§lHAPPY HOLIDAYS".to_owned())
200 .get();
201
202 assert_eq!(
203 component.to_html(),
204 format!(
205 "{GREEN}Hypixel Network {END_SPAN}{RED}[1.8-1.18]<br>{END_SPAN}{BOLD_AQUA}HAPPY HOLIDAYS{END_SPAN}",
206 END_SPAN = "</span>",
207 GREEN = "<span style=\"color:#55FF55;\">",
208 RED = "<span style=\"color:#FF5555;\">",
209 BOLD_AQUA = "<span style=\"color:#55FFFF;font-weight:bold;\">",
210 )
211 );
212 }
213
214 #[test]
215 fn test_xss_html() {
216 let component = TextComponent::new("§a<b>&\n§b</b>".to_owned()).get();
217
218 assert_eq!(
219 component.to_html(),
220 format!(
221 "{GREEN}<b>&<br>{END_SPAN}{AQUA}</b>{END_SPAN}",
222 END_SPAN = "</span>",
223 GREEN = "<span style=\"color:#55FF55;\">",
224 AQUA = "<span style=\"color:#55FFFF;\">",
225 )
226 );
227 }
228
229 #[test]
230 fn test_legacy_color_code_to_component() {
231 let component = TextComponent::new("§lHello §r§1w§2o§3r§4l§5d".to_owned()).get();
232 assert_eq!(
233 component.to_ansi(),
234 format!(
235 "{BOLD}{WHITE}Hello {RESET}{DARK_BLUE}w{DARK_GREEN}o{DARK_AQUA}r{DARK_RED}l{DARK_PURPLE}d{RESET}",
236 BOLD = Ansi::BOLD,
237 WHITE = Ansi::rgb(ChatFormatting::White.color().unwrap()),
238 RESET = Ansi::RESET,
239 DARK_BLUE = Ansi::rgb(ChatFormatting::DarkBlue.color().unwrap()),
240 DARK_GREEN = Ansi::rgb(ChatFormatting::DarkGreen.color().unwrap()),
241 DARK_AQUA = Ansi::rgb(ChatFormatting::DarkAqua.color().unwrap()),
242 DARK_RED = Ansi::rgb(ChatFormatting::DarkRed.color().unwrap()),
243 DARK_PURPLE = Ansi::rgb(ChatFormatting::DarkPurple.color().unwrap())
244 )
245 );
246 }
247
248 #[test]
249 fn test_legacy_color_code_with_rgb() {
250 let component = TextComponent::new("§#Ff0000This is a test message".to_owned()).get();
251 assert_eq!(
252 component.to_ansi(),
253 format!(
254 "{RED}This is a test message{RESET}",
255 RED = Ansi::rgb(0xff0000),
256 RESET = Ansi::RESET
257 )
258 );
259 }
260
261 #[test]
262 fn test_serialize_to_json() {
263 let component = TextComponent::new("Hello §aworld".to_owned()).get();
264 assert_eq!(
265 serde_json::to_string(&component).unwrap(),
266 "{\"text\":\"\",\"extra\":[\"Hello \",{\"text\":\"world\",\"color\":\"#55FF55\"}]}"
267 );
268 }
269}