azalea_chat/
style.rs

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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
use std::{collections::HashMap, fmt};

#[cfg(feature = "azalea-buf")]
use azalea_buf::McBuf;
use once_cell::sync::Lazy;
use serde::{ser::SerializeStruct, Serialize, Serializer};
use serde_json::Value;
#[cfg(feature = "simdnbt")]
use simdnbt::owned::{NbtCompound, NbtTag};

#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct TextColor {
    pub value: u32,
    pub name: Option<String>,
}

impl Serialize for TextColor {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(
            &self
                .name
                .as_ref()
                .map(|n| n.to_ascii_lowercase())
                .unwrap_or_else(|| self.format()),
        )
    }
}

#[cfg(feature = "simdnbt")]
impl simdnbt::ToNbtTag for TextColor {
    fn to_nbt_tag(self) -> simdnbt::owned::NbtTag {
        self.name
            .map(|n| NbtTag::String(n.to_ascii_lowercase().into()))
            .unwrap_or_else(|| NbtTag::Int(self.value as i32))
    }
}

impl TextColor {
    pub fn parse(value: String) -> Option<TextColor> {
        if value.starts_with('#') {
            let n = value.chars().skip(1).collect::<String>();
            let n = u32::from_str_radix(&n, 16).ok()?;
            return Some(TextColor::from_rgb(n));
        }
        let color_option = NAMED_COLORS.get(&value.to_ascii_uppercase());
        if let Some(color) = color_option {
            return Some(color.clone());
        }
        None
    }

    fn from_rgb(value: u32) -> TextColor {
        TextColor { value, name: None }
    }
}

static LEGACY_FORMAT_TO_COLOR: Lazy<HashMap<&'static ChatFormatting, TextColor>> =
    Lazy::new(|| {
        let mut legacy_format_to_color = HashMap::new();
        for formatter in &ChatFormatting::FORMATTERS {
            if !formatter.is_format() && *formatter != ChatFormatting::Reset {
                legacy_format_to_color.insert(
                    formatter,
                    TextColor {
                        value: formatter.color().unwrap(),
                        name: Some(formatter.name().to_string()),
                    },
                );
            }
        }
        legacy_format_to_color
    });
static NAMED_COLORS: Lazy<HashMap<String, TextColor>> = Lazy::new(|| {
    let mut named_colors = HashMap::new();
    for color in LEGACY_FORMAT_TO_COLOR.values() {
        named_colors.insert(color.name.clone().unwrap(), color.clone());
    }
    named_colors
});

pub struct Ansi {}
impl Ansi {
    pub const BOLD: &'static str = "\u{1b}[1m";
    pub const ITALIC: &'static str = "\u{1b}[3m";
    pub const UNDERLINED: &'static str = "\u{1b}[4m";
    pub const STRIKETHROUGH: &'static str = "\u{1b}[9m";
    pub const OBFUSCATED: &'static str = "\u{1b}[8m";
    pub const RESET: &'static str = "\u{1b}[m";

    pub fn rgb(value: u32) -> String {
        format!(
            "\u{1b}[38;2;{};{};{}m",
            (value >> 16) & 0xFF,
            (value >> 8) & 0xFF,
            value & 0xFF
        )
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "azalea-buf", derive(McBuf))]
pub enum ChatFormatting {
    Black,
    DarkBlue,
    DarkGreen,
    DarkAqua,
    DarkRed,
    DarkPurple,
    Gold,
    Gray,
    DarkGray,
    Blue,
    Green,
    Aqua,
    Red,
    LightPurple,
    Yellow,
    White,
    Obfuscated,
    Strikethrough,
    Bold,
    Underline,
    Italic,
    Reset,
}

impl ChatFormatting {
    pub const FORMATTERS: [ChatFormatting; 22] = [
        ChatFormatting::Black,
        ChatFormatting::DarkBlue,
        ChatFormatting::DarkGreen,
        ChatFormatting::DarkAqua,
        ChatFormatting::DarkRed,
        ChatFormatting::DarkPurple,
        ChatFormatting::Gold,
        ChatFormatting::Gray,
        ChatFormatting::DarkGray,
        ChatFormatting::Blue,
        ChatFormatting::Green,
        ChatFormatting::Aqua,
        ChatFormatting::Red,
        ChatFormatting::LightPurple,
        ChatFormatting::Yellow,
        ChatFormatting::White,
        ChatFormatting::Obfuscated,
        ChatFormatting::Strikethrough,
        ChatFormatting::Bold,
        ChatFormatting::Underline,
        ChatFormatting::Italic,
        ChatFormatting::Reset,
    ];

    pub fn name(&self) -> &'static str {
        match self {
            ChatFormatting::Black => "BLACK",
            ChatFormatting::DarkBlue => "DARK_BLUE",
            ChatFormatting::DarkGreen => "DARK_GREEN",
            ChatFormatting::DarkAqua => "DARK_AQUA",
            ChatFormatting::DarkRed => "DARK_RED",
            ChatFormatting::DarkPurple => "DARK_PURPLE",
            ChatFormatting::Gold => "GOLD",
            ChatFormatting::Gray => "GRAY",
            ChatFormatting::DarkGray => "DARK_GRAY",
            ChatFormatting::Blue => "BLUE",
            ChatFormatting::Green => "GREEN",
            ChatFormatting::Aqua => "AQUA",
            ChatFormatting::Red => "RED",
            ChatFormatting::LightPurple => "LIGHT_PURPLE",
            ChatFormatting::Yellow => "YELLOW",
            ChatFormatting::White => "WHITE",
            ChatFormatting::Obfuscated => "OBFUSCATED",
            ChatFormatting::Strikethrough => "STRIKETHROUGH",
            ChatFormatting::Bold => "BOLD",
            ChatFormatting::Underline => "UNDERLINE",
            ChatFormatting::Italic => "ITALIC",
            ChatFormatting::Reset => "RESET",
        }
    }

    pub fn code(&self) -> char {
        match self {
            ChatFormatting::Black => '0',
            ChatFormatting::DarkBlue => '1',
            ChatFormatting::DarkGreen => '2',
            ChatFormatting::DarkAqua => '3',
            ChatFormatting::DarkRed => '4',
            ChatFormatting::DarkPurple => '5',
            ChatFormatting::Gold => '6',
            ChatFormatting::Gray => '7',
            ChatFormatting::DarkGray => '8',
            ChatFormatting::Blue => '9',
            ChatFormatting::Green => 'a',
            ChatFormatting::Aqua => 'b',
            ChatFormatting::Red => 'c',
            ChatFormatting::LightPurple => 'd',
            ChatFormatting::Yellow => 'e',
            ChatFormatting::White => 'f',
            ChatFormatting::Obfuscated => 'k',
            ChatFormatting::Strikethrough => 'm',
            ChatFormatting::Bold => 'l',
            ChatFormatting::Underline => 'n',
            ChatFormatting::Italic => 'o',
            ChatFormatting::Reset => 'r',
        }
    }

    pub fn from_code(code: char) -> Option<ChatFormatting> {
        match code {
            '0' => Some(ChatFormatting::Black),
            '1' => Some(ChatFormatting::DarkBlue),
            '2' => Some(ChatFormatting::DarkGreen),
            '3' => Some(ChatFormatting::DarkAqua),
            '4' => Some(ChatFormatting::DarkRed),
            '5' => Some(ChatFormatting::DarkPurple),
            '6' => Some(ChatFormatting::Gold),
            '7' => Some(ChatFormatting::Gray),
            '8' => Some(ChatFormatting::DarkGray),
            '9' => Some(ChatFormatting::Blue),
            'a' => Some(ChatFormatting::Green),
            'b' => Some(ChatFormatting::Aqua),
            'c' => Some(ChatFormatting::Red),
            'd' => Some(ChatFormatting::LightPurple),
            'e' => Some(ChatFormatting::Yellow),
            'f' => Some(ChatFormatting::White),
            'k' => Some(ChatFormatting::Obfuscated),
            'm' => Some(ChatFormatting::Strikethrough),
            'l' => Some(ChatFormatting::Bold),
            'n' => Some(ChatFormatting::Underline),
            'o' => Some(ChatFormatting::Italic),
            'r' => Some(ChatFormatting::Reset),
            _ => None,
        }
    }

    pub fn is_format(&self) -> bool {
        matches!(
            self,
            ChatFormatting::Obfuscated
                | ChatFormatting::Strikethrough
                | ChatFormatting::Bold
                | ChatFormatting::Underline
                | ChatFormatting::Italic
                | ChatFormatting::Reset
        )
    }

    pub fn color(&self) -> Option<u32> {
        match self {
            ChatFormatting::Black => Some(0),
            ChatFormatting::DarkBlue => Some(170),
            ChatFormatting::DarkGreen => Some(43520),
            ChatFormatting::DarkAqua => Some(43690),
            ChatFormatting::DarkRed => Some(1114112),
            ChatFormatting::DarkPurple => Some(11141290),
            ChatFormatting::Gold => Some(16755200),
            ChatFormatting::Gray => Some(11184810),
            ChatFormatting::DarkGray => Some(5592405),
            ChatFormatting::Blue => Some(5592575),
            ChatFormatting::Green => Some(5635925),
            ChatFormatting::Aqua => Some(5636095),
            ChatFormatting::Red => Some(16733525),
            ChatFormatting::LightPurple => Some(16733695),
            ChatFormatting::Yellow => Some(16777045),
            ChatFormatting::White => Some(16777215),
            _ => None,
        }
    }
}

impl TextColor {
    fn new(value: u32, name: Option<String>) -> Self {
        Self { value, name }
    }

    pub fn format(&self) -> String {
        format!("#{:06X}", self.value)
    }
}

impl fmt::Display for TextColor {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(name) = &self.name {
            write!(f, "{}", name.clone())
        } else {
            write!(f, "{}", self.format())
        }
    }
}

// from ChatFormatting to TextColor
impl TryFrom<ChatFormatting> for TextColor {
    type Error = String;

    fn try_from(formatter: ChatFormatting) -> Result<Self, Self::Error> {
        if formatter.is_format() {
            return Err(format!("{} is not a color", formatter.name()));
        }
        let color = formatter.color().unwrap_or(0);
        Ok(Self::new(color, Some(formatter.name().to_string())))
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct Style {
    // These are options instead of just bools because None is different than false in this case
    pub color: Option<TextColor>,
    pub bold: Option<bool>,
    pub italic: Option<bool>,
    pub underlined: Option<bool>,
    pub strikethrough: Option<bool>,
    pub obfuscated: Option<bool>,
    /// Whether formatting should be reset before applying these styles
    pub reset: bool,
}

fn serde_serialize_field<S: serde::ser::SerializeStruct>(
    state: &mut S,
    name: &'static str,
    value: &Option<impl serde::Serialize>,
    default: &(impl serde::Serialize + ?Sized),
    reset: bool,
) -> Result<(), S::Error> {
    if let Some(value) = value {
        state.serialize_field(name, value)?;
    } else if reset {
        state.serialize_field(name, default)?;
    }
    Ok(())
}

#[cfg(feature = "simdnbt")]
fn simdnbt_serialize_field(
    compound: &mut simdnbt::owned::NbtCompound,
    name: &'static str,
    value: Option<impl simdnbt::ToNbtTag>,
    default: impl simdnbt::ToNbtTag,
    reset: bool,
) {
    if let Some(value) = value {
        compound.insert(name, value);
    } else if reset {
        compound.insert(name, default);
    }
}

impl Serialize for Style {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let len = if self.reset {
            6
        } else {
            usize::from(self.color.is_some())
                + usize::from(self.bold.is_some())
                + usize::from(self.italic.is_some())
                + usize::from(self.underlined.is_some())
                + usize::from(self.strikethrough.is_some())
                + usize::from(self.obfuscated.is_some())
        };
        let mut state = serializer.serialize_struct("Style", len)?;

        serde_serialize_field(&mut state, "color", &self.color, "white", self.reset)?;
        serde_serialize_field(&mut state, "bold", &self.bold, &false, self.reset)?;
        serde_serialize_field(&mut state, "italic", &self.italic, &false, self.reset)?;
        serde_serialize_field(
            &mut state,
            "underlined",
            &self.underlined,
            &false,
            self.reset,
        )?;
        serde_serialize_field(
            &mut state,
            "strikethrough",
            &self.strikethrough,
            &false,
            self.reset,
        )?;
        serde_serialize_field(
            &mut state,
            "obfuscated",
            &self.obfuscated,
            &false,
            self.reset,
        )?;

        state.end()
    }
}

#[cfg(feature = "simdnbt")]
impl simdnbt::Serialize for Style {
    fn to_compound(self) -> NbtCompound {
        let mut compound = NbtCompound::new();

        simdnbt_serialize_field(&mut compound, "color", self.color, "white", self.reset);
        simdnbt_serialize_field(&mut compound, "bold", self.bold, false, self.reset);
        simdnbt_serialize_field(&mut compound, "italic", self.italic, false, self.reset);
        simdnbt_serialize_field(
            &mut compound,
            "underlined",
            self.underlined,
            false,
            self.reset,
        );
        simdnbt_serialize_field(
            &mut compound,
            "strikethrough",
            self.strikethrough,
            false,
            self.reset,
        );
        simdnbt_serialize_field(
            &mut compound,
            "obfuscated",
            self.obfuscated,
            false,
            self.reset,
        );

        compound
    }
}

impl Style {
    pub fn empty() -> Self {
        Self::default()
    }

    pub fn deserialize(json: &Value) -> Style {
        let Some(json_object) = json.as_object() else {
            return Style::default();
        };
        let bold = json_object.get("bold").and_then(|v| v.as_bool());
        let italic = json_object.get("italic").and_then(|v| v.as_bool());
        let underlined = json_object.get("underlined").and_then(|v| v.as_bool());
        let strikethrough = json_object.get("strikethrough").and_then(|v| v.as_bool());
        let obfuscated = json_object.get("obfuscated").and_then(|v| v.as_bool());
        let color: Option<TextColor> = json_object
            .get("color")
            .and_then(|v| v.as_str())
            .and_then(|v| TextColor::parse(v.to_string()));
        Style {
            color,
            bold,
            italic,
            underlined,
            strikethrough,
            obfuscated,
            ..Style::default()
        }
    }

    /// Check if a style has no attributes set
    pub fn is_empty(&self) -> bool {
        self.color.is_none()
            && self.bold.is_none()
            && self.italic.is_none()
            && self.underlined.is_none()
            && self.strikethrough.is_none()
            && self.obfuscated.is_none()
    }

    /// find the necessary ansi code to get from this style to another
    pub fn compare_ansi(&self, after: &Style, default_style: &Style) -> String {
        let should_reset = after.reset ||
            // if it used to be bold and now it's not, reset
            (self.bold.unwrap_or(false) && !after.bold.unwrap_or(true)) ||
            // if it used to be italic and now it's not, reset
            (self.italic.unwrap_or(false) && !after.italic.unwrap_or(true)) ||
            // if it used to be underlined and now it's not, reset
            (self.underlined.unwrap_or(false) && !after.underlined.unwrap_or(true)) ||
            // if it used to be strikethrough and now it's not, reset
            (self.strikethrough.unwrap_or(false) && !after.strikethrough.unwrap_or(true)) ||
            // if it used to be obfuscated and now it's not, reset
            (self.obfuscated.unwrap_or(false) && !after.obfuscated.unwrap_or(true));

        let mut ansi_codes = String::new();

        let empty_style = Style::empty();

        let (before, after) = if should_reset {
            ansi_codes.push_str(Ansi::RESET);
            let mut updated_after = if after.reset {
                default_style.clone()
            } else {
                self.clone()
            };
            updated_after.apply(after);
            (&empty_style, updated_after)
        } else {
            (self, after.clone())
        };

        // if bold used to be false/default and now it's true, set bold
        if !before.bold.unwrap_or(false) && after.bold.unwrap_or(false) {
            ansi_codes.push_str(Ansi::BOLD);
        }
        // if italic used to be false/default and now it's true, set italic
        if !before.italic.unwrap_or(false) && after.italic.unwrap_or(false) {
            ansi_codes.push_str(Ansi::ITALIC);
        }
        // if underlined used to be false/default and now it's true, set underlined
        if !before.underlined.unwrap_or(false) && after.underlined.unwrap_or(false) {
            ansi_codes.push_str(Ansi::UNDERLINED);
        }
        // if strikethrough used to be false/default and now it's true, set
        // strikethrough
        if !before.strikethrough.unwrap_or(false) && after.strikethrough.unwrap_or(false) {
            ansi_codes.push_str(Ansi::STRIKETHROUGH);
        }
        // if obfuscated used to be false/default and now it's true, set obfuscated
        if !before.obfuscated.unwrap_or(false) && after.obfuscated.unwrap_or(false) {
            ansi_codes.push_str(Ansi::OBFUSCATED);
        }

        // if the new color is different and not none, set color
        let color_changed = {
            if before.color.is_none() && after.color.is_some() {
                true
            } else if before.color.is_some() && after.color.is_some() {
                before.color.clone().unwrap().value != after.color.as_ref().unwrap().value
            } else {
                false
            }
        };

        if color_changed {
            let after_color = after.color.as_ref().unwrap();
            ansi_codes.push_str(&Ansi::rgb(after_color.value));
        }

        ansi_codes
    }

    /// Apply another style to this one
    pub fn apply(&mut self, style: &Style) {
        if let Some(color) = &style.color {
            self.color = Some(color.clone());
        }
        if let Some(bold) = &style.bold {
            self.bold = Some(*bold);
        }
        if let Some(italic) = &style.italic {
            self.italic = Some(*italic);
        }
        if let Some(underlined) = &style.underlined {
            self.underlined = Some(*underlined);
        }
        if let Some(strikethrough) = &style.strikethrough {
            self.strikethrough = Some(*strikethrough);
        }
        if let Some(obfuscated) = &style.obfuscated {
            self.obfuscated = Some(*obfuscated);
        }
    }

    /// Apply a ChatFormatting to this style
    pub fn apply_formatting(&mut self, formatting: &ChatFormatting) {
        match *formatting {
            ChatFormatting::Bold => self.bold = Some(true),
            ChatFormatting::Italic => self.italic = Some(true),
            ChatFormatting::Underline => self.underlined = Some(true),
            ChatFormatting::Strikethrough => self.strikethrough = Some(true),
            ChatFormatting::Obfuscated => self.obfuscated = Some(true),
            ChatFormatting::Reset => self.reset = true,
            formatter => {
                // if it's a color, set it
                if let Some(color) = formatter.color() {
                    self.color = Some(TextColor::from_rgb(color));
                }
            }
        }
    }
}

#[cfg(feature = "simdnbt")]
impl simdnbt::Deserialize for Style {
    fn from_compound(
        compound: simdnbt::borrow::NbtCompound,
    ) -> Result<Self, simdnbt::DeserializeError> {
        let bold = compound.byte("bold").map(|v| v != 0);
        let italic = compound.byte("italic").map(|v| v != 0);
        let underlined = compound.byte("underlined").map(|v| v != 0);
        let strikethrough = compound.byte("strikethrough").map(|v| v != 0);
        let obfuscated = compound.byte("obfuscated").map(|v| v != 0);
        let color: Option<TextColor> = compound
            .string("color")
            .and_then(|v| TextColor::parse(v.to_string()));
        Ok(Style {
            color,
            bold,
            italic,
            underlined,
            strikethrough,
            obfuscated,
            ..Style::default()
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::component::DEFAULT_STYLE;

    #[test]
    fn text_color_named_colors() {
        assert_eq!(TextColor::parse("red".to_string()).unwrap().value, 16733525);
    }
    #[test]
    fn text_color_hex_colors() {
        assert_eq!(
            TextColor::parse("#a1b2c3".to_string()).unwrap().value,
            10597059
        );
    }

    #[test]
    fn ansi_difference_should_reset() {
        let style_a = Style {
            bold: Some(true),
            italic: Some(true),
            ..Style::default()
        };
        let style_b = Style {
            bold: Some(false),
            ..Style::default()
        };
        let ansi_difference = style_a.compare_ansi(&style_b, &Style::default());
        assert_eq!(
            ansi_difference,
            format!(
                "{reset}{italic}",
                reset = Ansi::RESET,
                italic = Ansi::ITALIC
            )
        )
    }
    #[test]
    fn ansi_difference_shouldnt_reset() {
        let style_a = Style {
            bold: Some(true),
            ..Style::default()
        };
        let style_b = Style {
            italic: Some(true),
            ..Style::default()
        };
        let ansi_difference = style_a.compare_ansi(&style_b, &Style::default());
        assert_eq!(ansi_difference, Ansi::ITALIC)
    }

    #[test]
    fn ansi_difference_explicit_reset() {
        let style_a = Style {
            bold: Some(true),
            ..Style::empty()
        };
        let style_b = Style {
            italic: Some(true),
            reset: true,
            ..Style::empty()
        };
        let ansi_difference = style_a.compare_ansi(&style_b, &DEFAULT_STYLE);
        assert_eq!(
            ansi_difference,
            format!(
                "{reset}{italic}{white}",
                reset = Ansi::RESET,
                white = Ansi::rgb(ChatFormatting::White.color().unwrap()),
                italic = Ansi::ITALIC
            )
        )
    }

    #[test]
    fn test_from_code() {
        assert_eq!(
            ChatFormatting::from_code('a').unwrap(),
            ChatFormatting::Green
        );
    }

    #[test]
    fn test_apply_formatting() {
        let mut style = Style::default();
        style.apply_formatting(&ChatFormatting::Bold);
        style.apply_formatting(&ChatFormatting::Red);
        assert_eq!(style.color, Some(TextColor::from_rgb(16733525)));
    }
}