Simplify types and update `microserde` crate

d6b25b6c9eaaa4fb2686dfbdd3efdb1f90750517
Alexis Sellier committed ago 1 parent 5b01e6e5
Cargo.lock +4 -4
30 30
source = "registry+https://github.com/rust-lang/crates.io-index"
31 31
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
32 32
33 33
[[package]]
34 34
name = "microserde"
35 -
version = "0.1.13"
35 +
version = "0.2.0"
36 36
source = "registry+https://github.com/rust-lang/crates.io-index"
37 -
checksum = "bc73d64402c57ce913a4a4788ca4e0c60329a2e29782f8e88d510c1c951fac46"
37 +
checksum = "bcec4fadec845fca55c475f81f7adabe9151b7e16c1d090d95f4eed29ff0412c"
38 38
dependencies = [
39 39
 "microserde-derive",
40 40
]
41 41
42 42
[[package]]
43 43
name = "microserde-derive"
44 -
version = "0.1.13"
44 +
version = "0.2.0"
45 45
source = "registry+https://github.com/rust-lang/crates.io-index"
46 -
checksum = "6e3afa8ee462bb9ed735b544476a9e4ece0f10be76864ff9b80a6a525b837232"
46 +
checksum = "ab91f2f9f9c8079c5ebab098deb191eaff87107b2d044041520a499b0af1e08c"
47 47
dependencies = [
48 48
 "proc-macro2",
49 49
 "quote",
50 50
 "syn",
51 51
]
Cargo.toml +1 -1
38 38
edition = "2024"
39 39
license = "MIT"
40 40
41 41
[workspace.dependencies]
42 42
onig = { package = "onig-regset", version = "6.7.0", default-features = false }
43 -
serde = { package = "microserde", version = "0.1.13" }
43 +
serde = { package = "microserde", version = "0.2.0" }
44 44
termion = "4"
45 45
rustik-highlight = { path = "crates/highlight", version = "0.1.0" }
46 46
rustik-diff = { path = "crates/diff", version = "0.1.0" }
47 47
rustik-tui = { path = "crates/tui", version = "0.1.0" }
48 48
rustik-tui-core = { path = "crates/tui-core", version = "0.1.0" }
crates/diff/src/bin/rustik-diff.rs +3 -3
83 83
    }
84 84
85 85
    /// Parses this grammar and attaches local path aliases.
86 86
    fn grammar(&self) -> Result<syntax::Grammar, syntax::Error> {
87 87
        let mut raw: syntax::RawGrammar = self.source.parse()?;
88 -
        let file_types = raw.file_types.get_or_insert_with(Vec::new);
89 88
90 89
        for file_type in self.file_types {
91 -
            if !file_types
90 +
            if !raw
91 +
                .file_types
92 92
                .iter()
93 93
                .any(|existing| existing.eq_ignore_ascii_case(file_type))
94 94
            {
95 -
                file_types.push((*file_type).to_owned());
95 +
                raw.file_types.push((*file_type).to_owned());
96 96
            }
97 97
        }
98 98
        syntax::Grammar::compile(&raw)
99 99
    }
100 100
}
+0 -0
+0 -0
a.rs +0 -0
crates/highlight/src/grammar.rs +0 -2
121 121
        Ok(Self {
122 122
            name: raw.display_name.clone().unwrap_or_else(|| raw.name.clone()),
123 123
            scope_name: raw.scope_name.clone(),
124 124
            file_types: raw
125 125
                .file_types
126 -
                .as_deref()
127 -
                .unwrap_or_default()
128 126
                .iter()
129 127
                .chain(std::iter::once(&raw.name))
130 128
                .map(|item| key(item))
131 129
                .collect(),
132 130
            head_match,
crates/highlight/src/grammar/pattern.rs +7 -18
186 186
        self.ids.insert(scope.to_owned(), id);
187 187
        id
188 188
    }
189 189
190 190
    /// Converts raw capture maps into interned capture scopes.
191 -
    fn captures(&mut self, raw: Option<&BTreeMap<String, RawCapture>>) -> Captures {
192 -
        raw.into_iter()
193 -
            .flat_map(BTreeMap::iter)
191 +
    fn captures(&mut self, raw: &BTreeMap<String, RawCapture>) -> Captures {
192 +
        raw.iter()
194 193
            .filter_map(|(index, capture)| {
195 194
                let index = index.parse().ok()?;
196 195
                let name = capture.name.as_ref()?;
197 196
                let scope = self.intern(name);
198 197
                Some((index, scope))
318 317
        return Ok(set.patterns);
319 318
    }
320 319
    let Some(name) = include.strip_prefix('#') else {
321 320
        return Ok(Vec::new());
322 321
    };
323 -
    let Some(pattern) = raw
324 -
        .repository
325 -
        .as_ref()
326 -
        .and_then(|repository| repository.get(name))
327 -
    else {
322 +
    let Some(pattern) = raw.repository.get(name) else {
328 323
        return Ok(Vec::new());
329 324
    };
330 325
    let set = PatternSet::compile(
331 326
        raw,
332 327
        std::slice::from_ref(pattern),
349 344
        next_rule: &mut usize,
350 345
        depth: usize,
351 346
        interner: &mut ScopeInterner,
352 347
    ) -> Result<Compiled, Error> {
353 348
        let scope = Scope::from_raw(pattern.name.as_deref(), interner);
354 -
        let nested = PatternSet::compile(
355 -
            raw,
356 -
            pattern.patterns.as_deref().unwrap_or_default(),
357 -
            next_rule,
358 -
            depth + 1,
359 -
            interner,
360 -
        )?;
349 +
        let nested = PatternSet::compile(raw, &pattern.patterns, next_rule, depth + 1, interner)?;
361 350
362 351
        let body = match (&pattern.match_rule, &pattern.begin, &pattern.end) {
363 352
            (Some(source), _, _) => {
364 353
                if Regex::new(source).is_err() {
365 354
                    return Ok(Compiled::Skip);
387 376
388 377
        Ok(Compiled::Rule(Self {
389 378
            id,
390 379
            scope,
391 380
            captures: PatternCaptures {
392 -
                matched: interner.captures(pattern.captures.as_ref()),
393 -
                begin: interner.captures(pattern.begin_captures.as_ref()),
394 -
                end: interner.captures(pattern.end_captures.as_ref()),
381 +
                matched: interner.captures(&pattern.captures),
382 +
                begin: interner.captures(&pattern.begin_captures),
383 +
                end: interner.captures(&pattern.end_captures),
395 384
            },
396 385
            body,
397 386
            nested,
398 387
        }))
399 388
    }
crates/highlight/src/raw.rs +17 -8
22 22
    pub name: String,
23 23
    /// Root scope name.
24 24
    #[serde(rename = "scopeName")]
25 25
    pub scope_name: String,
26 26
    /// File extensions and special file names.
27 +
    #[serde(default)]
27 28
    #[serde(rename = "fileTypes")]
28 -
    pub file_types: Option<Vec<String>>,
29 +
    pub file_types: Vec<String>,
29 30
    /// First-line match expressions, as either a string or string array.
30 31
    #[serde(rename = "firstLineMatch")]
31 32
    pub first_line_match: Option<serde_json::Value>,
32 33
    /// Root patterns.
33 34
    pub patterns: Vec<RawPattern>,
34 35
    /// Named pattern repository.
35 -
    pub repository: Option<BTreeMap<String, RawPattern>>,
36 +
    #[serde(default)]
37 +
    pub repository: BTreeMap<String, RawPattern>,
36 38
}
37 39
38 40
/// A parsed, uncompiled TextMate pattern.
39 41
#[derive(Clone, Debug, Default, Deserialize)]
40 42
pub struct RawPattern {
46 48
    /// Begin expression for a stateful rule.
47 49
    pub begin: Option<String>,
48 50
    /// End expression for a stateful rule.
49 51
    pub end: Option<String>,
50 52
    /// Nested patterns.
51 -
    pub patterns: Option<Vec<RawPattern>>,
53 +
    #[serde(default)]
54 +
    pub patterns: Vec<RawPattern>,
52 55
    /// Include target such as `$self`, `$base`, or `#name`.
53 56
    pub include: Option<String>,
54 57
    /// Match captures.
55 -
    pub captures: Option<BTreeMap<String, RawCapture>>,
58 +
    #[serde(default)]
59 +
    pub captures: BTreeMap<String, RawCapture>,
56 60
    /// Begin captures.
61 +
    #[serde(default)]
57 62
    #[serde(rename = "beginCaptures")]
58 -
    pub begin_captures: Option<BTreeMap<String, RawCapture>>,
63 +
    pub begin_captures: BTreeMap<String, RawCapture>,
59 64
    /// End captures.
65 +
    #[serde(default)]
60 66
    #[serde(rename = "endCaptures")]
61 -
    pub end_captures: Option<BTreeMap<String, RawCapture>>,
67 +
    pub end_captures: BTreeMap<String, RawCapture>,
62 68
}
63 69
64 70
/// A parsed, uncompiled TextMate capture.
65 71
#[derive(Clone, Debug, Deserialize)]
66 72
pub struct RawCapture {
72 78
#[derive(Clone, Debug, Deserialize)]
73 79
pub struct RawTheme {
74 80
    /// Theme name.
75 81
    pub name: String,
76 82
    /// Editor UI color keys.
83 +
    #[serde(default)]
77 84
    pub colors: BTreeMap<String, String>,
78 85
    /// TextMate theme settings.
79 -
    pub settings: Option<Vec<RawThemeRule>>,
86 +
    #[serde(default)]
87 +
    pub settings: Vec<RawThemeRule>,
80 88
    /// TextMate token color rules.
89 +
    #[serde(default)]
81 90
    #[serde(rename = "tokenColors")]
82 -
    pub token_colors: Option<Vec<RawThemeRule>>,
91 +
    pub token_colors: Vec<RawThemeRule>,
83 92
}
84 93
85 94
/// One parsed TextMate theme rule.
86 95
#[derive(Clone, Debug, Deserialize)]
87 96
pub struct RawThemeRule {
crates/highlight/src/theme.rs +6 -2
113 113
impl Theme {
114 114
    /// Compiles a raw theme.
115 115
    pub fn compile(raw: &RawTheme) -> Self {
116 116
        let mut default = Style::NONE;
117 117
        let mut rules = Vec::new();
118 -
        let raw_rules = raw.token_colors.as_deref().or(raw.settings.as_deref());
118 +
        let raw_rules = if raw.token_colors.is_empty() {
119 +
            &raw.settings
120 +
        } else {
121 +
            &raw.token_colors
122 +
        };
119 123
120 -
        for rule in raw_rules.unwrap_or_default() {
124 +
        for rule in raw_rules {
121 125
            let selectors = rule.scope_selectors();
122 126
            if selectors.is_empty() {
123 127
                if let Some(foreground) = &rule.settings.foreground {
124 128
                    default.foreground = foreground.parse().ok();
125 129
                }