rsconstruct 0.9.132

Rust based fast build system
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
use anyhow::{Context, Result, bail};
use std::collections::HashSet;
use std::fs;

use crate::registries::{all_pnames, find_plugin, parse_name};

const CONFIG_FILE: &str = "rsconstruct.toml";

/// Load rsconstruct.toml as a `toml_edit` document.
fn load_doc() -> Result<toml_edit::DocumentMut> {
    let content =
        fs::read_to_string(CONFIG_FILE).with_context(|| format!("Failed to read {CONFIG_FILE}"))?;
    content
        .parse()
        .with_context(|| format!("Failed to parse {CONFIG_FILE}"))
}

/// Write a `toml_edit` document back to rsconstruct.toml.
fn save_doc(doc: &toml_edit::DocumentMut) -> Result<()> {
    fs::write(CONFIG_FILE, doc.to_string())
        .with_context(|| format!("Failed to write {CONFIG_FILE}"))
}

/// Get or create the [processor] table in the document.
fn processor_table(doc: &mut toml_edit::DocumentMut) -> Result<&mut toml_edit::Table> {
    doc.entry("processor")
        .or_insert_with(|| toml_edit::Item::Table(toml_edit::Table::new()))
        .as_table_mut()
        .context("[processor] must be a table")
}

/// Validate that a name is a registered processor (`processor.<type>.<name>`),
/// not an instance of one.
fn validate_name(name: &str) -> Result<()> {
    match parse_name(name) {
        Some(parsed) if parsed.instance.is_none() && find_plugin(name).is_some() => Ok(()),
        _ => bail!(
            "Unknown processor '{name}'. Processors are named processor.<type>.<name>; run \
             'rsconstruct processor list' to see them."
        ),
    }
}

/// The key path of a processor or analyzer name below its section table.
///
/// A processor name `processor.checker.ruff` lives at `[processor]` →
/// `checker` → `ruff`; the instance `processor.checker.ruff.core` one level
/// further down. Analyzer names have no type segment: `tera` or `tera.sub`.
fn key_path(section: &str, name: &str) -> Vec<String> {
    let prefix = format!("{section}.");
    let rest = name.strip_prefix(&prefix).unwrap_or(name);
    rest.split('.').map(str::to_string).collect()
}

/// The table at `path` below `table`, created empty where missing.
fn ensure_table<'a>(
    table: &'a mut toml_edit::Table,
    path: &[String],
) -> Result<&'a mut toml_edit::Table> {
    let mut current = table;
    for key in path {
        current = current
            .entry(key)
            .or_insert_with(|| toml_edit::Item::Table(toml_edit::Table::new()))
            .as_table_mut()
            .with_context(|| format!("'{key}' must be a table"))?;
    }
    Ok(current)
}

/// The table at `path` below `table`, if every segment exists and is a table.
fn get_table<'a>(
    table: &'a mut toml_edit::Table,
    path: &[String],
) -> Option<&'a mut toml_edit::Table> {
    let mut current = table;
    for key in path {
        current = current.get_mut(key)?.as_table_mut()?;
    }
    Some(current)
}

/// Whether a section for `name` exists below `table`.
fn has_section(table: &mut toml_edit::Table, section: &str, name: &str) -> bool {
    get_table(table, &key_path(section, name)).is_some()
}

/// Add an empty section for `name` below `table`. Returns false if it existed.
fn add_section(table: &mut toml_edit::Table, section: &str, name: &str) -> Result<bool> {
    if has_section(table, section, name) {
        return Ok(false);
    }
    let path = key_path(section, name);
    let (parents, last) = path.split_at(path.len() - 1);
    let parent = ensure_table(table, parents)?;
    parent.insert(&last[0], toml_edit::Item::Table(toml_edit::Table::new()));
    Ok(true)
}

/// Remove the section for `name` below `table`, then every parent table it
/// left empty, so a removed `processor.checker.ruff` does not leave a bare
/// `[processor.checker]` behind. Returns whether anything was removed.
fn remove_section(table: &mut toml_edit::Table, section: &str, name: &str) -> bool {
    let path = key_path(section, name);
    fn remove_at(table: &mut toml_edit::Table, path: &[String]) -> bool {
        let Some((first, rest)) = path.split_first() else {
            return false;
        };
        if rest.is_empty() {
            return table.remove(first).is_some();
        }
        let Some(child) = table.get_mut(first).and_then(|t| t.as_table_mut()) else {
            return false;
        };
        let removed = remove_at(child, rest);
        if removed && child.is_empty() {
            table.remove(first);
        }
        removed
    }
    remove_at(table, &path)
}

/// Disable all processors by removing all [processor.*] sections.
pub fn disable_all() -> Result<()> {
    let mut doc = load_doc()?;
    let table = processor_table(&mut doc)?;
    let count = remove_all_sections(table);

    save_doc(&doc)?;
    println!("Removed {count} processor sections from {CONFIG_FILE}.");
    Ok(())
}

/// Remove every processor section, counting leaf sections (one per
/// processor or instance), and return the count.
fn remove_all_sections(table: &mut toml_edit::Table) -> usize {
    // processor → type → name → (fields | instances): a name table whose
    // values are all tables holds instances, one section each; any other
    // name table is one section itself.
    fn count_sections(name_table: &toml_edit::Table) -> usize {
        let instances = name_table
            .iter()
            .filter(|(_, item)| item.is_table())
            .count();
        if instances > 0 && instances == name_table.len() {
            instances
        } else {
            1
        }
    }
    let count: usize = table
        .iter()
        .filter_map(|(_, type_item)| type_item.as_table())
        .flat_map(|type_table| type_table.iter())
        .filter_map(|(_, name_item)| name_item.as_table())
        .map(count_sections)
        .sum();
    let keys: Vec<String> = table.iter().map(|(k, _)| k.to_string()).collect();
    for key in &keys {
        table.remove(key);
    }
    count
}

/// Enable all processors by adding a section for every registered processor.
pub fn enable_all() -> Result<()> {
    let mut doc = load_doc()?;
    let table = processor_table(&mut doc)?;
    let mut count = 0;

    for name in all_pnames() {
        if add_section(table, "processor", &name)? {
            count += 1;
        }
    }

    save_doc(&doc)?;
    println!("Added {count} processor sections to {CONFIG_FILE}.");
    Ok(())
}

/// Disable a single processor by removing its section.
pub fn disable(name: &str) -> Result<()> {
    validate_name(name)?;
    let mut doc = load_doc()?;
    let table = processor_table(&mut doc)?;

    if remove_section(table, "processor", name) {
        save_doc(&doc)?;
        println!("Removed processor '{name}'.");
    } else {
        println!("Processor '{name}' is not declared.");
    }
    Ok(())
}

/// Enable a single processor by adding an empty section for it.
pub fn enable(name: &str) -> Result<()> {
    validate_name(name)?;
    let mut doc = load_doc()?;
    let table = processor_table(&mut doc)?;

    if add_section(table, "processor", name)? {
        save_doc(&doc)?;
        println!("Added processor '{name}'.");
    } else {
        println!("Processor '{name}' is already declared.");
    }
    Ok(())
}

/// Enable only processors whose files are detected in the project.
pub fn enable_detected(detected: &HashSet<String>) -> Result<()> {
    let mut doc = load_doc()?;
    let table = processor_table(&mut doc)?;
    let mut count = 0;

    for name in detected {
        if add_section(table, "processor", name)? {
            count += 1;
        }
    }

    save_doc(&doc)?;
    println!("Added {count} detected processor sections to {CONFIG_FILE}.");
    Ok(())
}

/// Remove all processor sections, returning to empty config.
pub fn reset() -> Result<()> {
    disable_all()
}

/// Remove all processor sections, then add only the listed ones.
pub fn only(names: &[String]) -> Result<()> {
    for name in names {
        validate_name(name)?;
    }

    let mut doc = load_doc()?;
    let table = processor_table(&mut doc)?;
    remove_all_sections(table);
    for name in names {
        add_section(table, "processor", name)?;
    }

    save_doc(&doc)?;
    println!("Active processors: {}", names.join(", "));
    Ok(())
}

/// Remove all processor sections, then add only detected ones.
pub fn minimal(detected: &HashSet<String>) -> Result<()> {
    let mut doc = load_doc()?;
    let table = processor_table(&mut doc)?;
    remove_all_sections(table);
    for name in detected {
        add_section(table, "processor", name)?;
    }

    save_doc(&doc)?;
    if detected.is_empty() {
        println!("No processors detected.");
    } else {
        let mut names: Vec<&String> = detected.iter().collect();
        names.sort();
        println!(
            "Minimal config: {}",
            names
                .iter()
                .map(|s| s.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        );
    }
    Ok(())
}

/// Add sections for processors whose files are detected AND tools are installed.
pub fn enable_if_available(available: &HashSet<String>) -> Result<()> {
    enable_detected(available)
}

/// Auto-detect relevant processors and add them to rsconstruct.toml.
/// Only adds processors whose files are detected AND whose tools are installed.
/// Does not remove existing processor sections.
pub fn auto(available: &HashSet<String>) -> Result<()> {
    let mut doc = load_doc()?;
    let table = processor_table(&mut doc)?;
    let mut added = Vec::new();

    for name in available {
        if add_section(table, "processor", name)? {
            added.push(name.as_str());
        }
    }

    if added.is_empty() {
        println!("No new processors to add (all detected processors are already declared).");
    } else {
        save_doc(&doc)?;
        let mut added = added;
        added.sort_unstable();
        println!("Added {} processor(s): {}", added.len(), added.join(", "));
    }
    Ok(())
}

/// Get or create the [analyzer] table in the document.
fn analyzer_table(doc: &mut toml_edit::DocumentMut) -> Result<&mut toml_edit::Table> {
    doc.entry("analyzer")
        .or_insert_with(|| toml_edit::Item::Table(toml_edit::Table::new()))
        .as_table_mut()
        .context("[analyzer] must be a table")
}

/// Set `enabled = VALUE` on the entry for `iname` below `section_table`.
/// Returns an error if the iname is not found.
fn set_enabled_iname(
    section_table: &mut toml_edit::Table,
    iname: &str,
    value: bool,
    section: &str,
) -> Result<()> {
    match get_table(section_table, &key_path(section, iname)) {
        Some(t) => {
            t.insert("enabled", toml_edit::value(value));
            Ok(())
        }
        None => bail!("{section} '{iname}' is not declared in rsconstruct.toml."),
    }
}

/// Delete a processor by iname from rsconstruct.toml.
pub fn delete_processor(iname: &str) -> Result<()> {
    let mut doc = load_doc()?;
    let table = processor_table(&mut doc)?;
    if remove_section(table, "processor", iname) {
        save_doc(&doc)?;
        println!("Deleted processor '{iname}'.");
    } else {
        println!("Processor '{iname}' is not declared.");
    }
    Ok(())
}

/// Set enabled = false on a processor by iname.
pub fn disable_processor(iname: &str) -> Result<()> {
    let mut doc = load_doc()?;
    let table = processor_table(&mut doc)?;
    set_enabled_iname(table, iname, false, "processor")?;
    save_doc(&doc)?;
    println!("Disabled processor '{iname}'.");
    Ok(())
}

/// Set enabled = true on a processor by iname.
pub fn enable_processor(iname: &str) -> Result<()> {
    let mut doc = load_doc()?;
    let table = processor_table(&mut doc)?;
    set_enabled_iname(table, iname, true, "processor")?;
    save_doc(&doc)?;
    println!("Enabled processor '{iname}'.");
    Ok(())
}

/// Delete an analyzer by iname from rsconstruct.toml.
pub fn delete_analyzer(iname: &str) -> Result<()> {
    let mut doc = load_doc()?;
    let table = analyzer_table(&mut doc)?;
    if remove_section(table, "analyzer", iname) {
        save_doc(&doc)?;
        println!("Deleted analyzer '{iname}'.");
    } else {
        println!("Analyzer '{iname}' is not declared.");
    }
    Ok(())
}

/// Set enabled = false on an analyzer by iname.
pub fn disable_analyzer(iname: &str) -> Result<()> {
    let mut doc = load_doc()?;
    let table = analyzer_table(&mut doc)?;
    set_enabled_iname(table, iname, false, "analyzer")?;
    save_doc(&doc)?;
    println!("Disabled analyzer '{iname}'.");
    Ok(())
}

/// Set enabled = true on an analyzer by iname.
pub fn enable_analyzer(iname: &str) -> Result<()> {
    let mut doc = load_doc()?;
    let table = analyzer_table(&mut doc)?;
    set_enabled_iname(table, iname, true, "analyzer")?;
    save_doc(&doc)?;
    println!("Enabled analyzer '{iname}'.");
    Ok(())
}

/// Remove processors from rsconstruct.toml that don't match any files.
pub fn remove_no_file_processors(empty_processors: &[String]) -> Result<()> {
    if empty_processors.is_empty() {
        println!("All processors match at least one file.");
        return Ok(());
    }

    let mut doc = load_doc()?;
    let table = processor_table(&mut doc)?;
    let mut removed = Vec::new();

    for name in empty_processors {
        if remove_section(table, "processor", name) {
            removed.push(name.as_str());
        }
    }

    if removed.is_empty() {
        println!("No processors to remove.");
    } else {
        save_doc(&doc)?;
        println!(
            "Removed {} processor(s) with no files: {}",
            removed.len(),
            removed.join(", ")
        );
    }
    Ok(())
}

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

    fn doc(src: &str) -> toml_edit::DocumentMut {
        src.parse().unwrap()
    }

    /// Sections nest by name segment, and removing the last processor of a
    /// type takes the empty type table with it.
    #[test]
    fn sections_nest_by_name_segment() {
        let mut d = doc("");
        let table = processor_table(&mut d).unwrap();
        assert!(add_section(table, "processor", "processor.checker.ruff").unwrap());
        assert!(!add_section(table, "processor", "processor.checker.ruff").unwrap());
        assert!(add_section(table, "processor", "processor.checker.pylint.core").unwrap());
        let text = d.to_string();
        assert!(text.contains("[processor.checker.ruff]"), "{text}");
        assert!(text.contains("[processor.checker.pylint.core]"), "{text}");

        let table = processor_table(&mut d).unwrap();
        assert!(remove_section(table, "processor", "processor.checker.ruff"));
        assert!(remove_section(
            table,
            "processor",
            "processor.checker.pylint.core"
        ));
        assert!(!remove_section(
            table,
            "processor",
            "processor.checker.pylint.core"
        ));
        assert!(
            table.get("checker").is_none(),
            "empty type table must go: {d}"
        );
    }

    #[test]
    fn enabled_flag_lands_on_the_named_section() {
        let mut d = doc("[processor.checker.pylint.core]\nargs = []\n[analyzer.tera]\n");
        let table = processor_table(&mut d).unwrap();
        set_enabled_iname(table, "processor.checker.pylint.core", false, "processor").unwrap();
        assert!(set_enabled_iname(table, "processor.checker.ruff", false, "processor").is_err());
        let table = analyzer_table(&mut d).unwrap();
        set_enabled_iname(table, "tera", false, "analyzer").unwrap();
        let text = d.to_string();
        assert!(
            text.contains("[processor.checker.pylint.core]\nargs = []\nenabled = false"),
            "{text}"
        );
        assert!(text.contains("[analyzer.tera]\nenabled = false"), "{text}");
    }

    #[test]
    fn remove_all_counts_processors_and_instances() {
        let mut d = doc(
            "[processor.checker.ruff]\n[processor.checker.pylint.core]\n[processor.checker.pylint.tests]\n[processor.generator.tera]\n",
        );
        let table = processor_table(&mut d).unwrap();
        assert_eq!(remove_all_sections(table), 4);
        assert!(table.is_empty());
    }
}