brake 2026.11.3

A brake on breaking API changes. Checks OpenAPI, protobuf and GraphQL contracts for backward compatibility, hermetically.
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
//! The `brake` CLI.
//!
//! This binary parses arguments, renders output, and exits. It does not decide
//! whether something is a breaking change — that lives in the library, so it
//! can be tested without spawning a process and so `forge` reaches the same
//! code. See `design/03-implementation-plan.md` §1.

use std::io::IsTerminal;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::time::{SystemTime, UNIX_EPOCH};

use brake::check::{Document, Options, Scope};
use brake::config::{Baseline, Compatibility, Config, ContractFormat};
use brake::report::Report;
use brake::{Severity, Verdict};
use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(
    name = "brake",
    version = brake::VERSION,
    about = "A brake on breaking API changes",
    long_about = None,
)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Check the contracts among the given paths against their baseline.
    ///
    /// The primary surface: a pre-commit hook passes the changed files, and
    /// scoping the run to the change is what makes the gate adoptable on a
    /// repository that already has findings.
    Check {
        /// Contract files to check. Defaults to every configured contract.
        paths: Vec<PathBuf>,
        /// Scope to contracts changed since the merge-base with this git ref.
        #[arg(long)]
        since: Option<String>,
        /// Path to brake.toml.
        #[arg(long)]
        config: Option<PathBuf>,
        /// Check only these contracts, by name. Repeatable.
        #[arg(long)]
        contract: Vec<String>,
        /// Verify against only these declared consumers, by name. Repeatable.
        ///
        /// Mirrors `--contract`. A path scope that names a consumer
        /// declaration selects the contracts that declaration constrains, so a
        /// hook run on a pact-updating commit verifies the right thing.
        #[arg(long)]
        consumer: Vec<String>,
        /// Override every contract's baseline.
        ///
        /// A revision (`v1.2.0`, `origin/main`, `8743cba`), a tag glob
        /// (`latest-tag:v*`), or the explicit `ref:path` form.
        #[arg(long)]
        baseline: Option<String>,
        /// Override the compatibility level: wire, wire-json, surface, strict.
        #[arg(long)]
        compatibility: Option<String>,
        /// Minimum severity to fail on.
        #[arg(long, default_value = "warning")]
        severity: String,
        /// Evaluate suppression expiry at this date (YYYY-MM-DD).
        /// Defaults to today.
        #[arg(long)]
        as_of: Option<String>,
        /// Output format: auto, text, json, sarif, github, gitlab.
        #[arg(long, short, default_value = "auto")]
        format: String,
        /// Also run declared generator commands and check for drift.
        ///
        /// The only place brake executes a subprocess, and off by default so
        /// `brake check` stays safe against an untrusted repository.
        #[arg(long)]
        drift: bool,
    },
    /// Check every configured contract in the repository.
    Analyze {
        /// Repository root.
        #[arg(default_value = ".")]
        path: PathBuf,
        /// Path to brake.toml.
        #[arg(long)]
        config: Option<PathBuf>,
        /// Check only these contracts, by name. Repeatable.
        #[arg(long)]
        contract: Vec<String>,
        /// Verify against only these declared consumers, by name. Repeatable.
        #[arg(long)]
        consumer: Vec<String>,
        /// Override the compatibility level.
        #[arg(long)]
        compatibility: Option<String>,
        /// Output format: auto, text, json, sarif, github, gitlab.
        #[arg(long, short, default_value = "auto")]
        format: String,
        /// Minimum severity that should fail analyze.
        #[arg(long, default_value = "warning")]
        fail_on: String,
        /// Evaluate suppression expiry at this date (YYYY-MM-DD).
        #[arg(long)]
        as_of: Option<String>,
        /// Also run declared generator commands and check for drift.
        #[arg(long)]
        drift: bool,
    },
    /// Report every change with its classification, without failing.
    ///
    /// For pull-request descriptions and changelog drafting: it always exits
    /// 0, and reports at `strict` so additive changes are listed too.
    Diff {
        /// Path to brake.toml.
        #[arg(long)]
        config: Option<PathBuf>,
        /// Report only these contracts, by name. Repeatable.
        #[arg(long)]
        contract: Vec<String>,
        /// Override every contract's baseline. See `brake check --help`.
        #[arg(long)]
        baseline: Option<String>,
        /// Output format: auto, text, json, sarif, github, gitlab.
        #[arg(long, short, default_value = "auto")]
        format: String,
    },
    /// Compare two contract documents, with no configuration.
    ///
    /// The same check `brake check` runs, over two files you name rather than
    /// a contract and its baseline. Nothing outside the two files is read: a
    /// `$ref` into a sibling is reported as unmodelled.
    Compare {
        /// The previous version.
        base: PathBuf,
        /// The new version.
        head: PathBuf,
        /// The contract format: openapi, proto, graphql, asyncapi, mcp.
        /// Detected by parsing when omitted.
        #[arg(long)]
        contract_format: Option<String>,
        /// Compatibility level: wire, wire-json, surface, strict.
        #[arg(long, default_value = "wire-json")]
        compatibility: String,
        /// Minimum severity that fails the run: info, warning, error.
        #[arg(long, default_value = "warning")]
        severity: String,
        /// Output format: auto, text, json, sarif, github, gitlab.
        #[arg(long, short, default_value = "auto")]
        format: String,
    },
    /// Discover the contracts here and write a brake.toml declaring them.
    ///
    /// A file counts as a contract only if brake can actually parse it, so a
    /// CI workflow that happens to be called `api-something.yaml` is not
    /// mistaken for an API.
    Init {
        /// Repository root. Defaults to the working directory.
        #[arg(default_value = ".")]
        path: PathBuf,
        /// Print the configuration instead of writing it.
        #[arg(long)]
        dry_run: bool,
        /// Overwrite an existing brake.toml.
        #[arg(long)]
        force: bool,
    },
    /// Serve the ruleset over MCP, for a coding agent.
    ///
    /// The same checks `brake check` runs, consulted while an API is being
    /// edited rather than when it is committed. stdio transport; it never
    /// listens on a port and never runs a declared generator command.
    ///
    /// Needs the `mcp` feature, which is not on by default because the server
    /// requires an async runtime. The subcommand is listed either way: a
    /// capability that silently does not exist is one nobody can discover, and
    /// brake's whole posture is to say what it cannot do rather than go quiet.
    Mcp {
        /// Repository root. Defaults to the working directory.
        #[arg(default_value = ".")]
        path: PathBuf,
        /// Evaluate suppression expiry at this date (YYYY-MM-DD).
        #[arg(long)]
        as_of: Option<String>,
    },
    /// Report who consumes what, and what the verdict rested on.
    ///
    /// Non-gating: it always exits 0, joining `diff` in that family. Each
    /// declaration is listed with its path and a content digest, because brake
    /// does not measure freshness — a pact from eighteen months ago and one
    /// from this morning are the same bytes to a file reader.
    Consumers {
        /// Path to brake.toml.
        #[arg(long)]
        config: Option<PathBuf>,
        /// Report only these contracts, by name. Repeatable.
        #[arg(long)]
        contract: Vec<String>,
        /// Report only these consumers, by name. Repeatable.
        #[arg(long)]
        consumer: Vec<String>,
        /// Output format: auto, text, json.
        #[arg(long, short, default_value = "auto")]
        format: String,
    },
    /// Explain why a rule exists and what to do about it.
    Explain {
        /// A rule ID, for example `response-field-removed`. Omit to list all.
        rule: Option<String>,
    },
}

fn main() -> ExitCode {
    match run(Cli::parse()) {
        Ok(code) => code,
        Err(message) => {
            eprintln!("brake: {message}");
            ExitCode::from(Verdict::ToolFailure.exit_code() as u8)
        }
    }
}

fn run(cli: Cli) -> Result<ExitCode, String> {
    match cli.command {
        Command::Explain { rule } => explain(rule.as_deref()),

        Command::Init {
            path,
            dry_run,
            force,
        } => init(&path, dry_run, force),

        Command::Mcp { path, as_of } => serve_mcp(path, as_of),

        Command::Check {
            paths,
            since,
            config,
            contract,
            consumer,
            baseline,
            compatibility,
            severity,
            as_of,
            format,
            drift,
        } => {
            let (root, config_value) = load(config.as_deref())?;
            let scope = match (since, paths.is_empty()) {
                (Some(reference), _) => Scope::Since(reference),
                (None, false) => Scope::Paths(paths),
                (None, true) => Scope::All,
            };
            let options = Options {
                // Expiry is the single documented clock dependency, and
                // `--as-of` overrides it so the expiry path stays testable.
                as_of: Some(as_of.unwrap_or_else(today)),
                drift,
                // A scoped run has not looked at everything, so it cannot know
                // a suppression is dead.
                report_stale: matches!(scope, Scope::All),
                compatibility: compatibility
                    .as_deref()
                    .map(Compatibility::parse)
                    .transpose()?,
                baseline: baseline.as_deref().map(Baseline::from_flag),
                only: contract,
                consumers: consumer,
                ..Options::default()
            };

            let report = brake::check::check(&root, &config_value, &scope, &options);
            emit(&report, &format)?;
            Ok(ExitCode::from(
                report.exit_code(Severity::parse(&severity)?) as u8,
            ))
        }

        Command::Analyze {
            path,
            config,
            contract,
            consumer,
            compatibility,
            format,
            fail_on,
            as_of,
            drift,
        } => {
            let config_path = config.unwrap_or_else(|| path.join("brake.toml"));
            let config_value = Config::from_path(&config_path).map_err(|e| e.to_string())?;
            let options = Options {
                as_of: Some(as_of.unwrap_or_else(today)),
                drift,
                // analyze covers everything, so a suppression matching nothing
                // really is dead.
                report_stale: true,
                compatibility: compatibility
                    .as_deref()
                    .map(Compatibility::parse)
                    .transpose()?,
                baseline: None,
                only: contract,
                consumers: consumer,
                ..Options::default()
            };

            let report = brake::check::check(&path, &config_value, &Scope::All, &options);
            emit(&report, &format)?;
            Ok(ExitCode::from(
                report.exit_code(Severity::parse(&fail_on)?) as u8
            ))
        }

        Command::Consumers {
            config,
            contract,
            consumer,
            format,
        } => {
            let (root, config_value) = load(config.as_deref())?;
            let inventory =
                brake::demand::inventory::build(&root, &config_value, &contract, &consumer);
            let rendered = match normalize_format(&format)? {
                OutputFormat::Text => brake::demand::inventory::render_text(&inventory),
                OutputFormat::Json
                | OutputFormat::Sarif
                | OutputFormat::Github
                | OutputFormat::Gitlab => brake::demand::inventory::render_json(&inventory),
            };
            print!("{rendered}");
            // Non-gating, by design: it answers a question, it does not judge.
            Ok(ExitCode::from(Verdict::Clean.exit_code() as u8))
        }

        Command::Diff {
            config,
            contract,
            baseline,
            format,
        } => {
            let (root, config_value) = load(config.as_deref())?;
            let options = Options {
                as_of: Some(today()),
                drift: false,
                report_stale: false,
                // Report everything, including additive changes, because the
                // point is a complete description of the change.
                compatibility: Some(Compatibility::Strict),
                baseline: baseline.as_deref().map(Baseline::from_flag),
                only: contract,
                consumers: Vec::new(),
                ..Options::default()
            };

            let report = brake::check::check(&root, &config_value, &Scope::All, &options);
            emit(&report, &format)?;
            // Never gates, by design.
            Ok(ExitCode::from(Verdict::Clean.exit_code() as u8))
        }
        Command::Compare {
            base,
            head,
            contract_format,
            compatibility,
            severity,
            format,
        } => {
            let read = |path: &Path| {
                std::fs::read(path)
                    .map_err(|error| format!("cannot read `{}`: {error}", path.display()))
            };
            let contract_format = match contract_format {
                Some(name) => ContractFormat::parse(&name)?,
                // The head is what is being judged, so it decides the format.
                None => brake::init::identify(&head).ok_or_else(|| {
                    format!(
                        "`{}` is not a contract brake can parse. Pass --contract-format \
                         to say which format it is meant to be",
                        head.display()
                    )
                })?,
            };
            let (base_bytes, head_bytes) = (read(&base)?, read(&head)?);
            let (base_label, head_label) = (
                brake::check::display_path(&base),
                brake::check::display_path(&head),
            );
            let report = brake::check::check_documents(
                contract_format,
                Document {
                    label: &base_label,
                    bytes: &base_bytes,
                },
                Document {
                    label: &head_label,
                    bytes: &head_bytes,
                },
                Compatibility::parse(&compatibility)?,
            );
            emit(&report, &format)?;
            Ok(ExitCode::from(
                report.exit_code(Severity::parse(&severity)?) as u8,
            ))
        }
    }
}

/// `brake init` — discover contracts and write the configuration.
fn init(path: &Path, dry_run: bool, force: bool) -> Result<ExitCode, String> {
    let discovery = brake::init::discover(path);
    let reference = brake::init::default_baseline_reference(path);
    let rendered = brake::init::render_config(&discovery, &reference);

    if dry_run {
        print!("{rendered}");
        return Ok(ExitCode::from(Verdict::Clean.exit_code() as u8));
    }

    let destination = path.join("brake.toml");
    if destination.exists() && !force {
        // Overwriting would discard hand-written suppressions and their
        // reasons, which are the most expensive thing in the file.
        return Err(format!(
            "`{}` already exists. Pass --force to overwrite it, or --dry-run to \
             see what init would write",
            destination.display()
        ));
    }

    // Report what was found before writing, so a wrong guess is visible at the
    // moment it can still be corrected.
    if discovery.contracts.is_empty() {
        println!(
            "no API contracts found under `{}`.\n\n\
             brake looks for OpenAPI, protobuf and GraphQL files it can parse — a \
             file is\nonly a contract if the ingester that would gate it can \
             actually read it. The\nconfiguration below has a commented example \
             to fill in.\n",
            path.display()
        );
    } else {
        println!(
            "found {} contract{}:",
            discovery.contracts.len(),
            if discovery.contracts.len() == 1 {
                ""
            } else {
                "s"
            }
        );
        for contract in &discovery.contracts {
            println!(
                "  {:<40} {:<8} as `{}`",
                contract.source,
                brake::init::format_name(contract.format),
                contract.name
            );
        }
        if !discovery.baselines_skipped.is_empty() {
            println!(
                "\nskipped {} checked-in baseline{} — a baseline is a previous \
                 version, not a contract to gate:",
                discovery.baselines_skipped.len(),
                if discovery.baselines_skipped.len() == 1 {
                    ""
                } else {
                    "s"
                }
            );
            for skipped in &discovery.baselines_skipped {
                println!("  {skipped}");
            }
        }
        if discovery.truncated {
            println!(
                "\nnote: stopped after examining a large number of files, so this \
                 may be incomplete."
            );
        }
        println!();
    }

    std::fs::write(&destination, &rendered)
        .map_err(|error| format!("failed to write `{}`: {error}", destination.display()))?;
    println!("wrote {}", display_relative(path, &destination));

    match discovery.contracts.first() {
        Some(contract) => println!(
            "\nnext:\n  brake check {}\n\n\
             The baseline is the merge-base with `{reference}`, so a contract that \
             is new\nto this branch reports as new rather than failing.",
            contract.source
        ),
        None => println!("\nnext: declare a contract in brake.toml, then run `brake check`"),
    }
    Ok(ExitCode::from(Verdict::Clean.exit_code() as u8))
}

fn display_relative(root: &Path, path: &Path) -> String {
    path.strip_prefix(root).map_or_else(
        |_| path.display().to_string(),
        |rest| rest.display().to_string(),
    )
}

/// The `mcp` feature is off: say so, and say how to get it.
///
/// Exiting `2` with an actionable message is the same contract every other
/// unavailable path here follows — a tool that cannot answer says which, and
/// what to do about it.
#[cfg(not(feature = "mcp"))]
fn serve_mcp(_path: PathBuf, _as_of: Option<String>) -> Result<ExitCode, String> {
    Err(format!(
        "this build of brake {} has no MCP server.\n\n\
         It is behind the `mcp` feature, which is off by default because the \
         server needs an\nasync runtime that the rest of brake does not. To get it:\n\n    \
         cargo install brake --features mcp\n\n\
         or, from a checkout:\n\n    \
         cargo run --features mcp -- mcp .\n\n\
         See design/04-mcp-interface.md.",
        brake::VERSION
    ))
}

/// Run the MCP server on stdio.
///
/// The runtime is built here rather than with `#[tokio::main]` so the async
/// surface stays inside this one function: every other path through the binary
/// is synchronous, and so is every tool handler.
#[cfg(feature = "mcp")]
fn serve_mcp(path: PathBuf, as_of: Option<String>) -> Result<ExitCode, String> {
    let root = path
        .canonicalize()
        .map_err(|error| format!("cannot read `{}`: {error}", path.display()))?;

    let context = brake::mcp::handlers::Context::new(root, as_of.unwrap_or_else(today));

    let runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .map_err(|error| format!("failed to start the async runtime: {error}"))?;

    runtime
        .block_on(brake::mcp::serve_stdio(context))
        .map_err(|error| format!("the MCP server stopped: {error}"))?;
    Ok(ExitCode::from(Verdict::Clean.exit_code() as u8))
}

fn explain(rule: Option<&str>) -> Result<ExitCode, String> {
    let Some(rule) = rule else {
        for definition in brake::rules::catalogue::RULES {
            println!(
                "{:<28} {:<10} {:<10} {}",
                definition.id,
                format!("{:?}", definition.severity).to_lowercase(),
                definition.level.name(),
                definition.summary
            );
        }
        return Ok(ExitCode::from(Verdict::Clean.exit_code() as u8));
    };

    let Some(definition) = brake::rules::catalogue::lookup(rule) else {
        return Err(format!(
            "unknown rule id `{rule}`. Run `brake explain` with no argument to list the catalogue"
        ));
    };

    println!(
        "{}\n\nseverity:      {}\nfires from:    {}\n\n{}\n\n{}",
        definition.id,
        format!("{:?}", definition.severity).to_lowercase(),
        definition.level.name(),
        definition.summary,
        definition.explanation,
    );

    // The ways out, generic here because there is no finding to bind them to.
    // A finding renders the same strategies with its own field named.
    let remedies: Vec<_> = definition
        .remedies
        .iter()
        .filter_map(|id| brake::rules::strategies::lookup(id))
        .collect();
    if !remedies.is_empty() {
        println!("\nways to make the change safely:");
        for strategy in remedies {
            println!(
                "  {}\n    {}\n    costs: {}",
                strategy.id,
                strategy
                    .summary
                    .replace("{subject}", "the field")
                    .replace("{endpoint}", "the endpoint"),
                strategy.cost
            );
        }
        println!(
            "\nbrake does not choose between these: which one fits depends on whether you\n\
             control every consumer and whether you have a version scheme, and it cannot\n\
             see either."
        );
    }

    println!("\n{}", definition.help_uri());
    Ok(ExitCode::from(Verdict::Clean.exit_code() as u8))
}

/// Find `brake.toml` and the repository root it sits in.
///
/// Walking up means `brake check` works from a subdirectory, which is where a
/// developer usually is when a hook fires.
fn load(config: Option<&Path>) -> Result<(PathBuf, Config), String> {
    if let Some(explicit) = config {
        let root = explicit
            .parent()
            .filter(|parent| !parent.as_os_str().is_empty())
            .unwrap_or(Path::new("."))
            .to_path_buf();
        let parsed = Config::from_path(explicit).map_err(|e| e.to_string())?;
        return Ok((root, parsed));
    }

    let start =
        std::env::current_dir().map_err(|error| format!("no working directory: {error}"))?;
    let mut directory = start.as_path();
    loop {
        let candidate = directory.join("brake.toml");
        if candidate.is_file() {
            let parsed = Config::from_path(&candidate).map_err(|e| e.to_string())?;
            return Ok((directory.to_path_buf(), parsed));
        }
        match directory.parent() {
            Some(parent) => directory = parent,
            None => {
                return Err(format!(
                    "no brake.toml found in `{}` or any parent directory.\n\n\
                     Run `brake init` to discover the contracts here and write one, \
                     or\n`brake init --dry-run` to see what it would write first. \
                     Pass --config to\npoint at a file elsewhere.",
                    start.display()
                ));
            }
        }
    }
}

fn emit(report: &Report, requested_format: &str) -> Result<(), String> {
    let rendered = match normalize_format(requested_format)? {
        OutputFormat::Text => brake::render::text::render(report),
        OutputFormat::Json => brake::render::json::render(report),
        OutputFormat::Sarif => brake::render::sarif::render(report),
        OutputFormat::Github => brake::render::github::render(report),
        OutputFormat::Gitlab => brake::render::gitlab::render(report),
    };
    print!("{rendered}");
    Ok(())
}

/// Today, as `YYYY-MM-DD` UTC.
///
/// Suppression expiry is the one place brake reads the clock — §6.1 guarantee
/// 5 — and the library never does it, so every other path stays trivially
/// deterministic. Days-from-civil, inverted; no calendar dependency needed for
/// a value that is only ever compared for order.
fn today() -> String {
    let seconds = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |elapsed| elapsed.as_secs());
    let days = (seconds / 86_400) as i64 + 719_468;

    let era = days.div_euclid(146_097);
    let day_of_era = days.rem_euclid(146_097);
    let year_of_era =
        (day_of_era - day_of_era / 1460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
    let year = year_of_era + era * 400;
    let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
    let month_prime = (5 * day_of_year + 2) / 153;
    let day = day_of_year - (153 * month_prime + 2) / 5 + 1;
    let month = if month_prime < 10 {
        month_prime + 3
    } else {
        month_prime - 9
    };
    let year = if month <= 2 { year + 1 } else { year };

    format!("{year:04}-{month:02}-{day:02}")
}

fn normalize_format(requested_format: &str) -> Result<OutputFormat, String> {
    match requested_format.trim().to_ascii_lowercase().as_str() {
        "text" => Ok(OutputFormat::Text),
        "json" => Ok(OutputFormat::Json),
        "sarif" => Ok(OutputFormat::Sarif),
        "github" | "github-actions" => Ok(OutputFormat::Github),
        "gitlab" | "codequality" | "code-quality" => Ok(OutputFormat::Gitlab),
        "auto" => {
            if std::io::stdout().is_terminal() {
                Ok(OutputFormat::Text)
            } else {
                Ok(OutputFormat::Json)
            }
        }
        other => Err(format!(
            "unknown format `{other}`; expected one of: auto, text, json, sarif, github, gitlab"
        )),
    }
}

enum OutputFormat {
    Text,
    Json,
    Sarif,
    Github,
    Gitlab,
}

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

    #[test]
    fn severity_and_level_parse_their_documented_spellings() {
        assert_eq!(Severity::parse("Error").expect("error"), Severity::Error);
        assert_eq!(Severity::parse(" warn ").expect("warn"), Severity::Warning);
        assert!(Severity::parse("critical").is_err());

        assert_eq!(
            Compatibility::parse("wire-json").expect("wire-json"),
            Compatibility::WireJson
        );
        assert_eq!(
            Compatibility::parse("STRICT").expect("strict"),
            Compatibility::Strict
        );
        assert!(Compatibility::parse("loose").is_err());
    }

    #[test]
    fn today_is_a_well_formed_date_the_rules_can_parse() {
        let today = today();
        assert!(
            brake::rules::parse_date(&today).is_some(),
            "today() produced an unparseable date: {today}"
        );
        assert_eq!(today.len(), 10, "{today}");
    }

    #[test]
    fn baseline_flag_reads_the_shapes_it_documents() {
        assert!(matches!(
            Baseline::from_flag("v1.2.0"),
            Baseline::Rev { rev } if rev == "v1.2.0"
        ));
        assert!(matches!(
            Baseline::from_flag("latest-tag:v*"),
            Baseline::LatestTag { pattern } if pattern == "v*"
        ));
        assert!(matches!(
            Baseline::from_flag("merge-base:origin/main"),
            Baseline::GitMergeBase { reference } if reference == "origin/main"
        ));
        assert!(matches!(
            Baseline::from_flag("origin/main:api/openapi.yaml"),
            Baseline::Git { spec } if spec == "origin/main:api/openapi.yaml"
        ));
    }

    #[test]
    fn level_names_round_trip() {
        for level in [
            Compatibility::Wire,
            Compatibility::WireJson,
            Compatibility::Surface,
            Compatibility::Strict,
        ] {
            assert_eq!(
                Compatibility::parse(level.name()).expect("round trip"),
                level
            );
        }
    }
}