Skip to main content

lintel_check/
lib.rs

1#![doc = include_str!("../README.md")]
2
3use anyhow::Result;
4use bpaf::Bpaf;
5pub use lintel_validate::Reporter;
6
7// -----------------------------------------------------------------------
8// CheckArgs — CLI struct for the `lintel check` command
9// -----------------------------------------------------------------------
10
11#[derive(Debug, Clone, Bpaf)]
12#[bpaf(generate(check_args_inner))]
13pub struct CheckArgs {
14    /// Automatically fix formatting issues
15    #[bpaf(long("fix"), switch)]
16    pub fix: bool,
17
18    #[bpaf(external(lintel_validate::validate_args))]
19    pub validate: lintel_validate::ValidateArgs,
20}
21
22/// Construct the bpaf parser for `CheckArgs`.
23pub fn check_args() -> impl bpaf::Parser<CheckArgs> {
24    check_args_inner()
25}
26
27/// Run all checks: schema validation and formatting.
28///
29/// Returns `Ok(true)` if any errors were found, `Ok(false)` if clean.
30///
31/// # Errors
32///
33/// Returns an error if schema validation fails to run (e.g. network or I/O issues).
34pub async fn run(args: &mut CheckArgs, reporter: &mut dyn Reporter) -> Result<bool> {
35    // Save original args before validate's merge_config modifies them.
36    let original_globs = args.validate.globs.clone();
37    let original_exclude = args.validate.exclude.clone();
38
39    let had_validation_errors = lintel_validate::run(&mut args.validate, reporter).await?;
40
41    if args.fix {
42        let fixed = lintel_format::fix_format(&original_globs, &original_exclude)?;
43        if fixed > 0 {
44            eprintln!("Fixed formatting in {fixed} file(s).");
45        }
46        Ok(had_validation_errors)
47    } else {
48        // Check formatting using original (pre-merge) args so lintel-format
49        // can load lintel.toml independently without double-merging excludes.
50        let format_diagnostics = lintel_format::check_format(&original_globs, &original_exclude)?;
51        let had_format_errors = !format_diagnostics.is_empty();
52
53        for diag in format_diagnostics {
54            eprintln!("{:?}", miette::Report::new(diag));
55        }
56
57        Ok(had_validation_errors || had_format_errors)
58    }
59}