gruff-rs 0.3.0

Rust static analyzer and quality linter for CI: dead-code, complexity, security, secrets, and architecture rules with deterministic SARIF/JSON output and baseline support.
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
use super::*;

mod args;

pub(crate) use args::{
    AnalyseArgs, CheckIgnoreArgs, CheckIgnoreFormat, CompletionArgs, DashboardArgs, HookArgs,
    InitArgs, ListRulesArgs, ReportArgs, SummaryArgs,
};

/// Symfony-Console-style colours for help output: yellow section headers,
/// green flag/command literals, dimmed placeholders. Matches the gruff-php
/// help layout users may be coming from.
const HELP_STYLES: styling::Styles = styling::Styles::styled()
    .header(
        styling::Style::new()
            .fg_color(Some(styling::Color::Ansi(styling::AnsiColor::Yellow)))
            .bold(),
    )
    .usage(
        styling::Style::new()
            .fg_color(Some(styling::Color::Ansi(styling::AnsiColor::Yellow)))
            .bold(),
    )
    .literal(styling::Style::new().fg_color(Some(styling::Color::Ansi(styling::AnsiColor::Green))))
    .placeholder(styling::Style::new().dimmed())
    .error(
        styling::Style::new()
            .fg_color(Some(styling::Color::Ansi(styling::AnsiColor::Red)))
            .bold(),
    )
    .valid(styling::Style::new().fg_color(Some(styling::Color::Ansi(styling::AnsiColor::Green))))
    .invalid(
        styling::Style::new().fg_color(Some(styling::Color::Ansi(styling::AnsiColor::Yellow))),
    );

const HELP_TEMPLATE: &str = "\
{before-help}{name} {version}\n\n\
\x1b[1m\x1b[33mUsage:\x1b[0m\n  {usage}\n\n\
\x1b[1m\x1b[33mOptions:\x1b[0m\n{options}\n\n\
\x1b[1m\x1b[33mAvailable commands:\x1b[0m\n{subcommands}{after-help}";

#[derive(Parser)]
#[command(
    name = "gruff-rs",
    version = VERSION,
    about = "Rust project quality analysis.",
    styles = HELP_STYLES,
    help_template = HELP_TEMPLATE,
    subcommand_help_heading = "Available commands",
    subcommand_value_name = "command",
    arg_required_else_help = true,
)]
pub(crate) struct Cli {
    #[command(flatten)]
    pub(crate) global: GlobalOptions,
    #[command(subcommand)]
    pub(crate) command: Commands,
}

// Symfony-Console-style global flags shared by every subcommand.
// `--silent` and `-q/--quiet` gate the primary stdout writer. `--ansi`/`--no-ansi`
// is reserved for the text renderer's future colour mode; today the text renderer
// emits no ANSI, so these flags accept and store but otherwise do not change
// output. `-v/-vv/-vvv` is a count flag the analyzer can opt into for stderr
// debug traces. `-n/--no-interaction` is accepted for parity and ignored;
// gruff-rs is non-interactive.
#[derive(Args, Clone, Debug, Default)]
pub(crate) struct GlobalOptions {
    /// Do not output any message.
    #[arg(long, global = true)]
    silent: bool,
    /// Only errors are displayed. All other output is suppressed.
    #[arg(short = 'q', long, global = true)]
    quiet: bool,
    /// Force ANSI output.
    #[arg(long, global = true, conflicts_with = "no_ansi")]
    ansi: bool,
    /// Disable ANSI output.
    #[arg(long = "no-ansi", global = true)]
    no_ansi: bool,
    /// Increase the verbosity of stderr messages (-v, -vv, -vvv). `check-ignore`
    /// also reads this: any `-v` switches its text output to `<path>\t<source>:<pattern>`.
    #[arg(short = 'v', long, action = clap::ArgAction::Count, global = true)]
    pub(crate) verbose: u8,
    /// Do not ask any interactive question (accepted for CLI parity; gruff-rs is non-interactive).
    #[arg(short = 'n', long, global = true)]
    no_interaction: bool,
}

impl GlobalOptions {
    pub(crate) fn writer(&self) -> OutputWriter {
        OutputWriter {
            silent: self.silent,
            quiet: self.quiet,
        }
    }

    pub(crate) fn is_non_interactive(&self) -> bool {
        self.no_interaction || self.silent
    }
}

#[derive(Clone, Copy, Debug)]
pub(crate) struct OutputWriter {
    silent: bool,
    quiet: bool,
}

impl OutputWriter {
    pub(crate) fn emit(self, outcome: RunOutcome, body: &str) {
        if self.silent {
            return;
        }
        if self.quiet && !outcome.is_failure() {
            return;
        }
        println!("{body}");
    }

    /// Emit a body that is not gated by the success/failure outcome of an
    /// analysis run (e.g. completion scripts, list-rules output).
    pub(crate) fn emit_unconditional(self, body: &str) {
        if self.silent {
            return;
        }
        println!("{body}");
    }

    pub(crate) fn is_silent(self) -> bool {
        self.silent
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum RunOutcome {
    Success,
    ThresholdHit,
    DiagnosticsFailed,
}

impl RunOutcome {
    /// Classify a run's exit outcome. A fatal diagnostic is exit 2; otherwise the
    /// `gate:` block evaluates first (a trip with `onMatch: fail` is exit 1), then
    /// the legacy `--fail-on` flag - the precedence documented in ADR-003.
    pub(crate) fn classify(
        report: &AnalysisReport,
        fail_on: FailThreshold,
        gate: Option<&Gate>,
    ) -> Self {
        if report.diagnostics.iter().any(RunDiagnostic::is_failure) {
            return Self::DiagnosticsFailed;
        }
        if gate.is_some_and(|gate| gate.evaluate_report(report).fails) {
            return Self::ThresholdHit;
        }
        if report
            .findings
            .iter()
            .any(|finding| fail_on.is_triggered_by_severity(finding.severity))
        {
            return Self::ThresholdHit;
        }
        Self::Success
    }

    pub(crate) fn exit_code(self) -> ExitCode {
        match self {
            Self::Success => ExitCode::SUCCESS,
            Self::ThresholdHit => ExitCode::from(1),
            Self::DiagnosticsFailed => ExitCode::from(2),
        }
    }

    pub(crate) fn is_failure(self) -> bool {
        !matches!(self, Self::Success)
    }
}

#[derive(Subcommand)]
pub(crate) enum Commands {
    /// Run gruff analysis.
    Analyse(AnalyseArgs),
    /// Emit gruff.hook.v1 JSON for coding-agent hooks.
    Hook(HookArgs),
    /// Render a gruff report to stdout or a file.
    Report(ReportArgs),
    /// List gruff rule metadata.
    #[command(alias = "rules")]
    ListRules(ListRulesArgs),
    /// Serve the local gruff dashboard.
    Dashboard(DashboardArgs),
    /// Print a compact digest of a scan: per-pillar finding counts, top rules, and top file offenders.
    Summary(SummaryArgs),
    /// Report whether gruff would ignore each path and why, using the same config and ignore engine as `analyse`. Runs no analysis.
    CheckIgnore(CheckIgnoreArgs),
    /// Dump the shell completion script.
    Completion(CompletionArgs),
    /// Write a default `.gruff-rs.yaml` config derived from the built-in rule registry.
    Init(InitArgs),
}

#[derive(Clone, Copy, Debug, ValueEnum, Serialize, PartialEq, Eq)]
pub(crate) enum OutputFormat {
    Text,
    Json,
    Sarif,
    Html,
    Markdown,
    Github,
    Hotspot,
}

impl OutputFormat {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::Text => "text",
            Self::Json => "json",
            Self::Sarif => "sarif",
            Self::Html => "html",
            Self::Markdown => "markdown",
            Self::Github => "github",
            Self::Hotspot => "hotspot",
        }
    }
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub(crate) enum SummaryFormat {
    Text,
    Json,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub(crate) enum ReportFormat {
    Html,
    Json,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub(crate) enum RuleListFormat {
    Text,
    Json,
}

#[derive(Clone, Copy, Debug, ValueEnum, Serialize, PartialEq, Eq)]
pub(crate) enum FailThreshold {
    None,
    Advisory,
    Warning,
    Error,
}

impl FailThreshold {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Advisory => "advisory",
            Self::Warning => "warning",
            Self::Error => "error",
        }
    }

    pub(crate) fn is_triggered_by_severity(self, severity: Severity) -> bool {
        match self {
            Self::None => false,
            Self::Advisory => true,
            Self::Warning => severity == Severity::Warning || severity == Severity::Error,
            Self::Error => severity == Severity::Error,
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct FailThresholdParseError {
    pub(crate) value: String,
}

impl std::fmt::Display for FailThresholdParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "invalid fail-on value \"{}\": expected one of advisory, warning, error, none",
            self.value
        )
    }
}

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

impl std::str::FromStr for FailThreshold {
    type Err = FailThresholdParseError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.trim().to_ascii_lowercase().as_str() {
            "none" => Ok(Self::None),
            "advisory" => Ok(Self::Advisory),
            "warning" => Ok(Self::Warning),
            "error" => Ok(Self::Error),
            _ => Err(FailThresholdParseError {
                value: value.to_string(),
            }),
        }
    }
}

impl<'de> Deserialize<'de> for FailThreshold {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct FailThresholdVisitor;

        impl serde::de::Visitor<'_> for FailThresholdVisitor {
            type Value = FailThreshold;

            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_str("a fail-on threshold (advisory, warning, error, or none)")
            }

            fn visit_str<E>(self, value: &str) -> Result<FailThreshold, E>
            where
                E: serde::de::Error,
            {
                <FailThreshold as std::str::FromStr>::from_str(value).map_err(E::custom)
            }
        }

        deserializer.deserialize_str(FailThresholdVisitor)
    }
}

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

    #[test]
    fn fail_threshold_from_str_accepts_canonical_values() {
        assert!(matches!(
            "none".parse::<FailThreshold>(),
            Ok(FailThreshold::None)
        ));
        assert!(matches!(
            "advisory".parse::<FailThreshold>(),
            Ok(FailThreshold::Advisory)
        ));
        assert!(matches!(
            "warning".parse::<FailThreshold>(),
            Ok(FailThreshold::Warning)
        ));
        assert!(matches!(
            "error".parse::<FailThreshold>(),
            Ok(FailThreshold::Error)
        ));
    }

    #[test]
    fn fail_threshold_round_trips_via_as_str() {
        for value in [
            FailThreshold::None,
            FailThreshold::Advisory,
            FailThreshold::Warning,
            FailThreshold::Error,
        ] {
            let parsed: FailThreshold = value.as_str().parse().expect("canonical value parses");
            assert_eq!(parsed.as_str(), value.as_str());
        }
    }

    #[test]
    fn fail_threshold_from_str_is_case_insensitive_and_trims() {
        assert!(matches!(
            "None".parse::<FailThreshold>(),
            Ok(FailThreshold::None)
        ));
        assert!(matches!(
            "ADVISORY".parse::<FailThreshold>(),
            Ok(FailThreshold::Advisory)
        ));
        assert!(matches!(
            "  warning  ".parse::<FailThreshold>(),
            Ok(FailThreshold::Warning)
        ));
    }

    #[test]
    fn fail_threshold_from_str_rejects_never_with_documented_message() {
        let err = "never"
            .parse::<FailThreshold>()
            .expect_err("never must reject");
        assert_eq!(
            err.to_string(),
            "invalid fail-on value \"never\": expected one of advisory, warning, error, none"
        );
    }

    #[test]
    fn fail_threshold_from_str_rejects_legacy_and_typo_values() {
        for bogus in [
            "medium", "critical", "info", "warn", "low", "high", "notice", "",
        ] {
            let err = bogus
                .parse::<FailThreshold>()
                .expect_err(&format!("{bogus} must reject"));
            assert!(
                err.to_string().contains("advisory, warning, error, none"),
                "error for {bogus:?} missing valid-values list: {err}"
            );
        }
    }

    #[test]
    fn fail_threshold_deserialize_accepts_canonical_lowercase() {
        let parsed: FailThreshold =
            serde_yaml::from_str("none").expect("lowercase none parses via serde");
        assert!(matches!(parsed, FailThreshold::None));
    }

    #[test]
    fn fail_threshold_deserialize_is_case_insensitive() {
        let parsed: FailThreshold =
            serde_yaml::from_str("Advisory").expect("capitalised value parses via serde");
        assert!(matches!(parsed, FailThreshold::Advisory));
    }

    #[test]
    fn fail_threshold_deserialize_yaml_rejects_never() {
        let err = serde_yaml::from_str::<FailThreshold>("never")
            .expect_err("never must reject via serde");
        assert!(
            err.to_string()
                .contains("expected one of advisory, warning, error, none"),
            "serde error missing valid-values list: {err}"
        );
    }

    #[test]
    fn fail_threshold_deserialize_yaml_rejects_bogus() {
        let err = serde_yaml::from_str::<FailThreshold>("bogus")
            .expect_err("bogus must reject via serde");
        assert!(
            err.to_string().contains("advisory, warning, error, none"),
            "serde error missing valid-values list: {err}"
        );
    }
}