lumis-core 0.0.3

Lumis shared core
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
//! Theme system for syntax highlighting.
//!
//! This module provides access to Neovim-based color themes for syntax highlighting.
//! Themes define colors and styling for different syntax elements like keywords,
//! strings, comments, etc.

use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, fs, path::Path, str::FromStr};

/// Error type for theme operations.
#[derive(Debug, Clone)]
pub enum ThemeError {
    /// Theme not found
    NotFound(String),
    /// Invalid theme JSON
    InvalidJson(String),
    /// Theme file not found
    FileNotFound(String),
    /// Theme file read error
    FileReadError(String),
}

impl std::fmt::Display for ThemeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ThemeError::NotFound(name) => write!(f, "theme '{name}' not found"),
            ThemeError::InvalidJson(msg) => write!(f, "invalid theme json: {msg}"),
            ThemeError::FileNotFound(path) => write!(f, "theme file not found: {path}"),
            ThemeError::FileReadError(msg) => write!(f, "failed to read theme file: {msg}"),
        }
    }
}

impl std::error::Error for ThemeError {}

/// Error type returned when parsing a theme from a string fails.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ThemeParseError(String);

impl std::fmt::Display for ThemeParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "unknown theme: {}", self.0)
    }
}

impl std::error::Error for ThemeParseError {}

impl From<std::io::Error> for ThemeError {
    fn from(err: std::io::Error) -> Self {
        if err.kind() == std::io::ErrorKind::NotFound {
            ThemeError::FileNotFound(err.to_string())
        } else {
            ThemeError::FileReadError(err.to_string())
        }
    }
}

impl From<serde_json::Error> for ThemeError {
    fn from(err: serde_json::Error) -> Self {
        ThemeError::InvalidJson(err.to_string())
    }
}

/// Underline style for text decoration.
///
/// Corresponds to Neovim's underline variants:
/// - `Solid` - standard underline (`underline` in Neovim)
/// - `Wavy` - wavy/curly underline (`undercurl` in Neovim)
/// - `Double` - double underline (`underdouble` in Neovim)
/// - `Dotted` - dotted underline (`underdotted` in Neovim)
/// - `Dashed` - dashed underline (`underdashed` in Neovim)
#[derive(Default, Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum UnderlineStyle {
    #[default]
    None,
    Solid,
    Wavy,
    Double,
    Dotted,
    Dashed,
}

/// Text decoration combining underline style and strikethrough.
#[derive(Default, Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct TextDecoration {
    /// The underline style to apply.
    #[serde(default)]
    pub underline: UnderlineStyle,
    /// Whether to apply strikethrough.
    #[serde(default)]
    pub strikethrough: bool,
}

/// The visual appearance of a theme.
///
/// Themes are categorized as either light (dark text on light background) or
/// dark (light text on dark background).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Appearance {
    /// Light theme (dark text on light background)
    Light,
    /// Dark theme (light text on dark background)
    #[default]
    Dark,
}

impl std::fmt::Display for Appearance {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Appearance::Light => write!(f, "light"),
            Appearance::Dark => write!(f, "dark"),
        }
    }
}

/// A theme for syntax highlighting.
///
/// A theme consists of a name, appearance (light/dark), revision (commit) and a collection
/// of highlight styles mapped to their scope names.
///
/// # Examples
///
/// Loading a theme by name:
///
/// ```
/// use lumis_core::themes::{self, Theme, Appearance};
///
/// let theme = themes::get("github_light").expect("Theme not found");
/// assert_eq!(theme.appearance, Appearance::Light);
///
/// let theme: Theme = "dracula".parse().expect("Theme not found");
/// assert_eq!(theme.name, "dracula");
/// ```
///
/// Creating a theme programmatically:
///
/// ```
/// use lumis_core::themes::{Theme, Style, Appearance};
/// use std::collections::BTreeMap;
///
/// let mut highlights = BTreeMap::new();
/// highlights.insert("keyword".to_string(), Style {
///     fg: Some("#ff79c6".to_string()),
///     bold: true,
///     ..Default::default()
/// });
///
/// let theme = Theme::new(
///     "my_theme".to_string(),
///     Appearance::Dark,
///     "3e976b4".to_string(),
///     highlights
/// );
/// ```
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct Theme {
    /// The name of the theme.
    pub name: String,
    /// The appearance of the theme (light or dark).
    pub appearance: Appearance,
    /// The commit of the theme plugin
    pub revision: String,
    /// A map of highlight scope names to their styles.
    pub highlights: BTreeMap<String, Style>,
}

/// A style for syntax highlighting.
///
/// Defines the visual appearance of a highlight scope, including colors,
/// font weight, and text decoration.
///
/// # Examples
///
/// ```
/// use lumis_core::themes::{Style, TextDecoration, UnderlineStyle};
///
/// let style = Style {
///     fg: Some("#ff79c6".to_string()),
///     bold: true,
///     ..Default::default()
/// };
///
/// let style = Style {
///     text_decoration: TextDecoration {
///         underline: UnderlineStyle::Wavy,
///         strikethrough: true,
///     },
///     ..Default::default()
/// };
/// ```
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Style {
    /// The foreground color in hex format (e.g., "#ff79c6").
    pub fg: Option<String>,
    /// The background color in hex format (e.g., "#282a36").
    pub bg: Option<String>,
    /// Whether to make the text bold.
    pub bold: bool,
    /// Whether to make the text italic.
    pub italic: bool,
    /// Text decoration (underline style and strikethrough).
    pub text_decoration: TextDecoration,
}

/// Deserializes underline from either a boolean (`true` = solid) or a string (`"solid"`, `"wavy"`, etc.).
fn deserialize_underline_field<'de, D>(deserializer: D) -> Result<UnderlineStyle, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde::de;

    struct UnderlineVisitor;

    impl<'de> de::Visitor<'de> for UnderlineVisitor {
        type Value = UnderlineStyle;

        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("a boolean or underline style string")
        }

        fn visit_bool<E: de::Error>(self, v: bool) -> Result<UnderlineStyle, E> {
            Ok(if v {
                UnderlineStyle::Solid
            } else {
                UnderlineStyle::None
            })
        }

        fn visit_str<E: de::Error>(self, v: &str) -> Result<UnderlineStyle, E> {
            match v {
                "solid" => Ok(UnderlineStyle::Solid),
                "wavy" | "undercurl" => Ok(UnderlineStyle::Wavy),
                "double" => Ok(UnderlineStyle::Double),
                "dotted" => Ok(UnderlineStyle::Dotted),
                "dashed" => Ok(UnderlineStyle::Dashed),
                "none" => Ok(UnderlineStyle::None),
                _ => Err(de::Error::unknown_variant(
                    v,
                    &["solid", "wavy", "double", "dotted", "dashed", "none"],
                )),
            }
        }
    }

    deserializer.deserialize_any(UnderlineVisitor)
}

/// Helper struct for deserializing Style from JSON.
///
/// Accepts two formats:
/// - Old format: separate bool fields (`underline: true`, `undercurl: true`, etc.)
/// - New format: single `underline` field with string value (`"solid"`, `"wavy"`, etc.)
#[derive(Deserialize)]
struct StyleHelper {
    #[serde(default)]
    fg: Option<String>,
    #[serde(default)]
    bg: Option<String>,
    #[serde(default)]
    bold: bool,
    #[serde(default)]
    italic: bool,
    #[serde(default, deserialize_with = "deserialize_underline_field")]
    underline: UnderlineStyle,
    #[serde(default)]
    undercurl: bool,
    #[serde(default)]
    underdouble: bool,
    #[serde(default)]
    underdotted: bool,
    #[serde(default)]
    underdashed: bool,
    #[serde(default)]
    strikethrough: bool,
}

impl<'de> Deserialize<'de> for Style {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let helper = StyleHelper::deserialize(deserializer)?;

        // Old-format bool fields take precedence if set, otherwise use the underline field
        let underline = if helper.undercurl {
            UnderlineStyle::Wavy
        } else if helper.underdouble {
            UnderlineStyle::Double
        } else if helper.underdotted {
            UnderlineStyle::Dotted
        } else if helper.underdashed {
            UnderlineStyle::Dashed
        } else {
            helper.underline
        };

        Ok(Style {
            fg: helper.fg,
            bg: helper.bg,
            bold: helper.bold,
            italic: helper.italic,
            text_decoration: TextDecoration {
                underline,
                strikethrough: helper.strikethrough,
            },
        })
    }
}

impl Serialize for Style {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeStruct;

        let mut count = 0;
        if self.fg.is_some() {
            count += 1;
        }
        if self.bg.is_some() {
            count += 1;
        }
        if self.bold {
            count += 1;
        }
        if self.italic {
            count += 1;
        }
        if self.text_decoration.underline != UnderlineStyle::None {
            count += 1;
        }
        if self.text_decoration.strikethrough {
            count += 1;
        }

        let mut state = serializer.serialize_struct("Style", count)?;

        if let Some(fg) = &self.fg {
            state.serialize_field("fg", fg)?;
        }
        if let Some(bg) = &self.bg {
            state.serialize_field("bg", bg)?;
        }
        if self.bold {
            state.serialize_field("bold", &true)?;
        }
        if self.italic {
            state.serialize_field("italic", &true)?;
        }
        match self.text_decoration.underline {
            UnderlineStyle::None => {}
            UnderlineStyle::Solid => state.serialize_field("underline", &true)?,
            UnderlineStyle::Wavy => state.serialize_field("undercurl", &true)?,
            UnderlineStyle::Double => state.serialize_field("underdouble", &true)?,
            UnderlineStyle::Dotted => state.serialize_field("underdotted", &true)?,
            UnderlineStyle::Dashed => state.serialize_field("underdashed", &true)?,
        }
        if self.text_decoration.strikethrough {
            state.serialize_field("strikethrough", &true)?;
        }

        state.end()
    }
}

/// Load a theme from a JSON file.
///
/// Reads a theme definition from a JSON file and parses it into a [`Theme`].
/// The JSON file should contain theme metadata (name, appearance, revision) and
/// highlight style definitions for various syntax scopes.
///
/// # Errors
///
/// Returns [`ThemeError::FileNotFound`] if the file doesn't exist,
/// [`ThemeError::FileReadError`] if the file can't be read,
/// or [`ThemeError::InvalidJson`] if the JSON is malformed.
///
/// # Examples
///
/// ```rust,no_run
/// use lumis_core::themes;
///
/// let theme = themes::from_file("themes/my_theme.json")
///     .expect("Failed to load theme");
/// println!("Loaded theme: {} ({})", theme.name, theme.appearance);
/// ```
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Theme, ThemeError> {
    let path = path.as_ref();
    let json = fs::read_to_string(path).map_err(|e| {
        if e.kind() == std::io::ErrorKind::NotFound {
            ThemeError::FileNotFound(path.display().to_string())
        } else {
            ThemeError::FileReadError(e.to_string())
        }
    })?;

    from_json(&json)
}

/// Parse a theme from a JSON string.
///
/// Parses a JSON string containing a theme definition and creates a [`Theme`].
/// The JSON must contain required fields (name, appearance, revision) and
/// highlight style definitions.
///
/// # Errors
///
/// Returns [`ThemeError::InvalidJson`] if the JSON is malformed or if
/// required fields (name, revision) are empty.
///
/// # Examples
///
/// ```rust
/// use lumis_core::themes;
///
/// let json = r##"{
///     "name": "my_theme",
///     "appearance": "dark",
///     "revision": "v1.0.0",
///     "highlights": {
///         "keyword": { "fg": "#ff79c6", "bold": true },
///         "string": { "fg": "#f1fa8c" }
///     }
/// }"##;
///
/// let theme = themes::from_json(json).expect("Failed to parse theme");
/// assert_eq!(theme.name, "my_theme");
/// ```
pub fn from_json(json: &str) -> Result<Theme, ThemeError> {
    let theme: Theme = serde_json::from_str(json)?;

    // Validate required fields
    if theme.name.is_empty() {
        return Err(ThemeError::InvalidJson("theme name cannot be empty".into()));
    }
    if theme.revision.is_empty() {
        return Err(ThemeError::InvalidJson(
            "theme revision cannot be empty".into(),
        ));
    }

    Ok(theme)
}

impl FromStr for Theme {
    type Err = ThemeParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        get(s).map_err(|_| ThemeParseError(s.to_string()))
    }
}

impl Theme {
    pub fn new(
        name: String,
        appearance: Appearance,
        revision: String,
        highlights: BTreeMap<String, Style>,
    ) -> Self {
        Theme {
            name,
            appearance,
            revision,
            highlights,
        }
    }

    pub fn css(&self, enable_italic: bool) -> String {
        let mut rules = Vec::new();

        rules.push(format!(
            "/* {}\n * revision: {}\n */\n\npre.lumis",
            self.name, self.revision
        ));

        if let Some(pre_style) = &self.pre_style("\n  ") {
            rules.push(format!(" {{\n  {pre_style}\n}}\n"));
        } else {
            rules.push(" {}\n".to_string());
        }

        for (scope, style) in &self.highlights {
            let style_css = style.css(enable_italic, "\n  ");

            if !style_css.is_empty() {
                rules.push(format!(
                    ".{} {{\n  {}\n}}\n",
                    scope.replace('.', "-"),
                    style_css
                ))
            };
        }

        rules.join("")
    }

    /// Get style for a scope.
    ///
    /// Implements Neovim's treesitter-highlight-groups scope hierarchy walk-up:
    /// tries exact match first, then walks up parent scopes
    /// (e.g., "markup.heading.2.markdown" -> "markup.heading.2" -> "markup.heading").
    ///
    /// For language-specific styles, callers should construct the full scope themselves
    /// (e.g., "comment.lua" instead of "comment"). If the specialized scope doesn't exist,
    /// it automatically falls back to parent scopes.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lumis_core::themes;
    ///
    /// let theme = themes::get("catppuccin_mocha").unwrap();
    ///
    /// // Request specialized scope - falls back to parent if not found
    /// let style = theme.get_style("comment.lua");
    ///
    /// // Request generic scope
    /// let style = theme.get_style("keyword");
    /// ```
    pub fn get_style(&self, scope: &str) -> Option<&Style> {
        let mut current = scope;
        loop {
            if let Some(style) = self.highlights.get(current) {
                return Some(style);
            }
            match current.rsplit_once('.') {
                Some((parent, _)) => current = parent,
                None => return None,
            }
        }
    }

    pub fn fg(&self) -> Option<&str> {
        self.get_style("normal").and_then(|s| s.fg.as_deref())
    }

    pub fn bg(&self) -> Option<&str> {
        self.get_style("normal").and_then(|s| s.bg.as_deref())
    }

    pub fn pre_style(&self, separator: &str) -> Option<String> {
        let mut rules = Vec::new();

        if let Some(fg) = self.fg() {
            rules.push(format!("color: {fg};"));
        }

        if let Some(bg) = self.bg() {
            rules.push(format!("background-color: {bg};"));
        }

        if rules.is_empty() {
            None
        } else {
            Some(rules.join(separator))
        }
    }
}

impl Style {
    pub fn css(&self, enable_italic: bool, separator: &str) -> String {
        let mut rules = Vec::new();

        if let Some(fg) = &self.fg {
            rules.push(format!("color: {fg};"))
        };

        if let Some(bg) = &self.bg {
            rules.push(format!("background-color: {bg};"))
        };

        if self.bold {
            rules.push("font-weight: bold;".to_string())
        }

        if enable_italic && self.italic {
            rules.push("font-style: italic;".to_string())
        };

        let td = &self.text_decoration;
        let underline_css = match td.underline {
            UnderlineStyle::None => None,
            UnderlineStyle::Solid => Some("underline"),
            UnderlineStyle::Wavy => Some("underline wavy"),
            UnderlineStyle::Double => Some("underline double"),
            UnderlineStyle::Dotted => Some("underline dotted"),
            UnderlineStyle::Dashed => Some("underline dashed"),
        };

        match (underline_css, td.strikethrough) {
            (Some(u), true) => rules.push(format!("text-decoration: {u} line-through;")),
            (Some(u), false) => rules.push(format!("text-decoration: {u};")),
            (None, true) => rules.push("text-decoration: line-through;".to_string()),
            (None, false) => (),
        };

        rules.join(separator)
    }
}

include!(concat!(env!("OUT_DIR"), "/theme_data.rs"));

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

    #[test]
    fn test_available_themes() {
        let themes: Vec<_> = available_themes().collect();

        assert!(!themes.is_empty());

        let dracula = themes.iter().find(|t| t.name == "dracula").unwrap();
        assert_eq!(dracula.name, "dracula");
        assert_eq!(dracula.appearance, Appearance::Dark);

        let github_light = themes.iter().find(|t| t.name == "github_light").unwrap();
        assert_eq!(github_light.name, "github_light");
        assert_eq!(github_light.appearance, Appearance::Light);

        for theme in themes {
            assert!(!theme.name.is_empty());
            assert!(theme.appearance == Appearance::Light || theme.appearance == Appearance::Dark);
        }
    }

    #[test]
    fn test_load_all_themes() {
        for theme in ALL_THEMES.iter() {
            assert!(!theme.name.is_empty());
        }

        let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
        let expected_count = std::fs::read_dir(manifest_dir.join("themes"))
            .unwrap()
            .filter_map(|entry| entry.ok())
            .filter(|entry| entry.path().extension().and_then(|s| s.to_str()) == Some("json"))
            .count();

        assert_eq!(ALL_THEMES.len(), expected_count);
    }

    #[test]
    fn test_get_by_name() {
        let theme = get("github_light").expect("Theme not found");
        assert_eq!(theme.name, "github_light");

        let err = get("non_existent_theme");
        assert!(err.is_err());
    }

    #[test]
    fn test_from_json() {
        let json = r#"{"name": "test", "appearance": "dark", "revision": "3e976b4", "highlights": {"keyword": {"fg": "blue"}}}"#;
        let theme = from_json(json).unwrap();

        assert_eq!(theme.name, "test");

        assert_eq!(
            theme.get_style("keyword"),
            Some(&Style {
                fg: Some("blue".to_string()),
                ..Default::default()
            })
        );
    }

    #[test]
    fn test_from_file() {
        let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
        let path = manifest_dir.join("themes/catppuccin_frappe.json");
        let theme = from_file(&path).unwrap();

        assert_eq!(theme.name, "catppuccin_frappe");

        assert_eq!(
            theme.get_style("tag.attribute"),
            Some(&Style {
                fg: Some("#e5c890".to_string()),
                italic: true,
                ..Default::default()
            })
        );
    }

    #[test]
    fn test_style_css() {
        let style = Style {
            fg: Some("blue".to_string()),
            italic: true,
            text_decoration: TextDecoration {
                underline: UnderlineStyle::Solid,
                strikethrough: false,
            },
            ..Default::default()
        };

        assert_eq!(
            style.css(true, " "),
            "color: blue; font-style: italic; text-decoration: underline;"
        );
    }

    #[test]
    fn test_theme_css() {
        let json = r#"{"name": "test", "appearance": "dark", "revision": "3e976b4", "highlights": {"normal": {"fg": "red", "bg": "green"}, "keyword": {"fg": "blue", "italic": true}, "tag.attribute": {"bg": "gray", "bold": true}}}"#;
        let theme = from_json(json).unwrap();

        let expected = r#"/* test
 * revision: 3e976b4
 */

pre.lumis {
  color: red;
  background-color: green;
}
.keyword {
  color: blue;
  font-style: italic;
}
.normal {
  color: red;
  background-color: green;
}
.tag-attribute {
  background-color: gray;
  font-weight: bold;
}
"#;

        assert_eq!(theme.css(true), expected);
    }

    #[test]
    fn test_get_style_specialized() {
        let json = r##"{
            "name": "test",
            "appearance": "dark",
            "revision": "test",
            "highlights": {
                "comment": {"fg": "#666666"},
                "comment.lua": {"fg": "#888888", "italic": true},
                "markup.heading.2": {"fg": "#ff0000", "bold": true},
                "markup.heading.2.markdown": {"fg": "#00ff00", "bold": true}
            }
        }"##;
        let theme = from_json(json).unwrap();

        let lua_comment = theme.get_style("comment.lua");
        assert_eq!(
            lua_comment,
            Some(&Style {
                fg: Some("#888888".to_string()),
                italic: true,
                ..Default::default()
            })
        );

        let md_heading = theme.get_style("markup.heading.2.markdown");
        assert_eq!(
            md_heading,
            Some(&Style {
                fg: Some("#00ff00".to_string()),
                bold: true,
                ..Default::default()
            })
        );
    }

    #[test]
    fn test_get_style_fallback_to_generic() {
        let json = r##"{
            "name": "test",
            "appearance": "dark",
            "revision": "test",
            "highlights": {
                "comment": {"fg": "#666666"},
                "markup.heading.2": {"fg": "#ff0000", "bold": true}
            }
        }"##;
        let theme = from_json(json).unwrap();

        let rust_comment = theme.get_style("comment.rust");
        assert_eq!(
            rust_comment,
            Some(&Style {
                fg: Some("#666666".to_string()),
                ..Default::default()
            })
        );

        let md_heading = theme.get_style("markup.heading.2.markdown");
        assert_eq!(
            md_heading,
            Some(&Style {
                fg: Some("#ff0000".to_string()),
                bold: true,
                ..Default::default()
            })
        );
    }

    #[test]
    fn test_get_style_fallback_to_parent_scope() {
        let json = r##"{
            "name": "test",
            "appearance": "dark",
            "revision": "test",
            "highlights": {
                "markup.heading": {"fg": "#ff0000", "bold": true}
            }
        }"##;
        let theme = from_json(json).unwrap();

        let md_heading = theme.get_style("markup.heading.2.markdown");
        assert_eq!(
            md_heading,
            Some(&Style {
                fg: Some("#ff0000".to_string()),
                bold: true,
                ..Default::default()
            })
        );
    }

    #[test]
    fn test_get_style_no_match() {
        let json = r##"{
            "name": "test",
            "appearance": "dark",
            "revision": "test",
            "highlights": {
                "keyword": {"fg": "#ff0000"}
            }
        }"##;
        let theme = from_json(json).unwrap();

        let result = theme.get_style("comment.rust");
        assert!(result.is_none());
    }

    #[test]
    fn test_get_style_prefers_specialized_over_generic() {
        let json = r##"{
            "name": "test",
            "appearance": "dark",
            "revision": "test",
            "highlights": {
                "string.special.symbol": {"fg": "#aaaaaa"},
                "string.special.symbol.ruby": {"fg": "#bbbbbb", "bold": true},
                "string.special.symbol.elixir": {"fg": "#cccccc", "italic": true}
            }
        }"##;
        let theme = from_json(json).unwrap();

        let ruby_symbol = theme.get_style("string.special.symbol.ruby");
        assert_eq!(
            ruby_symbol,
            Some(&Style {
                fg: Some("#bbbbbb".to_string()),
                bold: true,
                ..Default::default()
            })
        );

        let elixir_symbol = theme.get_style("string.special.symbol.elixir");
        assert_eq!(
            elixir_symbol,
            Some(&Style {
                fg: Some("#cccccc".to_string()),
                italic: true,
                ..Default::default()
            })
        );

        let python_symbol = theme.get_style("string.special.symbol.python");
        assert_eq!(
            python_symbol,
            Some(&Style {
                fg: Some("#aaaaaa".to_string()),
                ..Default::default()
            })
        );
    }

    #[test]
    fn test_get_style_real_world_scenario() {
        let json = r##"{
            "name": "test",
            "appearance": "dark",
            "revision": "test",
            "highlights": {
                "markup.heading": {"fg": "#ff6b6b"},
                "markup.heading.1": {"fg": "#ff0000", "bold": true},
                "markup.heading.2": {"fg": "#00ff00", "bold": true},
                "markup.heading.2.markdown": {"fg": "#4ecdc4", "bold": true, "underline": true},
                "string.special.symbol": {"fg": "#ffeaa7"},
                "string.special.symbol.elixir": {"fg": "#fab1a0", "italic": true}
            }
        }"##;
        let theme = from_json(json).unwrap();

        let markdown_h2 = theme.get_style("markup.heading.2.markdown");
        assert_eq!(
            markdown_h2,
            Some(&Style {
                fg: Some("#4ecdc4".to_string()),
                bold: true,
                text_decoration: TextDecoration {
                    underline: UnderlineStyle::Solid,
                    strikethrough: false,
                },
                ..Default::default()
            })
        );

        let elixir_atom = theme.get_style("string.special.symbol.elixir");
        assert_eq!(
            elixir_atom,
            Some(&Style {
                fg: Some("#fab1a0".to_string()),
                italic: true,
                ..Default::default()
            })
        );

        let rust_symbol = theme.get_style("string.special.symbol.rust");
        assert_eq!(
            rust_symbol,
            Some(&Style {
                fg: Some("#ffeaa7".to_string()),
                ..Default::default()
            })
        );
    }
}