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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
//! A parser for Minecraft's [legacy formatting system][legacy_fmt], created
//! with careful attention to the quirks of the vanilla client's implementation.
//!
//! # Features
//!
//! * Iterator-based, non-allocating parser
//! * Supports `#![no_std]` usage (with `default-features` set to `false`)
//! * Implements the entire spec as well as vanilla client quirks (such as handling
//!   of whitespace with the `STRIKETHROUGH` style)
//! * Helpers for pretty-printing the parsed `Span`s to the terminal
//! * Support for parsing any start character for the formatting codes (vanilla
//!   uses `§` while many community tools use `&`)
//!
//! # Examples
//!
//! Using [`SpanIter`][SpanIter]:
//!
//! ```
//! use mc_legacy_formatting::{SpanIter, Span, Color, Styles};
//!
//! let s = "§4This will be dark red §oand italic";
//! let mut span_iter = SpanIter::new(s);
//!
//! assert_eq!(span_iter.next().unwrap(), Span::new_styled("This will be dark red ", Color::DarkRed, Styles::empty()));
//! assert_eq!(span_iter.next().unwrap(), Span::new_styled("and italic", Color::DarkRed, Styles::ITALIC));
//! assert!(span_iter.next().is_none());
//! ```
//!
//! With a custom start character:
//!
//! ```
//! use mc_legacy_formatting::{SpanIter, Span, Color, Styles};
//!
//! let s = "&6It's a lot easier to type &b& &6than &b§";
//! let mut span_iter = SpanIter::new(s).with_start_char('&');
//!
//! assert_eq!(span_iter.next().unwrap(), Span::new_styled("It's a lot easier to type ", Color::Gold, Styles::empty()));
//! assert_eq!(span_iter.next().unwrap(), Span::new_styled("& ", Color::Aqua, Styles::empty()));
//! assert_eq!(span_iter.next().unwrap(), Span::new_styled("than ", Color::Gold, Styles::empty()));
//! assert_eq!(span_iter.next().unwrap(), Span::new_styled("§", Color::Aqua, Styles::empty()));
//! assert!(span_iter.next().is_none());
//! ```
//!
//! [legacy_fmt]: https://wiki.vg/Chat#Colors
//! [SpanIter]: struct.SpanIter.html

#![no_std]
#![deny(missing_docs)]
#![deny(unused_must_use)]

/// Bring `std` in for testing
#[cfg(test)]
extern crate std;

use core::str::CharIndices;

use bitflags::bitflags;

#[cfg(feature = "color-print")]
mod color_print;

#[cfg(feature = "color-print")]
pub use color_print::PrintSpanColored;

/// An iterator that yields [`Span`][Span]s from an input string.
///
/// # Examples
///
/// ```
/// use mc_legacy_formatting::{SpanIter, Span, Color, Styles};
///
/// let s = "§4This will be dark red §oand italic";
/// let mut span_iter = SpanIter::new(s);
///
/// assert_eq!(span_iter.next().unwrap(), Span::new_styled("This will be dark red ", Color::DarkRed, Styles::empty()));
/// assert_eq!(span_iter.next().unwrap(), Span::new_styled("and italic", Color::DarkRed, Styles::ITALIC));
/// assert!(span_iter.next().is_none());
/// ```
///
/// [Span]: enum.Span.html
#[derive(Debug, Clone)]
pub struct SpanIter<'a> {
    buf: &'a str,
    chars: CharIndices<'a>,
    /// The character that indicates the beginning of a fmt code
    ///
    /// The vanilla client uses `§` for this, but community tooling often uses
    /// `&`, so we allow it to be configured
    start_char: char,
    color: Color,
    styles: Styles,
    finished: bool,
}

impl<'a> SpanIter<'a> {
    /// Create a new `SpanIter` to parse the given string
    pub fn new(s: &'a str) -> Self {
        Self {
            buf: s,
            chars: s.char_indices(),
            start_char: '§',
            color: Color::White,
            styles: Styles::default(),
            finished: false,
        }
    }

    /// Set the start character used while parsing.
    ///
    /// # Examples
    ///
    /// ```
    /// use mc_legacy_formatting::{SpanIter, Span, Color, Styles};
    ///
    /// let s = "&6It's a lot easier to type &b& &6than &b§";
    /// let mut span_iter = SpanIter::new(s).with_start_char('&');
    ///
    /// assert_eq!(span_iter.next().unwrap(), Span::new_styled("It's a lot easier to type ", Color::Gold, Styles::empty()));
    /// assert_eq!(span_iter.next().unwrap(), Span::new_styled("& ", Color::Aqua, Styles::empty()));
    /// assert_eq!(span_iter.next().unwrap(), Span::new_styled("than ", Color::Gold, Styles::empty()));
    /// assert_eq!(span_iter.next().unwrap(), Span::new_styled("§", Color::Aqua, Styles::empty()));
    /// assert!(span_iter.next().is_none());
    /// ```
    pub fn with_start_char(mut self, c: char) -> Self {
        self.start_char = c;
        self
    }

    /// Set the start character used while parsing
    pub fn set_start_char(&mut self, c: char) {
        self.start_char = c;
    }

    /// Update the currently stored color
    fn update_color(&mut self, color: Color) {
        self.color = color;
        // According to https://wiki.vg/Chat, using a color code resets the current
        // style
        self.styles = Styles::empty();
    }

    /// Insert `styles` into the currently stored styles
    fn update_styles(&mut self, styles: Styles) {
        self.styles.insert(styles);
    }

    /// Should be called when encountering the `RESET` fmt code
    fn reset_styles(&mut self) {
        self.color = Color::White;
        self.styles = Styles::empty();
    }

    /// Make a `Span` based off the current state of the iterator
    ///
    /// The span will be from `start..end`
    fn make_span(&self, start: usize, end: usize) -> Span<'a> {
        if self.color == Color::White && self.styles.is_empty() {
            Span::Plain(&self.buf[start..end])
        } else {
            let text = &self.buf[start..end];

            // The vanilla client renders whitespace with `Styles::STRIKETHROUGH`
            // as a solid line. This replicates that behavior
            if text.chars().all(|c| c.is_ascii_whitespace())
                && self.styles.contains(Styles::STRIKETHROUGH)
            {
                Span::StrikethroughWhitespace {
                    num_chars: text.len(),
                    color: self.color,
                    styles: self.styles,
                }
            } else {
                Span::Styled {
                    text,
                    color: self.color,
                    styles: self.styles,
                }
            }
        }
    }
}

/// Keeps track of the state for each iteration
#[derive(Debug, Copy, Clone)]
enum SpanIterState {
    GatheringStyles(GatheringStylesState),
    GatheringText(GatheringTextState),
}

/// In this state we are at the beginning of an iteration and we are looking to
/// handle any initial formatting codes
#[derive(Debug, Copy, Clone)]
enum GatheringStylesState {
    /// We're looking for our start char
    ExpectingStartChar,
    /// We've found our start char and are expecting a fmt code after it
    ExpectingFmtCode,
}

/// In this state we've encountered text unrelated to formatting, which means
/// the next valid fmt code we encounter ends this iteration
#[derive(Debug, Copy, Clone)]
enum GatheringTextState {
    /// We're waiting to find our start char
    WaitingForStartChar,
    /// We've found our start char and are expecting a fmt code after it
    ///
    /// If we find a valid fmt code in this state, we need to make a span, apply
    /// this last fmt code to our state, and end this iteration.
    ExpectingEndChar,
}

impl<'a> Iterator for SpanIter<'a> {
    type Item = Span<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.finished {
            return None;
        }
        let mut state = SpanIterState::GatheringStyles(GatheringStylesState::ExpectingStartChar);
        let mut span_start = None;
        let mut span_end = None;

        while let Some((idx, c)) = self.chars.next() {
            state = match state {
                SpanIterState::GatheringStyles(style_state) => match style_state {
                    GatheringStylesState::ExpectingStartChar => {
                        span_start = Some(idx);
                        match c {
                            c if c == self.start_char => SpanIterState::GatheringStyles(
                                GatheringStylesState::ExpectingFmtCode,
                            ),
                            _ => SpanIterState::GatheringText(
                                GatheringTextState::WaitingForStartChar,
                            ),
                        }
                    }
                    GatheringStylesState::ExpectingFmtCode => {
                        if let Some(color) = Color::from_char(c) {
                            self.update_color(color);
                            span_start = None;
                            SpanIterState::GatheringStyles(GatheringStylesState::ExpectingStartChar)
                        } else if let Some(style) = Styles::from_char(c) {
                            self.update_styles(style);
                            span_start = None;
                            SpanIterState::GatheringStyles(GatheringStylesState::ExpectingStartChar)
                        } else if c == 'r' || c == 'R' {
                            // Handle the `RESET` fmt code

                            self.reset_styles();
                            span_start = None;
                            SpanIterState::GatheringStyles(GatheringStylesState::ExpectingStartChar)
                        } else {
                            SpanIterState::GatheringText(GatheringTextState::WaitingForStartChar)
                        }
                    }
                },
                SpanIterState::GatheringText(text_state) => match text_state {
                    GatheringTextState::WaitingForStartChar => match c {
                        c if c == self.start_char => {
                            span_end = Some(idx);
                            SpanIterState::GatheringText(GatheringTextState::ExpectingEndChar)
                        }
                        _ => state,
                    },
                    GatheringTextState::ExpectingEndChar => {
                        // Note that we only end this iteration if we find a valid fmt code
                        //
                        // If we do, we make sure to apply it to our state so that we can
                        // pick up where we left off when the next iteration begins

                        if let Some(color) = Color::from_char(c) {
                            let span = self.make_span(span_start.unwrap(), span_end.unwrap());
                            self.update_color(color);
                            return Some(span);
                        } else if let Some(style) = Styles::from_char(c) {
                            let span = self.make_span(span_start.unwrap(), span_end.unwrap());
                            self.update_styles(style);
                            return Some(span);
                        } else if c == 'r' || c == 'R' {
                            // Handle the `RESET` fmt code

                            let span = self.make_span(span_start.unwrap(), span_end.unwrap());
                            self.reset_styles();
                            return Some(span);
                        } else {
                            span_end = None;
                            SpanIterState::GatheringText(GatheringTextState::WaitingForStartChar)
                        }
                    }
                },
            }
        }

        self.finished = true;
        span_start.map(|start| self.make_span(start, self.buf.len()))
    }
}

/// Text with an associated color and associated styles.
///
/// `Span` implements `Display` and can be neatly printed.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum Span<'a> {
    /// A styled slice of text
    Styled {
        /// The styled text slice
        text: &'a str,
        /// The color of the text
        color: Color,
        /// Styles that should be applied to the text
        styles: Styles,
    },
    /// An unbroken sequence of whitespace that was given the `STRIKETHROUGH`
    /// style.
    ///
    /// The vanilla client renders whitespace with the `STRIKETHROUGH` style
    /// as a solid line; this variant allows for replicating that behavior.
    StrikethroughWhitespace {
        /// The number of whitespace characters this span is in place of.
        ///
        /// You should draw `num_chars` dashes to represent the line (or,
        /// if your rendering situation allows for it, a solid line of
        /// `num_chars` length).
        num_chars: usize,
        /// The color of the line
        color: Color,
        /// Styles applied to the line (will contain at least
        /// `STRIKETHROUGH`)
        styles: Styles,
    },
    /// An unstyled slice of text
    ///
    /// This should be given a default style. The vanilla client
    /// would use `color::White` and `Styles::empty()`.
    Plain(&'a str),
}

impl<'a> core::fmt::Display for Span<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        match self {
            Span::Styled { text, .. } => f.write_str(text),
            Span::StrikethroughWhitespace { num_chars, .. } => {
                (0..*num_chars).try_for_each(|_| f.write_str("-"))
            }
            Span::Plain(text) => f.write_str(text),
        }
    }
}

impl<'a> Span<'a> {
    /// Create a new `Span::Plain`
    pub fn new_plain(s: &'a str) -> Self {
        Span::Plain(s)
    }

    /// Create a new `Span::StrikethroughWhitespace`
    pub fn new_strikethrough_whitespace(num_chars: usize, color: Color, styles: Styles) -> Self {
        Span::StrikethroughWhitespace {
            num_chars,
            color,
            styles,
        }
    }

    /// Create a new `Span::Styled`
    pub fn new_styled(s: &'a str, color: Color, styles: Styles) -> Self {
        Span::Styled {
            text: s,
            color,
            styles,
        }
    }
}

/// Various colors that a `Span` can have.
///
/// See [the wiki.vg docs][colors] for specific information.
///
/// [colors]: https://wiki.vg/Chat#Colors
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[allow(missing_docs)]
pub enum Color {
    Black,
    DarkBlue,
    DarkGreen,
    DarkAqua,
    DarkRed,
    DarkPurple,
    Gold,
    Gray,
    DarkGray,
    Blue,
    Green,
    Aqua,
    Red,
    LightPurple,
    Yellow,
    White,
}

impl Default for Color {
    fn default() -> Self {
        Color::White
    }
}

impl Color {
    /// Map a `char` to a `Color`.
    ///
    /// Returns `None` if `c` didn't map to a `Color`.
    pub fn from_char(c: char) -> Option<Self> {
        Some(match c {
            '0' => Color::Black,
            '1' => Color::DarkBlue,
            '2' => Color::DarkGreen,
            '3' => Color::DarkAqua,
            '4' => Color::DarkRed,
            '5' => Color::DarkPurple,
            '6' => Color::Gold,
            '7' => Color::Gray,
            '8' => Color::DarkGray,
            '9' => Color::DarkBlue,
            // The vanilla client accepts lower or uppercase interchangeably
            'a' | 'A' => Color::Green,
            'b' | 'B' => Color::Aqua,
            'c' | 'C' => Color::Red,
            'd' | 'D' => Color::LightPurple,
            'e' | 'E' => Color::Yellow,
            'f' | 'F' => Color::White,
            _ => return None,
        })
    }

    /// Get the correct foreground hex color string for a given color
    ///
    /// # Examples
    ///
    /// ```
    /// use mc_legacy_formatting::Color;
    /// assert_eq!(Color::Aqua.foreground_hex_str(), "#55ffff");
    /// ```
    pub const fn foreground_hex_str(&self) -> &'static str {
        match self {
            Color::Black => "#000000",
            Color::DarkBlue => "#0000aa",
            Color::DarkGreen => "#00aa00",
            Color::DarkAqua => "#00aaaa",
            Color::DarkRed => "#aa0000",
            Color::DarkPurple => "#aa00aa",
            Color::Gold => "#ffaa00",
            Color::Gray => "#aaaaaa",
            Color::DarkGray => "#555555",
            Color::Blue => "#5555ff",
            Color::Green => "#55ff55",
            Color::Aqua => "#55ffff",
            Color::Red => "#ff5555",
            Color::LightPurple => "#ff55ff",
            Color::Yellow => "#ffff55",
            Color::White => "#ffffff",
        }
    }

    /// Get the correct background hex color string for a given color
    ///
    /// # Examples
    ///
    /// ```
    /// use mc_legacy_formatting::Color;
    /// assert_eq!(Color::Aqua.background_hex_str(), "#153f3f");
    /// ```
    pub const fn background_hex_str(&self) -> &'static str {
        match self {
            Color::Black => "#000000",
            Color::DarkBlue => "#00002a",
            Color::DarkGreen => "#002a00",
            Color::DarkAqua => "#002a2a",
            Color::DarkRed => "#2a0000",
            Color::DarkPurple => "#2a002a",
            Color::Gold => "#2a2a00",
            Color::Gray => "#2a2a2a",
            Color::DarkGray => "#151515",
            Color::Blue => "#15153f",
            Color::Green => "#153f15",
            Color::Aqua => "#153f3f",
            Color::Red => "#3f1515",
            Color::LightPurple => "#3f153f",
            Color::Yellow => "#3f3f15",
            Color::White => "#3f3f3f",
        }
    }

    /// Get the correct foreground RGB color values for a given color
    ///
    /// Returns (red, green, blue)
    ///
    /// # Examples
    ///
    /// ```
    /// use mc_legacy_formatting::Color;
    /// assert_eq!(Color::Aqua.foreground_rgb(), (85, 255, 255));
    /// ```
    pub const fn foreground_rgb(&self) -> (u8, u8, u8) {
        match self {
            Color::Black => (0, 0, 0),
            Color::DarkBlue => (0, 0, 170),
            Color::DarkGreen => (0, 170, 0),
            Color::DarkAqua => (0, 170, 170),
            Color::DarkRed => (170, 0, 0),
            Color::DarkPurple => (170, 0, 170),
            Color::Gold => (255, 170, 0),
            Color::Gray => (170, 170, 170),
            Color::DarkGray => (85, 85, 85),
            Color::Blue => (85, 85, 255),
            Color::Green => (85, 255, 85),
            Color::Aqua => (85, 255, 255),
            Color::Red => (255, 85, 85),
            Color::LightPurple => (255, 85, 255),
            Color::Yellow => (255, 255, 85),
            Color::White => (255, 255, 255),
        }
    }

    /// Get the correct background RGB color values for a given color
    ///
    /// Returns (red, green, blue)
    ///
    /// # Examples
    ///
    /// ```
    /// use mc_legacy_formatting::Color;
    /// assert_eq!(Color::Aqua.background_rgb(), (21, 63, 63));
    /// ```
    pub const fn background_rgb(&self) -> (u8, u8, u8) {
        match self {
            Color::Black => (0, 0, 0),
            Color::DarkBlue => (0, 0, 42),
            Color::DarkGreen => (0, 42, 0),
            Color::DarkAqua => (0, 42, 42),
            Color::DarkRed => (42, 0, 0),
            Color::DarkPurple => (42, 0, 42),
            Color::Gold => (42, 42, 0),
            Color::Gray => (42, 42, 42),
            Color::DarkGray => (21, 21, 21),
            Color::Blue => (21, 21, 63),
            Color::Green => (21, 63, 21),
            Color::Aqua => (21, 63, 63),
            Color::Red => (63, 21, 21),
            Color::LightPurple => (63, 21, 63),
            Color::Yellow => (63, 63, 21),
            Color::White => (63, 63, 63),
        }
    }
}

bitflags! {
    /// Styles that can be combined and applied to a `Span`.
    ///
    /// The `RESET` flag is missing because the parser implemented in `SpanIter`
    /// takes care of it for you.
    ///
    /// See [wiki.vg's docs][styles] for detailed info about each style.
    ///
    /// # Examples
    ///
    /// ```
    /// use mc_legacy_formatting::Styles;
    /// let styles = Styles::BOLD | Styles::ITALIC | Styles::UNDERLINED;
    ///
    /// assert!(styles.contains(Styles::BOLD));
    /// assert!(!styles.contains(Styles::RANDOM));
    /// ```
    ///
    /// [styles]: https://wiki.vg/Chat#Styles
    #[derive(Default)]
    pub struct Styles: u32 {
        /// Signals that the `Span`'s text should be replaced with randomized
        /// characters at a constant interval
        const RANDOM        = 0b00000001;
        /// Signals that the `Span`'s text should be bold
        const BOLD          = 0b00000010;
        /// Signals that the `Span`'s text should be strikethrough
        const STRIKETHROUGH = 0b00000100;
        /// Signals that the `Span`'s text should be underlined
        const UNDERLINED    = 0b00001000;
        /// Signals that the `Span`'s text should be italic
        const ITALIC        = 0b00010000;
    }
}

impl Styles {
    /// Map a `char` to a `Styles` object.
    ///
    /// Returns `None` if `c` didn't map to a `Styles` object.
    pub fn from_char(c: char) -> Option<Self> {
        Some(match c {
            // The vanilla client accepts lower or uppercase interchangeably
            'k' | 'K' => Styles::RANDOM,
            'l' | 'L' => Styles::BOLD,
            'm' | 'M' => Styles::STRIKETHROUGH,
            'n' | 'N' => Styles::UNDERLINED,
            'o' | 'O' => Styles::ITALIC,
            _ => return None,
        })
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use pretty_assertions::assert_eq;
    use std::vec;
    use std::vec::Vec;

    fn spans(s: &str) -> Vec<Span> {
        SpanIter::new(s).collect()
    }

    fn spans_sc(start_char: char, s: &str) -> Vec<Span> {
        SpanIter::new(s).with_start_char(start_char).collect()
    }

    mod fake_codes {
        use super::*;
        use pretty_assertions::assert_eq;

        #[test]
        fn no_formatting_code() {
            let s = "this has no formatting codes";
            assert_eq!(
                spans(s),
                vec![Span::new_plain("this has no formatting codes")]
            );
        }

        #[test]
        fn fake_code_at_start() {
            let s = "§this has no formatting codes";
            assert_eq!(
                spans(s),
                vec![Span::new_plain("§this has no formatting codes")]
            );
        }

        #[test]
        fn fake_code_space_at_start() {
            let s = "§ this has no formatting codes";
            assert_eq!(
                spans(s),
                vec![Span::new_plain("§ this has no formatting codes")]
            );
        }

        #[test]
        fn fake_code_at_end() {
            let s = "this has no formatting codes§";
            assert_eq!(
                spans(s),
                vec![Span::new_plain("this has no formatting codes§")]
            );
        }

        #[test]
        fn fake_code_space_at_end() {
            let s = "this has no formatting codes §";
            assert_eq!(
                spans(s),
                vec![Span::new_plain("this has no formatting codes §")]
            );
        }

        #[test]
        fn fake_code_middle() {
            let s = "this ha§s no formatting codes";
            assert_eq!(
                spans(s),
                vec![Span::new_plain("this ha§s no formatting codes")]
            );
        }

        #[test]
        fn fake_code_space_middle() {
            let s = "this has no § formatting codes";
            assert_eq!(
                spans(s),
                vec![Span::new_plain("this has no § formatting codes")]
            );
        }

        #[test]
        fn a_bunch_of_fakes() {
            let s = "§§§§§this has no format§ting codes§";
            assert_eq!(
                spans(s),
                vec![Span::new_plain("§§§§§this has no format§ting codes§")]
            );
        }
    }

    mod custom_start_char {
        use super::*;
        use pretty_assertions::assert_eq;

        #[test]
        fn using_ampersand() {
            let s = "&4this will be dark red";
            assert_eq!(
                spans_sc('&', s),
                vec![Span::new_styled(
                    "this will be dark red",
                    Color::DarkRed,
                    Styles::empty()
                )]
            );
        }

        #[test]
        fn multiple_styles() {
            let s = "&1&e&d&lthis will be light purple and bold &o&a&e&a&mand this \
                    will be green and strikethrough";
            assert_eq!(
                spans_sc('&', s),
                vec![
                    Span::new_styled(
                        "this will be light purple and bold ",
                        Color::LightPurple,
                        Styles::BOLD
                    ),
                    Span::new_styled(
                        "and this will be green and strikethrough",
                        Color::Green,
                        Styles::STRIKETHROUGH
                    )
                ]
            );
        }

        #[test]
        fn supports_uppercase_style_codes() {
            let s = "&5&m                  &6>&7&l&6&l>&6&l[&5&l&oPurple &8&l&oPrison&6&l]&6&l<&6<&5&m                     \
                        &R &7              (&4!&7) &e&lSERVER HAS &D&LRESET! &7(&4!&7)";
            assert_eq!(
                spans_sc('&', s),
                vec![
                    // The vanilla client renders whitespace with `Styles::STRIKETHROUGH`
                    // as a solid line.
                    Span::new_strikethrough_whitespace(
                        18,
                        Color::DarkPurple,
                        Styles::STRIKETHROUGH
                    ),
                    Span::new_styled(">", Color::Gold, Styles::empty()),
                    Span::new_styled(">", Color::Gold, Styles::BOLD),
                    Span::new_styled("[", Color::Gold, Styles::BOLD),
                    Span::new_styled("Purple ", Color::DarkPurple, Styles::BOLD | Styles::ITALIC),
                    Span::new_styled("Prison", Color::DarkGray, Styles::BOLD | Styles::ITALIC),
                    Span::new_styled("]", Color::Gold, Styles::BOLD),
                    Span::new_styled("<", Color::Gold, Styles::BOLD),
                    Span::new_styled("<", Color::Gold, Styles::empty()),
                    Span::new_strikethrough_whitespace(
                        21,
                        Color::DarkPurple,
                        Styles::STRIKETHROUGH
                    ),
                    Span::new_plain(" "),
                    Span::new_styled("              (", Color::Gray, Styles::empty()),
                    Span::new_styled("!", Color::DarkRed, Styles::empty()),
                    Span::new_styled(") ", Color::Gray, Styles::empty()),
                    Span::new_styled("SERVER HAS ", Color::Yellow, Styles::BOLD),
                    Span::new_styled("RESET! ", Color::LightPurple, Styles::BOLD),
                    Span::new_styled("(", Color::Gray, Styles::empty()),
                    Span::new_styled("!", Color::DarkRed, Styles::empty()),
                    Span::new_styled(")", Color::Gray, Styles::empty()),
                ]
            );
        }
    }

    #[test]
    fn dark_red() {
        let s = "§4this will be dark red";
        assert_eq!(
            spans(s),
            vec![Span::new_styled(
                "this will be dark red",
                Color::DarkRed,
                Styles::empty()
            )]
        );
    }

    #[test]
    fn dark_blue() {
        let s = "§1this will be dark blue";
        assert_eq!(
            spans(s),
            vec![Span::new_styled(
                "this will be dark blue",
                Color::DarkBlue,
                Styles::empty()
            )]
        );
    }

    #[test]
    fn aqua() {
        let s = "§1§bthis will be aqua";
        assert_eq!(
            spans(s),
            vec![Span::new_styled(
                "this will be aqua",
                Color::Aqua,
                Styles::empty()
            )]
        );
    }

    #[test]
    fn light_purple_and_bold() {
        let s = "§1§e§d§lthis will be light purple and bold";
        assert_eq!(
            spans(s),
            vec![Span::new_styled(
                "this will be light purple and bold",
                Color::LightPurple,
                Styles::BOLD
            )]
        );
    }

    #[test]
    fn multiple_styles() {
        let s = "§1§e§d§lthis will be light purple and bold §o§a§e§a§mand this \
                will be green and strikethrough";
        assert_eq!(
            spans(s),
            vec![
                Span::new_styled(
                    "this will be light purple and bold ",
                    Color::LightPurple,
                    Styles::BOLD
                ),
                Span::new_styled(
                    "and this will be green and strikethrough",
                    Color::Green,
                    Styles::STRIKETHROUGH
                )
            ]
        );
    }

    #[test]
    fn multiple_styles_no_colors() {
        let s = "§lthis will be bold §o§mand this will be bold, italic, and strikethrough";
        assert_eq!(
            spans(s),
            vec![
                Span::new_styled("this will be bold ", Color::White, Styles::BOLD),
                Span::new_styled(
                    "and this will be bold, italic, and strikethrough",
                    Color::White,
                    Styles::BOLD | Styles::ITALIC | Styles::STRIKETHROUGH
                )
            ]
        );
    }

    #[test]
    fn colors_and_styles_at_end() {
        let s = "basic stuff but then§o§a§e§a§m";
        assert_eq!(spans(s), vec![Span::new_plain("basic stuff but then")]);
    }

    #[test]
    fn multiline_message() {
        let s = "§8Welcome to §6§lAmazing Minecraft Server\n§8§oYour hub for §d§op2w §8§ogameplay!";
        assert_eq!(
            spans(s),
            vec![
                Span::new_styled("Welcome to ", Color::DarkGray, Styles::empty()),
                Span::new_styled("Amazing Minecraft Server\n", Color::Gold, Styles::BOLD),
                Span::new_styled("Your hub for ", Color::DarkGray, Styles::ITALIC),
                Span::new_styled("p2w ", Color::LightPurple, Styles::ITALIC),
                Span::new_styled("gameplay!", Color::DarkGray, Styles::ITALIC)
            ]
        );
    }

    #[test]
    fn real_motd() {
        let s = " §7§l<§a§l+§7§l>§8§l§m-----§8§l[ §a§lMine§7§lSuperior§a§l Network§8§l ]§8§l§m-----§7§l<§a§l+§7§l>\n\
                §a§l§n1.7-1.16 SUPPORT§r §7§l| §a§lSITE§7§l:§a§l§nwww.minesuperior.com";
        assert_eq!(
            spans(s),
            vec![
                Span::new_plain(" "),
                Span::new_styled("<", Color::Gray, Styles::BOLD),
                Span::new_styled("+", Color::Green, Styles::BOLD),
                Span::new_styled(">", Color::Gray, Styles::BOLD),
                Span::new_styled(
                    "-----",
                    Color::DarkGray,
                    Styles::BOLD | Styles::STRIKETHROUGH
                ),
                Span::new_styled("[ ", Color::DarkGray, Styles::BOLD),
                Span::new_styled("Mine", Color::Green, Styles::BOLD),
                Span::new_styled("Superior", Color::Gray, Styles::BOLD),
                Span::new_styled(" Network", Color::Green, Styles::BOLD),
                Span::new_styled(" ]", Color::DarkGray, Styles::BOLD),
                Span::new_styled(
                    "-----",
                    Color::DarkGray,
                    Styles::BOLD | Styles::STRIKETHROUGH
                ),
                Span::new_styled("<", Color::Gray, Styles::BOLD),
                Span::new_styled("+", Color::Green, Styles::BOLD),
                Span::new_styled(">\n", Color::Gray, Styles::BOLD),
                Span::new_styled(
                    "1.7-1.16 SUPPORT",
                    Color::Green,
                    Styles::BOLD | Styles::UNDERLINED
                ),
                Span::Plain(" "),
                Span::new_styled("| ", Color::Gray, Styles::BOLD),
                Span::new_styled("SITE", Color::Green, Styles::BOLD),
                Span::new_styled(":", Color::Gray, Styles::BOLD),
                Span::new_styled(
                    "www.minesuperior.com",
                    Color::Green,
                    Styles::BOLD | Styles::UNDERLINED
                )
            ]
        );
    }

    #[test]
    fn supports_uppercase_style_codes() {
        let s = "§5§m                  §6>§7§l§6§l>§6§l[§5§l§oPurple §8§l§oPrison§6§l]§6§l<§6<§5§m                     \
                    §R §7              (§4!§7) §e§lSERVER HAS §D§LRESET! §7(§4!§7)";
        assert_eq!(
            spans(s),
            vec![
                // The vanilla client renders whitespace with `Styles::STRIKETHROUGH`
                // as a solid line.
                Span::new_strikethrough_whitespace(18, Color::DarkPurple, Styles::STRIKETHROUGH),
                Span::new_styled(">", Color::Gold, Styles::empty()),
                Span::new_styled(">", Color::Gold, Styles::BOLD),
                Span::new_styled("[", Color::Gold, Styles::BOLD),
                Span::new_styled("Purple ", Color::DarkPurple, Styles::BOLD | Styles::ITALIC),
                Span::new_styled("Prison", Color::DarkGray, Styles::BOLD | Styles::ITALIC),
                Span::new_styled("]", Color::Gold, Styles::BOLD),
                Span::new_styled("<", Color::Gold, Styles::BOLD),
                Span::new_styled("<", Color::Gold, Styles::empty()),
                Span::new_strikethrough_whitespace(21, Color::DarkPurple, Styles::STRIKETHROUGH),
                Span::new_plain(" "),
                Span::new_styled("              (", Color::Gray, Styles::empty()),
                Span::new_styled("!", Color::DarkRed, Styles::empty()),
                Span::new_styled(") ", Color::Gray, Styles::empty()),
                Span::new_styled("SERVER HAS ", Color::Yellow, Styles::BOLD),
                Span::new_styled("RESET! ", Color::LightPurple, Styles::BOLD),
                Span::new_styled("(", Color::Gray, Styles::empty()),
                Span::new_styled("!", Color::DarkRed, Styles::empty()),
                Span::new_styled(")", Color::Gray, Styles::empty()),
            ]
        );
    }

    #[test]
    fn avoids_incorrect_whitespace_strikethrough() {
        let s = "§f§b§lMINE§6§lHEROES §7- §astore.mineheroes.net§a §2§l[75% Sale]\n§b§lSKYBLOCK §f§l+ §2§lKRYPTON §f§lRESET! §f§l- §6§lNEW FALL CRATE";
        assert_eq!(
            spans(s),
            vec![
                Span::new_styled("MINE", Color::Aqua, Styles::BOLD),
                Span::new_styled("HEROES ", Color::Gold, Styles::BOLD),
                Span::new_styled("- ", Color::Gray, Styles::empty()),
                Span::new_styled("store.mineheroes.net", Color::Green, Styles::empty()),
                // A bug in the whitespace strikethrough handling was making this a
                // `Span::WhitespaceStrikethrough`
                Span::new_styled(" ", Color::Green, Styles::empty()),
                Span::new_styled("[75% Sale]\n", Color::DarkGreen, Styles::BOLD),
                Span::new_styled("SKYBLOCK ", Color::Aqua, Styles::BOLD),
                Span::new_styled("+ ", Color::White, Styles::BOLD),
                Span::new_styled("KRYPTON ", Color::DarkGreen, Styles::BOLD),
                Span::new_styled("RESET! ", Color::White, Styles::BOLD),
                Span::new_styled("- ", Color::White, Styles::BOLD),
                Span::new_styled("NEW FALL CRATE", Color::Gold, Styles::BOLD)
            ]
        );
    }
}