deps_cli/format/mod.rs
1//! Output formatters for a [`crate::report::CheckReport`] (FR-006, FR-007).
2
3pub mod json;
4pub mod sarif;
5pub mod table;
6
7/// Whether an `update` run only previewed its plan without writing any change to disk.
8///
9/// Shared by [`json::render_update`]/[`json::update_to_document`] and [`table::render_update`]
10/// so `--dry-run` cannot be transposed with another positional flag at either call site.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum DryRun {
13 /// `--dry-run` was passed: the plan was computed but never written to disk.
14 Yes,
15 /// A real run: applied edits were written to disk.
16 No,
17}
18
19impl DryRun {
20 /// Builds a `DryRun` from the `--dry-run` CLI flag (`true` means [`Self::Yes`]).
21 ///
22 /// The single, explicitly named conversion point from that boundary's `bool`
23 /// representation (issue #1436 S1) — deliberately not a `From<bool>` impl; see
24 /// `deps_core::cache::NetworkMode::from_offline_flag`'s doc for why an ambient blanket
25 /// impl defeats the point of typing this API.
26 #[must_use]
27 pub fn from_flag(dry_run: bool) -> Self {
28 if dry_run { Self::Yes } else { Self::No }
29 }
30}
31
32/// Renders `severity` as the lowercase token used by both output formats.
33#[must_use]
34pub fn severity_str(severity: deps_core::diagnostic::Severity) -> &'static str {
35 use deps_core::diagnostic::Severity;
36 match severity {
37 Severity::Error => "error",
38 Severity::Warning => "warning",
39 Severity::Information => "information",
40 Severity::Hint => "hint",
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47 use deps_core::diagnostic::Severity;
48
49 #[test]
50 fn test_severity_str_covers_every_lsp_severity() {
51 assert_eq!(severity_str(Severity::Error), "error");
52 assert_eq!(severity_str(Severity::Warning), "warning");
53 assert_eq!(severity_str(Severity::Information), "information");
54 assert_eq!(severity_str(Severity::Hint), "hint");
55 }
56}