kyori-component-json 0.2.0

A library for serializing and deserializing Kyori Component JSON content
Documentation
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
697
698
699
700
701
//! MiniMessage format parser and serializer for Minecraft components.
//!
//! Implements the [`ComponentParser`] and [`ComponentSerializer`] traits
//! for the MiniMessage text format.

use crate::parsing::{ComponentParser, ComponentSerializer};
use crate::{
    ClickEvent, Color, Component, ComponentObject, HoverEvent, NamedColor, Style, TextDecoration,
};
use std::collections::HashMap;
use std::error::Error;
use std::fmt;

/// Represents errors that can occur during MiniMessage parsing/serialization.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MiniMessageError(String);

impl fmt::Display for MiniMessageError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "MiniMessage error: {}", self.0)
    }
}

impl Error for MiniMessageError {}

/// Configuration for MiniMessage parsing/serialization.
#[derive(Debug, Clone, Default, PartialEq, Eq, Copy, Hash)]
pub struct MiniMessageConfig {
    /// Whether to use strict parsing (requires proper tag closing)
    pub strict: bool,
    /// Whether to parse legacy color codes (e.g., &6 for gold)
    pub parse_legacy_colors: bool,
}

/// MiniMessage parser and serializer implementation.
#[derive(Debug, Clone, Copy)]
pub struct MiniMessage {
    config: MiniMessageConfig,
}

impl MiniMessage {
    /// Creates a new MiniMessage instance with default configuration.
    pub fn new() -> Self {
        Self::with_config(Default::default())
    }

    /// Creates a new MiniMessage instance with custom configuration.
    pub fn with_config(config: MiniMessageConfig) -> Self {
        MiniMessage { config }
    }

    /// Parse input using instance configuration
    pub fn parse(&self, input: impl AsRef<str>) -> Result<Component, MiniMessageError> {
        let mut parser = Parser::new(input.as_ref(), &self.config);
        parser.parse()
    }
}

impl Default for MiniMessage {
    fn default() -> Self {
        Self::new()
    }
}

impl ComponentParser for MiniMessage {
    type Err = MiniMessageError;

    /// Parse input from MiniMessage string to Component using default configuration.
    fn from_string(input: impl AsRef<str>) -> Result<Component, Self::Err> {
        // FIXME: use default config since we can't access instance data in trait method as that
        // isn't idiomatic Rust
        let config = MiniMessageConfig::default();
        let mut parser = Parser::new(input.as_ref(), &config);
        parser.parse()
    }
}

impl ComponentSerializer for MiniMessage {
    type Err = MiniMessageError;

    fn to_string(component: &Component) -> Result<String, Self::Err> {
        Serializer::new().serialize(component)
    }
}

/// Internal parser state
struct Parser<'a> {
    input: &'a str,
    position: usize,
    config: &'a MiniMessageConfig,
    style_stack: Vec<Style>,
    component_parts: Vec<Component>,
}

impl<'a> Parser<'a> {
    fn new(input: &'a str, config: &'a MiniMessageConfig) -> Self {
        Self {
            input,
            position: 0,
            config,
            style_stack: vec![Style::default()],
            component_parts: Vec::new(),
        }
    }

    fn parse(&mut self) -> Result<Component, MiniMessageError> {
        while self.position < self.input.len() {
            if self.starts_with('<') {
                self.parse_tag()?;
            } else {
                self.parse_text()?;
            }
        }

        let parts = std::mem::take(&mut self.component_parts);
        if parts.len() == 1 {
            // SAFETY: This is safe because we always have at least one style
            Ok(parts.into_iter().next().unwrap())
        } else {
            Ok(Component::Array(parts))
        }
    }

    fn parse_text(&mut self) -> Result<(), MiniMessageError> {
        let start = self.position;
        while self.position < self.input.len() {
            if self.starts_with('<') || (self.config.parse_legacy_colors && self.starts_with('&')) {
                break;
            }
            self.position += 1;
        }

        if start < self.position {
            let text = &self.input[start..self.position];
            let current_style = self.current_style();
            let mut comp = Component::text(text);
            comp = comp.color(current_style.color.clone());
            comp = comp.decorations(&self.collect_decorations());
            self.component_parts.push(comp);
        }
        Ok(())
    }

    fn parse_tag(&mut self) -> Result<(), MiniMessageError> {
        // Skip '<'
        self.position += 1;

        if self.starts_with('/') {
            // Closing tag
            self.position += 1;
            let tag_name = self.read_tag_name()?;
            self.handle_close_tag(&tag_name)?;
            self.expect('>')?;
        } else {
            // Opening tag
            let tag_name = self.read_tag_name()?;
            let mut args = Vec::new();
            let mut self_closing = false;

            while self.position < self.input.len() {
                // skip whitespace
                self.skip_whitespace();
                // skip colon separators
                while self.starts_with(':') {
                    self.position += 1;
                    self.skip_whitespace();
                }

                // if we’ve hit the end of the tag, stop
                if self.starts_with('>') || self.starts_with('/') {
                    break;
                }

                // read an argument
                let arg = self.read_argument()?;
                args.push(arg);
            }

            // Check for self-closing tag
            if self.starts_with('/') {
                self.position += 1;
                self_closing = true;
            }
            self.expect('>')?;

            self.handle_open_tag(&tag_name, args, self_closing)?;
        }

        Ok(())
    }

    fn read_tag_name(&mut self) -> Result<String, MiniMessageError> {
        let start = self.position;
        while self.position < self.input.len() {
            let c = self.current_char();
            if !c.is_ascii_alphanumeric() && c != '_' && c != '-' {
                break;
            }
            self.position += 1;
        }
        if start == self.position {
            return Err(MiniMessageError("Expected tag name".to_string()));
        }
        Ok(self.input[start..self.position].to_lowercase())
    }

    fn read_argument(&mut self) -> Result<String, MiniMessageError> {
        if self.starts_with('\'') || self.starts_with('"') {
            self.read_quoted_string()
        } else {
            self.read_unquoted_string()
        }
    }

    fn read_quoted_string(&mut self) -> Result<String, MiniMessageError> {
        let quote_char = self.current_char();
        self.position += 1;

        let mut escaped = false;
        let mut result = String::new();

        while self.position < self.input.len() {
            let c = self.current_char();
            if escaped {
                result.push(c);
                escaped = false;
            } else if c == '\\' {
                escaped = true;
            } else if c == quote_char {
                self.position += 1;
                return Ok(result);
            } else {
                result.push(c);
            }
            self.position += 1;
        }

        Err(MiniMessageError("Unterminated quoted string".to_string()))
    }

    fn read_unquoted_string(&mut self) -> Result<String, MiniMessageError> {
        let start = self.position;
        while self.position < self.input.len() {
            let c = self.current_char();
            if c == ':' || c == '>' || c == '/' || c.is_whitespace() {
                break;
            }
            self.position += 1;
        }
        // what?
        // if start == self.position {
        //     return Err(MiniMessageError("Expected argument".to_string()));
        // }
        Ok(self.input[start..self.position].to_string())
    }

    fn handle_open_tag(
        &mut self,
        tag: &str,
        args: Vec<String>,
        self_closing: bool,
    ) -> Result<(), MiniMessageError> {
        match tag {
            // Colors
            "black" => self.push_style(|s| s.color = Some(Color::Named(NamedColor::Black)))?,
            "dark_blue" => {
                self.push_style(|s| s.color = Some(Color::Named(NamedColor::DarkBlue)))?
            }
            "dark_green" => {
                self.push_style(|s| s.color = Some(Color::Named(NamedColor::DarkGreen)))?
            }
            "dark_aqua" => {
                self.push_style(|s| s.color = Some(Color::Named(NamedColor::DarkAqua)))?
            }
            "dark_red" => self.push_style(|s| s.color = Some(Color::Named(NamedColor::DarkRed)))?,
            "dark_purple" => {
                self.push_style(|s| s.color = Some(Color::Named(NamedColor::DarkPurple)))?
            }
            "gold" => self.push_style(|s| s.color = Some(Color::Named(NamedColor::Gold)))?,
            "gray" => self.push_style(|s| s.color = Some(Color::Named(NamedColor::Gray)))?,
            "dark_gray" => {
                self.push_style(|s| s.color = Some(Color::Named(NamedColor::DarkGray)))?
            }
            "blue" => self.push_style(|s| s.color = Some(Color::Named(NamedColor::Blue)))?,
            "green" => self.push_style(|s| s.color = Some(Color::Named(NamedColor::Green)))?,
            "aqua" => self.push_style(|s| s.color = Some(Color::Named(NamedColor::Aqua)))?,
            "red" => self.push_style(|s| s.color = Some(Color::Named(NamedColor::Red)))?,
            "light_purple" => {
                self.push_style(|s| s.color = Some(Color::Named(NamedColor::LightPurple)))?
            }
            "yellow" => self.push_style(|s| s.color = Some(Color::Named(NamedColor::Yellow)))?,
            "white" => self.push_style(|s| s.color = Some(Color::Named(NamedColor::White)))?,
            "color" | "colour" | "c" if !args.is_empty() => {
                if let Some(color) = parse_color(&args[0]) {
                    self.push_style(|s| s.color = Some(color))?
                }
            }

            // Decorations
            "bold" | "b" => self.push_style(|s| s.bold = Some(true))?,
            "italic" | "i" | "em" => self.push_style(|s| s.italic = Some(true))?,
            "underlined" | "u" => self.push_style(|s| s.underlined = Some(true))?,
            "strikethrough" | "st" => self.push_style(|s| s.strikethrough = Some(true))?,
            "obfuscated" | "obf" => self.push_style(|s| s.obfuscated = Some(true))?,

            // Reset tag
            "reset" => self.reset_style()?,

            // Click events
            "click" if args.len() >= 2 => {
                let action = args[0].as_str();
                let value = args[1].as_str();
                match action {
                    "run_command" => self.push_style(|s| {
                        s.click_event = Some(ClickEvent::RunCommand {
                            command: value.to_string(),
                        })
                    })?,
                    "suggest_command" => self.push_style(|s| {
                        s.click_event = Some(ClickEvent::SuggestCommand {
                            command: value.to_string(),
                        })
                    })?,
                    "open_url" => self.push_style(|s| {
                        s.click_event = Some(ClickEvent::OpenUrl {
                            url: value.to_string(),
                        })
                    })?,
                    "copy_to_clipboard" => self.push_style(|s| {
                        s.click_event = Some(ClickEvent::CopyToClipboard {
                            value: value.to_string(),
                        })
                    })?,
                    _ => {}
                }
            }

            // Hover events
            "hover" if !args.is_empty() => {
                let action = args[0].as_str();
                if action == "show_text" && args.len() >= 2 {
                    let mut nested_parser = Parser::new(&args[1], self.config);
                    let nested = nested_parser.parse()?;
                    self.push_style(|s| {
                        s.hover_event = Some(HoverEvent::ShowText { value: nested })
                    })?;
                }
            }

            // Newline
            "newline" | "br" => {
                self.component_parts.push(Component::text("\n"));
            }

            // Insertion
            "insert" | "insertion" if !args.is_empty() => {
                self.push_style(|s| s.insertion = Some(args[0].clone()))?
            }

            // Handle self-closing tags
            _ if self_closing => {
                // For self-closing tags, create an empty component with the style
                let current_style = self.current_style();
                let mut comp = Component::text("");
                comp = comp.color(current_style.color.clone());
                comp = comp.decorations(&self.collect_decorations());
                self.component_parts.push(comp);
            }

            // Unknown tags are treated as text
            _ => {
                let mut tag_text = format!("<{tag}");
                for arg in args {
                    tag_text.push(':');
                    tag_text.push_str(&arg);
                }
                if self_closing {
                    tag_text.push('/');
                }
                tag_text.push('>');
                self.component_parts
                    .push(Component::text(tag_text).apply_fallback_style(self.current_style()));
            }
        }

        Ok(())
    }

    fn handle_close_tag(&mut self, tag: &str) -> Result<(), MiniMessageError> {
        match tag {
            "bold" | "b" | "italic" | "i" | "em" | "underlined" | "u" | "strikethrough" | "st"
            | "obfuscated" | "obf" | "color" | "colour" | "c" | "click" | "hover" | "insert"
            | "insertion" => {
                self.pop_style()?;
            }
            _ => {
                // For unknown tags, just pop the style anyway
                if self.style_stack.len() > 1 {
                    self.style_stack.pop();
                }
            }
        }
        Ok(())
    }

    fn push_style<F>(&mut self, modifier: F) -> Result<(), MiniMessageError>
    where
        F: FnOnce(&mut Style),
    {
        let mut new_style = self.current_style().clone();
        modifier(&mut new_style);
        self.style_stack.push(new_style);
        Ok(())
    }

    fn pop_style(&mut self) -> Result<(), MiniMessageError> {
        if self.style_stack.len() > 1 {
            self.style_stack.pop();
            Ok(())
        } else {
            Err(MiniMessageError("Unbalanced closing tag".to_string()))
        }
    }

    fn reset_style(&mut self) -> Result<(), MiniMessageError> {
        while self.style_stack.len() > 1 {
            self.style_stack.pop();
        }
        Ok(())
    }

    fn current_style(&self) -> &Style {
        // SAFETY: This is safe because we always have at least one style
        self.style_stack.last().unwrap()
    }

    fn collect_decorations(&self) -> HashMap<TextDecoration, Option<bool>> {
        let style = self.current_style();
        let mut decorations = HashMap::new();
        if let Some(bold) = style.bold {
            decorations.insert(TextDecoration::Bold, Some(bold));
        }
        if let Some(italic) = style.italic {
            decorations.insert(TextDecoration::Italic, Some(italic));
        }
        if let Some(underlined) = style.underlined {
            decorations.insert(TextDecoration::Underlined, Some(underlined));
        }
        if let Some(strikethrough) = style.strikethrough {
            decorations.insert(TextDecoration::Strikethrough, Some(strikethrough));
        }
        if let Some(obfuscated) = style.obfuscated {
            decorations.insert(TextDecoration::Obfuscated, Some(obfuscated));
        }
        decorations
    }

    fn starts_with(&self, c: char) -> bool {
        self.input[self.position..].starts_with(c)
    }

    fn current_char(&self) -> char {
        self.input[self.position..].chars().next().unwrap_or('\0')
    }

    fn skip_whitespace(&mut self) {
        while self.position < self.input.len() {
            if !self.input[self.position..].starts_with(char::is_whitespace) {
                break;
            }
            self.position += 1;
        }
    }

    fn expect(&mut self, c: char) -> Result<(), MiniMessageError> {
        if self.position < self.input.len() && self.current_char() == c {
            self.position += 1;
            Ok(())
        } else {
            Err(MiniMessageError(format!("Expected '{c}'")))
        }
    }
}

/// Serializes components to MiniMessage format
struct Serializer {
    output: String,
    current_style: Style,
}

impl Serializer {
    fn new() -> Self {
        Self {
            output: String::new(),
            current_style: Style::default(),
        }
    }

    fn serialize(&mut self, component: &Component) -> Result<String, MiniMessageError> {
        self.serialize_component(component)?;
        Ok(self.output.clone())
    }

    fn serialize_component(&mut self, component: &Component) -> Result<(), MiniMessageError> {
        match component {
            Component::String(s) => self.serialize_text(s),
            Component::Array(components) => {
                let base_style = self.current_style.clone();
                for comp in components {
                    // Reset to base style before each component
                    self.current_style = base_style.clone();
                    self.serialize_component(comp)?;
                }
                Ok(())
            }
            Component::Object(obj) => self.serialize_object(obj),
        }
    }

    fn serialize_object(&mut self, obj: &ComponentObject) -> Result<(), MiniMessageError> {
        // Save current style to compare changes
        let prev_style = self.current_style.clone();

        // Apply style changes
        let mut style_changes = Vec::new();

        if let Some(color) = &obj.color {
            if Some(color) != prev_style.color.as_ref() {
                if let Some(named) = get_named_color(color) {
                    style_changes.push(named.to_string());
                } else if let Color::Hex(hex) = color {
                    style_changes.push(format!("color:{hex}"));
                }
            }
        }

        if obj.bold != prev_style.bold && obj.bold == Some(true) {
            style_changes.push("bold".to_string());
        }

        if obj.italic != prev_style.italic && obj.italic == Some(true) {
            style_changes.push("italic".to_string());
        }

        if obj.underlined != prev_style.underlined && obj.underlined == Some(true) {
            style_changes.push("underlined".to_string());
        }

        if obj.strikethrough != prev_style.strikethrough && obj.strikethrough == Some(true) {
            style_changes.push("strikethrough".to_string());
        }

        if obj.obfuscated != prev_style.obfuscated && obj.obfuscated == Some(true) {
            style_changes.push("obfuscated".to_string());
        }

        // Apply style changes
        for change in &style_changes {
            self.output.push_str(&format!("<{change}>"));
        }

        // Update current style
        self.current_style = Style {
            color: obj.color.clone(),
            bold: obj.bold,
            italic: obj.italic,
            underlined: obj.underlined,
            strikethrough: obj.strikethrough,
            obfuscated: obj.obfuscated,
            ..self.current_style.clone()
        };

        // Serialize text content
        if let Some(text) = &obj.text {
            self.serialize_text(text)?;
        }

        // Serialize children
        if let Some(extra) = &obj.extra {
            for comp in extra {
                self.serialize_component(comp)?;
            }
        }

        // Close style changes
        for change in style_changes.iter().rev() {
            self.output.push_str(&format!("</{change}>"));
        }

        // Restore previous style
        self.current_style = prev_style;

        Ok(())
    }

    fn serialize_text(&mut self, text: &str) -> Result<(), MiniMessageError> {
        // Escape special characters
        for c in text.chars() {
            match c {
                '<' => self.output.push_str("&lt;"),
                '>' => self.output.push_str("&gt;"),
                '&' => self.output.push_str("&amp;"),
                _ => self.output.push(c),
            }
        }
        Ok(())
    }
}

fn parse_color(input: &str) -> Option<Color> {
    if let Ok(named) = input.parse::<NamedColor>() {
        return Some(Color::Named(named));
    }

    if input.starts_with('#') && input.len() == 7 {
        return Some(Color::Hex(input.to_string()));
    }

    None
}

fn get_named_color(color: &Color) -> Option<NamedColor> {
    match color {
        Color::Named(named) => Some(*named),
        Color::Hex(hex) => match hex.as_str() {
            "#000000" => Some(NamedColor::Black),
            "#0000AA" => Some(NamedColor::DarkBlue),
            "#00AA00" => Some(NamedColor::DarkGreen),
            "#00AAAA" => Some(NamedColor::DarkAqua),
            "#AA0000" => Some(NamedColor::DarkRed),
            "#AA00AA" => Some(NamedColor::DarkPurple),
            "#FFAA00" => Some(NamedColor::Gold),
            "#AAAAAA" => Some(NamedColor::Gray),
            "#555555" => Some(NamedColor::DarkGray),
            "#5555FF" => Some(NamedColor::Blue),
            "#55FF55" => Some(NamedColor::Green),
            "#55FFFF" => Some(NamedColor::Aqua),
            "#FF5555" => Some(NamedColor::Red),
            "#FF55FF" => Some(NamedColor::LightPurple),
            "#FFFF55" => Some(NamedColor::Yellow),
            "#FFFFFF" => Some(NamedColor::White),
            _ => None,
        },
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Component, NamedColor};

    #[test]
    fn test_parse_simple() {
        let mm = MiniMessage::new();
        let comp = mm.parse("Hello <red>world</red>!").unwrap();

        if let Component::Array(parts) = comp {
            assert_eq!(parts.len(), 3);
            assert_eq!(parts[0].get_plain_text().unwrap(), "Hello ");
            assert_eq!(parts[1].get_plain_text().unwrap(), "world");
            assert_eq!(parts[2].get_plain_text().unwrap(), "!");
        } else {
            panic!("Expected array component");
        }
    }

    #[test]
    fn test_parse_nested() {
        let mm = MiniMessage::new();
        let comp = mm
            .parse("Click <hover:show_text:'<red>Action!'>here</hover>")
            .expect("Failed to parse component");

        // Verify hover event exists and contains a red text component
        if let Component::Object(obj) = &comp
            && let Some(children) = &obj.extra
        {
            if let Component::Object(hover_obj) = &children[1]
                && let Some(hover_event) = &hover_obj.hover_event
            {
                match hover_event {
                    HoverEvent::ShowText { value } => {
                        assert_eq!(value.get_plain_text().unwrap(), "Action!");
                    }
                    _ => panic!("Expected show_text hover event"),
                }
            }
        }
    }

    #[test]
    fn test_serialize_simple() {
        let comp = Component::text("Hello ")
            .color(Some(Color::Named(NamedColor::Yellow)))
            .append(Component::text("world").color(Some(Color::Named(NamedColor::Red))));

        let result = MiniMessage::to_string(&comp).unwrap();
        // TODO: is <yellow>Hello </yellow><red>world</red> technically correct?
        assert_eq!(result, "<yellow>Hello <red>world</red></yellow>");
    }
}