twyg 0.6.3

A tiny logging setup for Rust applications
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
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
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
//! Color configuration for log formatting.
//!
//! This module provides fine-grained control over colors for all log components,
//! matching the zylog Go library's color system.

use log::Level;
use owo_colors::{OwoColorize, Stream};
use serde::{Deserialize, Serialize};

/// Color attribute for terminal output.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum ColorAttribute {
    /// No color (transparent/reset)
    #[default]
    Reset,

    // Standard colors
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,

    // Bright/high-intensity colors
    HiBlack, // Grey
    HiRed,
    HiGreen,
    HiYellow,
    HiBlue,
    HiMagenta,
    HiCyan,
    HiWhite,
}

impl ColorAttribute {
    /// Apply this color to a string using owo-colors
    pub(crate) fn apply(&self, text: &str, stream: Stream) -> String {
        match self {
            Self::Reset => text.to_string(),
            Self::Black => text.if_supports_color(stream, |x| x.black()).to_string(),
            Self::Red => text.if_supports_color(stream, |x| x.red()).to_string(),
            Self::Green => text.if_supports_color(stream, |x| x.green()).to_string(),
            Self::Yellow => text.if_supports_color(stream, |x| x.yellow()).to_string(),
            Self::Blue => text.if_supports_color(stream, |x| x.blue()).to_string(),
            Self::Magenta => text.if_supports_color(stream, |x| x.magenta()).to_string(),
            Self::Cyan => text.if_supports_color(stream, |x| x.cyan()).to_string(),
            Self::White => text.if_supports_color(stream, |x| x.white()).to_string(),
            Self::HiBlack => text
                .if_supports_color(stream, |x| x.bright_black())
                .to_string(),
            Self::HiRed => text
                .if_supports_color(stream, |x| x.bright_red())
                .to_string(),
            Self::HiGreen => text
                .if_supports_color(stream, |x| x.bright_green())
                .to_string(),
            Self::HiYellow => text
                .if_supports_color(stream, |x| x.bright_yellow())
                .to_string(),
            Self::HiBlue => text
                .if_supports_color(stream, |x| x.bright_blue())
                .to_string(),
            Self::HiMagenta => text
                .if_supports_color(stream, |x| x.bright_magenta())
                .to_string(),
            Self::HiCyan => text
                .if_supports_color(stream, |x| x.bright_cyan())
                .to_string(),
            Self::HiWhite => text
                .if_supports_color(stream, |x| x.bright_white())
                .to_string(),
        }
    }

    /// Apply as background color
    pub(crate) fn apply_bg(&self, text: &str, stream: Stream) -> String {
        match self {
            Self::Reset => text.to_string(),
            Self::Black => text.if_supports_color(stream, |x| x.on_black()).to_string(),
            Self::Red => text.if_supports_color(stream, |x| x.on_red()).to_string(),
            Self::Green => text.if_supports_color(stream, |x| x.on_green()).to_string(),
            Self::Yellow => text
                .if_supports_color(stream, |x| x.on_yellow())
                .to_string(),
            Self::Blue => text.if_supports_color(stream, |x| x.on_blue()).to_string(),
            Self::Magenta => text
                .if_supports_color(stream, |x| x.on_magenta())
                .to_string(),
            Self::Cyan => text.if_supports_color(stream, |x| x.on_cyan()).to_string(),
            Self::White => text.if_supports_color(stream, |x| x.on_white()).to_string(),
            Self::HiBlack => text
                .if_supports_color(stream, |x| x.on_bright_black())
                .to_string(),
            Self::HiRed => text
                .if_supports_color(stream, |x| x.on_bright_red())
                .to_string(),
            Self::HiGreen => text
                .if_supports_color(stream, |x| x.on_bright_green())
                .to_string(),
            Self::HiYellow => text
                .if_supports_color(stream, |x| x.on_bright_yellow())
                .to_string(),
            Self::HiBlue => text
                .if_supports_color(stream, |x| x.on_bright_blue())
                .to_string(),
            Self::HiMagenta => text
                .if_supports_color(stream, |x| x.on_bright_magenta())
                .to_string(),
            Self::HiCyan => text
                .if_supports_color(stream, |x| x.on_bright_cyan())
                .to_string(),
            Self::HiWhite => text
                .if_supports_color(stream, |x| x.on_bright_white())
                .to_string(),
        }
    }
}

/// Foreground and background color configuration.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Color {
    /// Foreground color
    pub fg: ColorAttribute,

    /// Background color
    pub bg: ColorAttribute,
}

impl Default for Color {
    fn default() -> Self {
        Self {
            fg: ColorAttribute::Reset,
            bg: ColorAttribute::Reset,
        }
    }
}

impl Color {
    /// Create a color with just foreground
    pub fn fg(color: ColorAttribute) -> Self {
        Self {
            fg: color,
            bg: ColorAttribute::Reset,
        }
    }

    /// Create a color with foreground and background
    pub fn new(fg: ColorAttribute, bg: ColorAttribute) -> Self {
        Self { fg, bg }
    }

    /// Apply both foreground and background to text
    pub(crate) fn apply(&self, text: &str, stream: Stream) -> String {
        let with_fg = self.fg.apply(text, stream);
        if self.bg == ColorAttribute::Reset {
            with_fg
        } else {
            self.bg.apply_bg(&with_fg, stream)
        }
    }
}

// Convenience constructors
impl Color {
    pub fn black() -> Self {
        Self::fg(ColorAttribute::Black)
    }
    pub fn red() -> Self {
        Self::fg(ColorAttribute::Red)
    }
    pub fn green() -> Self {
        Self::fg(ColorAttribute::Green)
    }
    pub fn yellow() -> Self {
        Self::fg(ColorAttribute::Yellow)
    }
    pub fn blue() -> Self {
        Self::fg(ColorAttribute::Blue)
    }
    pub fn magenta() -> Self {
        Self::fg(ColorAttribute::Magenta)
    }
    pub fn cyan() -> Self {
        Self::fg(ColorAttribute::Cyan)
    }
    pub fn white() -> Self {
        Self::fg(ColorAttribute::White)
    }
    pub fn hi_black() -> Self {
        Self::fg(ColorAttribute::HiBlack)
    }
    pub fn hi_red() -> Self {
        Self::fg(ColorAttribute::HiRed)
    }
    pub fn hi_green() -> Self {
        Self::fg(ColorAttribute::HiGreen)
    }
    pub fn hi_yellow() -> Self {
        Self::fg(ColorAttribute::HiYellow)
    }
    pub fn hi_blue() -> Self {
        Self::fg(ColorAttribute::HiBlue)
    }
    pub fn hi_magenta() -> Self {
        Self::fg(ColorAttribute::HiMagenta)
    }
    pub fn hi_cyan() -> Self {
        Self::fg(ColorAttribute::HiCyan)
    }
    pub fn hi_white() -> Self {
        Self::fg(ColorAttribute::HiWhite)
    }
}

/// Fine-grained color configuration for all log components.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Colors {
    /// Timestamp color (default: Green to match current behavior)
    #[serde(default = "default_timestamp")]
    pub timestamp: Option<Color>,

    /// TRACE level color (default: HiBlue)
    #[serde(default = "default_level_trace")]
    pub level_trace: Option<Color>,

    /// DEBUG level color (default: Cyan)
    #[serde(default = "default_level_debug")]
    pub level_debug: Option<Color>,

    /// INFO level color (default: HiGreen)
    #[serde(default = "default_level_info")]
    pub level_info: Option<Color>,

    /// WARN level color (default: HiYellow)
    #[serde(default = "default_level_warn")]
    pub level_warn: Option<Color>,

    /// ERROR level color (default: Red)
    #[serde(default = "default_level_error")]
    pub level_error: Option<Color>,

    /// Message text color (default: Green)
    #[serde(default = "default_message")]
    pub message: Option<Color>,

    /// Arrow separator "â–¶" (default: Cyan)
    #[serde(default = "default_arrow")]
    pub arrow: Option<Color>,

    /// Caller file name (default: HiYellow)
    #[serde(default = "default_caller_file")]
    pub caller_file: Option<Color>,

    /// Caller line number (default: HiYellow)
    #[serde(default = "default_caller_line")]
    pub caller_line: Option<Color>,

    /// Target/module name (default: HiYellow)
    #[serde(default = "default_target")]
    pub target: Option<Color>,

    /// Structured logging key (default: HiYellow)
    #[serde(default = "default_attr_key")]
    pub attr_key: Option<Color>,

    /// Structured logging value (default: Cyan)
    #[serde(default = "default_attr_value")]
    pub attr_value: Option<Color>,
}

// Default value functions for serde per-field defaults
fn default_timestamp() -> Option<Color> {
    Some(Color::green())
}
fn default_level_trace() -> Option<Color> {
    Some(Color::hi_blue())
}
fn default_level_debug() -> Option<Color> {
    Some(Color::cyan())
}
fn default_level_info() -> Option<Color> {
    Some(Color::hi_green())
}
fn default_level_warn() -> Option<Color> {
    Some(Color::hi_yellow())
}
fn default_level_error() -> Option<Color> {
    Some(Color::red())
}
fn default_message() -> Option<Color> {
    Some(Color::green())
}
fn default_arrow() -> Option<Color> {
    Some(Color::cyan())
}
fn default_caller_file() -> Option<Color> {
    Some(Color::hi_yellow())
}
fn default_caller_line() -> Option<Color> {
    Some(Color::hi_yellow())
}
fn default_target() -> Option<Color> {
    Some(Color::hi_yellow())
}
fn default_attr_key() -> Option<Color> {
    Some(Color::hi_yellow())
}
fn default_attr_value() -> Option<Color> {
    Some(Color::cyan())
}

impl Default for Colors {
    /// Returns colors matching current twyg behavior
    fn default() -> Self {
        Self {
            timestamp: Some(Color::green()),
            level_trace: Some(Color::hi_blue()),
            level_debug: Some(Color::cyan()),
            level_info: Some(Color::hi_green()),
            level_warn: Some(Color::hi_yellow()),
            level_error: Some(Color::red()),
            message: Some(Color::green()),
            arrow: Some(Color::cyan()),
            caller_file: Some(Color::hi_yellow()),
            caller_line: Some(Color::hi_yellow()),
            target: Some(Color::hi_yellow()),
            attr_key: Some(Color::hi_yellow()),
            attr_value: Some(Color::cyan()),
        }
    }
}

impl Colors {
    /// Get color for a specific log level
    pub(crate) fn level_color(&self, level: Level) -> Option<&Color> {
        match level {
            Level::Error => self.level_error.as_ref(),
            Level::Warn => self.level_warn.as_ref(),
            Level::Info => self.level_info.as_ref(),
            Level::Debug => self.level_debug.as_ref(),
            Level::Trace => self.level_trace.as_ref(),
        }
    }
}

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

    #[test]
    fn test_color_default() {
        let c = Color::default();
        assert_eq!(c.fg, ColorAttribute::Reset);
        assert_eq!(c.bg, ColorAttribute::Reset);
    }

    #[test]
    fn test_color_constructors() {
        let c = Color::red();
        assert_eq!(c.fg, ColorAttribute::Red);
        assert_eq!(c.bg, ColorAttribute::Reset);

        let c2 = Color::new(ColorAttribute::Green, ColorAttribute::Black);
        assert_eq!(c2.fg, ColorAttribute::Green);
        assert_eq!(c2.bg, ColorAttribute::Black);
    }

    #[test]
    fn test_colors_default_matches_current_behavior() {
        let colors = Colors::default();
        assert!(colors.timestamp.is_some());
        assert!(colors.level_error.is_some());
        assert!(colors.level_warn.is_some());
        assert!(colors.level_info.is_some());
        assert!(colors.level_debug.is_some());
        assert!(colors.level_trace.is_some());
        assert!(colors.message.is_some());
        assert!(colors.arrow.is_some());
        assert!(colors.caller_file.is_some());
        assert!(colors.caller_line.is_some());
        assert!(colors.target.is_some());
        assert!(colors.attr_key.is_some());
        assert!(colors.attr_value.is_some());
    }

    #[test]
    fn test_level_color_lookup() {
        let colors = Colors::default();
        assert!(colors.level_color(Level::Error).is_some());
        assert!(colors.level_color(Level::Warn).is_some());
        assert!(colors.level_color(Level::Info).is_some());
        assert!(colors.level_color(Level::Debug).is_some());
        assert!(colors.level_color(Level::Trace).is_some());
    }

    #[test]
    fn test_color_attribute_default() {
        let attr = ColorAttribute::default();
        assert_eq!(attr, ColorAttribute::Reset);
    }

    #[test]
    fn test_convenience_constructors() {
        assert_eq!(Color::black().fg, ColorAttribute::Black);
        assert_eq!(Color::red().fg, ColorAttribute::Red);
        assert_eq!(Color::green().fg, ColorAttribute::Green);
        assert_eq!(Color::yellow().fg, ColorAttribute::Yellow);
        assert_eq!(Color::blue().fg, ColorAttribute::Blue);
        assert_eq!(Color::magenta().fg, ColorAttribute::Magenta);
        assert_eq!(Color::cyan().fg, ColorAttribute::Cyan);
        assert_eq!(Color::white().fg, ColorAttribute::White);
        assert_eq!(Color::hi_black().fg, ColorAttribute::HiBlack);
        assert_eq!(Color::hi_red().fg, ColorAttribute::HiRed);
        assert_eq!(Color::hi_green().fg, ColorAttribute::HiGreen);
        assert_eq!(Color::hi_yellow().fg, ColorAttribute::HiYellow);
        assert_eq!(Color::hi_blue().fg, ColorAttribute::HiBlue);
        assert_eq!(Color::hi_magenta().fg, ColorAttribute::HiMagenta);
        assert_eq!(Color::hi_cyan().fg, ColorAttribute::HiCyan);
        assert_eq!(Color::hi_white().fg, ColorAttribute::HiWhite);
    }

    #[test]
    fn test_color_attribute_apply_all_variants() {
        // Test all ColorAttribute variants
        let text = "test";

        // Reset should return plain text
        assert_eq!(ColorAttribute::Reset.apply(text, Stream::Stdout), "test");

        // Test all standard colors (just verify they return something)
        assert!(!ColorAttribute::Black.apply(text, Stream::Stdout).is_empty());
        assert!(!ColorAttribute::Red.apply(text, Stream::Stdout).is_empty());
        assert!(!ColorAttribute::Green.apply(text, Stream::Stdout).is_empty());
        assert!(!ColorAttribute::Yellow
            .apply(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::Blue.apply(text, Stream::Stdout).is_empty());
        assert!(!ColorAttribute::Magenta
            .apply(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::Cyan.apply(text, Stream::Stdout).is_empty());
        assert!(!ColorAttribute::White.apply(text, Stream::Stdout).is_empty());

        // Test all bright colors
        assert!(!ColorAttribute::HiBlack
            .apply(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::HiRed.apply(text, Stream::Stdout).is_empty());
        assert!(!ColorAttribute::HiGreen
            .apply(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::HiYellow
            .apply(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::HiBlue
            .apply(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::HiMagenta
            .apply(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::HiCyan
            .apply(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::HiWhite
            .apply(text, Stream::Stdout)
            .is_empty());
    }

    #[test]
    fn test_color_attribute_apply_bg_all_variants() {
        // Test all ColorAttribute variants for background
        let text = "test";

        // Reset should return plain text
        assert_eq!(ColorAttribute::Reset.apply_bg(text, Stream::Stdout), "test");

        // Test all standard background colors
        assert!(!ColorAttribute::Black
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::Red
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::Green
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::Yellow
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::Blue
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::Magenta
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::Cyan
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::White
            .apply_bg(text, Stream::Stdout)
            .is_empty());

        // Test all bright background colors
        assert!(!ColorAttribute::HiBlack
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::HiRed
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::HiGreen
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::HiYellow
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::HiBlue
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::HiMagenta
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::HiCyan
            .apply_bg(text, Stream::Stdout)
            .is_empty());
        assert!(!ColorAttribute::HiWhite
            .apply_bg(text, Stream::Stdout)
            .is_empty());
    }

    #[test]
    fn test_color_attribute_with_stderr() {
        // Test that colors work with Stderr stream
        let text = "test";

        assert!(!ColorAttribute::Red.apply(text, Stream::Stderr).is_empty());
        assert!(!ColorAttribute::Green
            .apply_bg(text, Stream::Stderr)
            .is_empty());
        assert!(!ColorAttribute::HiYellow
            .apply(text, Stream::Stderr)
            .is_empty());
    }

    #[test]
    fn test_color_apply_with_background() {
        let color = Color::new(ColorAttribute::White, ColorAttribute::Red);
        let result = color.apply("test", Stream::Stdout);

        // Should return text (with or without colors depending on terminal support)
        assert!(!result.is_empty());
        assert!(result.contains("test"));
    }

    #[test]
    fn test_color_apply_fg_only() {
        let color = Color::fg(ColorAttribute::Green);
        let result = color.apply("test", Stream::Stdout);

        // Should have applied foreground only
        assert!(!result.is_empty());
    }

    #[test]
    fn test_color_apply_reset_bg() {
        // Color with Reset background should only apply foreground
        let color = Color {
            fg: ColorAttribute::Red,
            bg: ColorAttribute::Reset,
        };
        let result = color.apply("test", Stream::Stdout);
        assert!(!result.is_empty());
    }

    #[test]
    fn test_colors_level_color_returns_correct_option() {
        let colors = Colors::default();

        // All default colors should be Some
        assert!(colors.level_color(Level::Error).is_some());
        assert!(colors.level_color(Level::Warn).is_some());
        assert!(colors.level_color(Level::Info).is_some());
        assert!(colors.level_color(Level::Debug).is_some());
        assert!(colors.level_color(Level::Trace).is_some());

        // Test with None colors
        let empty_colors = Colors {
            timestamp: None,
            level_trace: None,
            level_debug: None,
            level_info: None,
            level_warn: None,
            level_error: None,
            message: None,
            arrow: None,
            caller_file: None,
            caller_line: None,
            target: None,
            attr_key: None,
            attr_value: None,
        };

        assert!(empty_colors.level_color(Level::Error).is_none());
        assert!(empty_colors.level_color(Level::Warn).is_none());
        assert!(empty_colors.level_color(Level::Info).is_none());
        assert!(empty_colors.level_color(Level::Debug).is_none());
        assert!(empty_colors.level_color(Level::Trace).is_none());
    }

    #[test]
    fn test_color_eq() {
        let c1 = Color::red();
        let c2 = Color::red();
        let c3 = Color::blue();

        assert_eq!(c1, c2);
        assert_ne!(c1, c3);
    }

    #[test]
    fn test_color_attribute_eq() {
        assert_eq!(ColorAttribute::Red, ColorAttribute::Red);
        assert_ne!(ColorAttribute::Red, ColorAttribute::Blue);
        assert_ne!(ColorAttribute::Red, ColorAttribute::HiRed);
    }

    #[test]
    fn test_color_clone() {
        let c1 = Color::new(ColorAttribute::Red, ColorAttribute::Blue);
        let c2 = c1.clone();
        assert_eq!(c1, c2);
    }

    #[test]
    fn test_color_attribute_clone() {
        let attr = ColorAttribute::HiGreen;
        let cloned = attr.clone();
        assert_eq!(attr, cloned);
    }

    #[test]
    fn test_colors_clone() {
        let colors1 = Colors::default();
        let colors2 = colors1.clone();
        assert_eq!(colors1.timestamp, colors2.timestamp);
        assert_eq!(colors1.level_error, colors2.level_error);
    }

    #[test]
    fn test_color_debug() {
        let color = Color::red();
        let debug_str = format!("{:?}", color);
        assert!(debug_str.contains("Color"));
    }

    #[test]
    fn test_color_attribute_debug() {
        let attr = ColorAttribute::HiYellow;
        let debug_str = format!("{:?}", attr);
        assert!(debug_str.contains("HiYellow"));
    }

    #[test]
    fn test_colors_debug() {
        let colors = Colors::default();
        let debug_str = format!("{:?}", colors);
        assert!(debug_str.contains("Colors"));
    }

    #[test]
    fn test_colors_partial_eq() {
        let c1 = Colors::default();
        let c2 = Colors::default();
        assert_eq!(c1, c2);

        let mut c3 = Colors::default();
        c3.message = None;
        assert_ne!(c1, c3);
    }

    #[test]
    fn test_all_color_convenience_constructors_have_reset_bg() {
        // All convenience constructors should have Reset background
        assert_eq!(Color::black().bg, ColorAttribute::Reset);
        assert_eq!(Color::red().bg, ColorAttribute::Reset);
        assert_eq!(Color::green().bg, ColorAttribute::Reset);
        assert_eq!(Color::yellow().bg, ColorAttribute::Reset);
        assert_eq!(Color::blue().bg, ColorAttribute::Reset);
        assert_eq!(Color::magenta().bg, ColorAttribute::Reset);
        assert_eq!(Color::cyan().bg, ColorAttribute::Reset);
        assert_eq!(Color::white().bg, ColorAttribute::Reset);
        assert_eq!(Color::hi_black().bg, ColorAttribute::Reset);
        assert_eq!(Color::hi_red().bg, ColorAttribute::Reset);
        assert_eq!(Color::hi_green().bg, ColorAttribute::Reset);
        assert_eq!(Color::hi_yellow().bg, ColorAttribute::Reset);
        assert_eq!(Color::hi_blue().bg, ColorAttribute::Reset);
        assert_eq!(Color::hi_magenta().bg, ColorAttribute::Reset);
        assert_eq!(Color::hi_cyan().bg, ColorAttribute::Reset);
        assert_eq!(Color::hi_white().bg, ColorAttribute::Reset);
    }

    #[test]
    fn test_color_new_sets_both_fg_and_bg() {
        let color = Color::new(ColorAttribute::Cyan, ColorAttribute::Magenta);
        assert_eq!(color.fg, ColorAttribute::Cyan);
        assert_eq!(color.bg, ColorAttribute::Magenta);
    }

    #[test]
    fn test_colors_default_has_all_fields_set() {
        let colors = Colors::default();

        // Verify all fields are Some (not None)
        assert!(colors.timestamp.is_some());
        assert!(colors.level_trace.is_some());
        assert!(colors.level_debug.is_some());
        assert!(colors.level_info.is_some());
        assert!(colors.level_warn.is_some());
        assert!(colors.level_error.is_some());
        assert!(colors.message.is_some());
        assert!(colors.arrow.is_some());
        assert!(colors.caller_file.is_some());
        assert!(colors.caller_line.is_some());
        assert!(colors.target.is_some());
        assert!(colors.attr_key.is_some());
        assert!(colors.attr_value.is_some());
    }

    #[test]
    fn test_color_attribute_serialize_deserialize() {
        let attr = ColorAttribute::HiCyan;
        let serialized = serde_json::to_string(&attr).unwrap();
        let deserialized: ColorAttribute = serde_json::from_str(&serialized).unwrap();
        assert_eq!(attr, deserialized);

        // Test Reset
        let reset = ColorAttribute::Reset;
        let serialized = serde_json::to_string(&reset).unwrap();
        let deserialized: ColorAttribute = serde_json::from_str(&serialized).unwrap();
        assert_eq!(reset, deserialized);
    }

    #[test]
    fn test_color_serialize_deserialize() {
        let color = Color::new(ColorAttribute::Red, ColorAttribute::Yellow);
        let serialized = serde_json::to_string(&color).unwrap();
        let deserialized: Color = serde_json::from_str(&serialized).unwrap();
        assert_eq!(color, deserialized);

        // Test with default
        let default_color = Color::default();
        let serialized = serde_json::to_string(&default_color).unwrap();
        let deserialized: Color = serde_json::from_str(&serialized).unwrap();
        assert_eq!(default_color, deserialized);
    }

    #[test]
    fn test_colors_serialize_deserialize() {
        let colors = Colors::default();
        let serialized = serde_json::to_string(&colors).unwrap();
        let deserialized: Colors = serde_json::from_str(&serialized).unwrap();
        assert_eq!(colors, deserialized);

        // Test with some None values
        let partial_colors = Colors {
            timestamp: Some(Color::green()),
            level_trace: None,
            level_debug: Some(Color::cyan()),
            level_info: None,
            level_warn: Some(Color::yellow()),
            level_error: Some(Color::red()),
            message: None,
            arrow: Some(Color::cyan()),
            caller_file: None,
            caller_line: None,
            target: Some(Color::hi_yellow()),
            attr_key: None,
            attr_value: Some(Color::cyan()),
        };
        let serialized = serde_json::to_string(&partial_colors).unwrap();
        let deserialized: Colors = serde_json::from_str(&serialized).unwrap();
        assert_eq!(partial_colors, deserialized);
    }

    #[test]
    fn test_color_apply_multiple_streams() {
        let color = Color::fg(ColorAttribute::Red);

        // Test with both Stdout and Stderr
        let stdout_result = color.apply("test", Stream::Stdout);
        let stderr_result = color.apply("test", Stream::Stderr);

        assert!(!stdout_result.is_empty());
        assert!(!stderr_result.is_empty());
    }

    #[test]
    fn test_all_convenience_constructors_work() {
        // Ensure all convenience constructors create valid Color instances
        let constructors = vec![
            Color::black(),
            Color::red(),
            Color::green(),
            Color::yellow(),
            Color::blue(),
            Color::magenta(),
            Color::cyan(),
            Color::white(),
            Color::hi_black(),
            Color::hi_red(),
            Color::hi_green(),
            Color::hi_yellow(),
            Color::hi_blue(),
            Color::hi_magenta(),
            Color::hi_cyan(),
            Color::hi_white(),
        ];

        for color in constructors {
            // Each should have a non-Reset fg and Reset bg
            assert_ne!(color.fg, ColorAttribute::Reset);
            assert_eq!(color.bg, ColorAttribute::Reset);

            // Each should be able to apply to text
            let result = color.apply("test", Stream::Stdout);
            assert!(!result.is_empty());
        }
    }

    #[test]
    fn test_color_attribute_all_variants_count() {
        // Ensure we're testing all 17 variants (Reset + 16 colors)
        let all_variants = vec![
            ColorAttribute::Reset,
            ColorAttribute::Black,
            ColorAttribute::Red,
            ColorAttribute::Green,
            ColorAttribute::Yellow,
            ColorAttribute::Blue,
            ColorAttribute::Magenta,
            ColorAttribute::Cyan,
            ColorAttribute::White,
            ColorAttribute::HiBlack,
            ColorAttribute::HiRed,
            ColorAttribute::HiGreen,
            ColorAttribute::HiYellow,
            ColorAttribute::HiBlue,
            ColorAttribute::HiMagenta,
            ColorAttribute::HiCyan,
            ColorAttribute::HiWhite,
        ];

        assert_eq!(all_variants.len(), 17);

        // Test each can be serialized
        for variant in all_variants {
            let serialized = serde_json::to_string(&variant).unwrap();
            assert!(!serialized.is_empty());
        }
    }

    #[test]
    fn test_color_default_apply() {
        // Test applying default color (both Reset)
        let default_color = Color::default();
        let result = default_color.apply("test", Stream::Stdout);
        // Should return plain text since both fg and bg are Reset
        assert_eq!(result, "test");
    }

    #[test]
    fn test_color_apply_with_non_reset_bg() {
        // Test that non-Reset background gets applied
        let color = Color::new(ColorAttribute::White, ColorAttribute::Blue);
        let result = color.apply("test", Stream::Stdout);
        assert!(!result.is_empty());
        assert!(result.contains("test"));
    }

    #[test]
    fn test_colors_all_none_values() {
        // Test Colors struct with all None values
        let empty_colors = Colors {
            timestamp: None,
            level_trace: None,
            level_debug: None,
            level_info: None,
            level_warn: None,
            level_error: None,
            message: None,
            arrow: None,
            caller_file: None,
            caller_line: None,
            target: None,
            attr_key: None,
            attr_value: None,
        };

        // Verify all level_color calls return None
        assert!(empty_colors.level_color(Level::Error).is_none());
        assert!(empty_colors.level_color(Level::Warn).is_none());
        assert!(empty_colors.level_color(Level::Info).is_none());
        assert!(empty_colors.level_color(Level::Debug).is_none());
        assert!(empty_colors.level_color(Level::Trace).is_none());

        // Verify it can be serialized/deserialized
        let serialized = serde_json::to_string(&empty_colors).unwrap();
        let deserialized: Colors = serde_json::from_str(&serialized).unwrap();
        assert_eq!(empty_colors, deserialized);
    }

    #[test]
    fn test_color_with_reset_fg_non_reset_bg() {
        // Edge case: Reset foreground with colored background
        let color = Color::new(ColorAttribute::Reset, ColorAttribute::Red);
        let result = color.apply("test", Stream::Stdout);
        assert!(!result.is_empty());
    }

    #[test]
    fn test_all_color_constructors_return_unique_colors() {
        // Verify each constructor creates a distinct color
        let colors = vec![
            Color::black(),
            Color::red(),
            Color::green(),
            Color::yellow(),
            Color::blue(),
            Color::magenta(),
            Color::cyan(),
            Color::white(),
            Color::hi_black(),
            Color::hi_red(),
            Color::hi_green(),
            Color::hi_yellow(),
            Color::hi_blue(),
            Color::hi_magenta(),
            Color::hi_cyan(),
            Color::hi_white(),
        ];

        // Check that not all colors are equal
        for i in 0..colors.len() {
            for j in (i + 1)..colors.len() {
                assert_ne!(colors[i], colors[j]);
            }
        }
    }

    #[test]
    fn test_comprehensive_color_attribute_coverage() {
        // Explicitly test every ColorAttribute variant with both streams
        let test_text = "coverage";
        let all_variants = [
            ColorAttribute::Reset,
            ColorAttribute::Black,
            ColorAttribute::Red,
            ColorAttribute::Green,
            ColorAttribute::Yellow,
            ColorAttribute::Blue,
            ColorAttribute::Magenta,
            ColorAttribute::Cyan,
            ColorAttribute::White,
            ColorAttribute::HiBlack,
            ColorAttribute::HiRed,
            ColorAttribute::HiGreen,
            ColorAttribute::HiYellow,
            ColorAttribute::HiBlue,
            ColorAttribute::HiMagenta,
            ColorAttribute::HiCyan,
            ColorAttribute::HiWhite,
        ];

        for variant in all_variants.iter() {
            // Test foreground with both streams
            let fg_stdout = variant.apply(test_text, Stream::Stdout);
            let fg_stderr = variant.apply(test_text, Stream::Stderr);
            assert!(fg_stdout.contains(test_text));
            assert!(fg_stderr.contains(test_text));

            // Test background with both streams
            let bg_stdout = variant.apply_bg(test_text, Stream::Stdout);
            let bg_stderr = variant.apply_bg(test_text, Stream::Stderr);
            assert!(bg_stdout.contains(test_text));
            assert!(bg_stderr.contains(test_text));

            // Test combined fg+bg through Color struct
            let color = Color::new(*variant, *variant);
            let combined = color.apply(test_text, Stream::Stdout);
            assert!(combined.contains(test_text));
        }
    }

    #[test]
    fn test_color_apply_branch_coverage() {
        // Explicitly test both branches of the if statement in Color::apply

        // Branch 1: bg == ColorAttribute::Reset (only apply fg)
        let color_reset_bg = Color {
            fg: ColorAttribute::Red,
            bg: ColorAttribute::Reset,
        };
        let result1 = color_reset_bg.apply("test", Stream::Stdout);
        assert!(result1.contains("test"));

        // Branch 2: bg != ColorAttribute::Reset (apply both fg and bg)
        let color_with_bg = Color {
            fg: ColorAttribute::Red,
            bg: ColorAttribute::Yellow,
        };
        let result2 = color_with_bg.apply("test", Stream::Stdout);
        assert!(result2.contains("test"));

        // Additional edge cases
        let color_both_reset = Color {
            fg: ColorAttribute::Reset,
            bg: ColorAttribute::Reset,
        };
        assert_eq!(color_both_reset.apply("test", Stream::Stdout), "test");

        let color_reset_fg = Color {
            fg: ColorAttribute::Reset,
            bg: ColorAttribute::Blue,
        };
        let result3 = color_reset_fg.apply("test", Stream::Stdout);
        assert!(result3.contains("test"));
    }

    #[test]
    fn test_colors_partial_deserialization_preserves_defaults() {
        // Test that deserializing a partial Colors config preserves defaults for unspecified fields
        // This is the key fix: per-field #[serde(default)] ensures missing fields get defaults

        let toml = r#"
            [timestamp]
            fg = "HiBlack"
            bg = "Reset"
        "#;

        let colors: Colors = toml::from_str(toml).expect("Failed to deserialize");

        // Specified field should use config value
        assert_eq!(
            colors.timestamp,
            Some(Color::new(ColorAttribute::HiBlack, ColorAttribute::Reset))
        );

        // Unspecified fields should use defaults (not None!)
        assert_eq!(
            colors.level_info,
            Some(Color::hi_green()),
            "level_info should have default"
        );
        assert_eq!(
            colors.level_error,
            Some(Color::red()),
            "level_error should have default"
        );
        assert_eq!(
            colors.message,
            Some(Color::green()),
            "message should have default"
        );
        assert_eq!(
            colors.arrow,
            Some(Color::cyan()),
            "arrow should have default"
        );
        assert_eq!(
            colors.target,
            Some(Color::hi_yellow()),
            "target should have default"
        );

        // All fields should be Some
        assert!(colors.level_trace.is_some());
        assert!(colors.level_debug.is_some());
        assert!(colors.level_warn.is_some());
        assert!(colors.caller_file.is_some());
        assert!(colors.caller_line.is_some());
        assert!(colors.attr_key.is_some());
        assert!(colors.attr_value.is_some());
    }
}