Skip to main content

webpkit_cli/
report.rs

1//! Verbosity-aware status reporting.
2//!
3//! Every human-readable line goes to **stderr** so that `-o -` can stream image
4//! bytes to stdout untouched. `-q`/`--quiet` silences everything but errors;
5//! `-v`/`--verbose` adds per-stage detail.
6
7use crate::error::CliError;
8
9/// How much status output to emit.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum Level {
12    /// Errors only.
13    Quiet,
14    /// A one-line summary per operation (the default).
15    Normal,
16    /// Per-stage detail.
17    Verbose,
18}
19
20/// Emits status, detail, and error lines to stderr according to a [`Level`].
21#[derive(Debug)]
22pub struct Reporter {
23    level: Level,
24}
25
26impl Reporter {
27    /// Build a reporter from the parsed `-v` count and `-q` flag. `quiet` wins.
28    #[must_use]
29    pub const fn new(verbose: u8, quiet: bool) -> Self {
30        let level = if quiet {
31            Level::Quiet
32        } else if verbose >= 1 {
33            Level::Verbose
34        } else {
35            Level::Normal
36        };
37        Self { level }
38    }
39
40    /// This reporter's [`Level`].
41    #[must_use]
42    pub const fn level(&self) -> Level {
43        self.level
44    }
45
46    /// A one-line summary, shown at [`Level::Normal`] and above.
47    #[allow(
48        clippy::print_stderr,
49        reason = "the CLI reports human-readable status on stderr by design"
50    )]
51    pub fn status(&self, message: &str) {
52        if self.level != Level::Quiet {
53            eprintln!("{message}");
54        }
55    }
56
57    /// A per-stage detail line, shown only at [`Level::Verbose`].
58    #[allow(
59        clippy::print_stderr,
60        reason = "the CLI reports human-readable status on stderr by design"
61    )]
62    pub fn detail(&self, message: &str) {
63        if self.level == Level::Verbose {
64            eprintln!("{message}");
65        }
66    }
67}
68
69/// Print an error line to stderr, prefixed with `error:`.
70///
71/// A free function rather than a [`Reporter`] method because errors are always
72/// shown — even under `--quiet` — so verbosity never applies.
73#[allow(
74    clippy::print_stderr,
75    reason = "the CLI reports errors on stderr by design"
76)]
77pub fn error(err: &CliError) {
78    eprintln!("error: {err}");
79}
80
81/// Print a non-fatal warning to stderr (always shown), prefixed with `warning:`.
82#[allow(
83    clippy::print_stderr,
84    reason = "the CLI reports warnings on stderr by design"
85)]
86pub fn warn(message: &str) {
87    eprintln!("warning: {message}");
88}