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
//! Output formatting for CLI commands.
//!
//! Provides [`OutputFormat`] to handle the dual human/JSON output pattern
//! without scattering `if json` checks throughout the codebase.
use serde::Serialize;
/// Controls how command output is rendered.
#[derive(Debug, Clone, Copy, Default)]
pub enum OutputFormat {
/// Human-readable styled output (the default).
#[default]
Human,
/// Machine-readable JSON output.
Json,
}
impl OutputFormat {
/// Returns `true` when human-readable output is active.
pub fn is_human(self) -> bool {
matches!(self, Self::Human)
}
/// Prints `data` as pretty JSON, or calls `human` to print styled output.
///
/// ```ignore
/// format.print(&jobs, || {
/// print_job_table(&jobs);
/// Ok(())
/// })?;
/// ```
pub fn print<T, E>(self, data: &T, human: impl FnOnce() -> Result<(), E>) -> Result<(), E>
where
T: Serialize,
E: From<serde_json::Error>,
{
match self {
Self::Human => human(),
Self::Json => {
println!("{}", serde_json::to_string_pretty(data)?);
Ok(())
}
}
}
/// Prints `data` as pretty JSON when in JSON mode; does nothing for human.
///
/// Useful when human output is handled separately (e.g. printed
/// incrementally in a loop) and only JSON needs a final dump.
pub fn print_json_only<T, E>(self, data: &T) -> Result<(), E>
where
T: Serialize,
E: From<serde_json::Error>,
{
if matches!(self, Self::Json) {
println!("{}", serde_json::to_string_pretty(data)?);
}
Ok(())
}
}