Skip to main content

base_console/
output_format.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4/// The representation written by the console.
5#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
6pub enum OutputFormat {
7    /// Text for a terminal, JSON for a redirected stream or custom writer.
8    #[default]
9    Auto,
10    /// Human-readable text, including indented causes and captured diagnostics.
11    Text,
12    /// One structured JSON object per line.
13    Json,
14}
15
16impl OutputFormat {
17    pub(crate) fn resolve(self, terminal: bool) -> Self {
18        match self {
19            Self::Auto if terminal => Self::Text,
20            Self::Auto => Self::Json,
21            format => format,
22        }
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::OutputFormat::*;
29
30    #[test]
31    fn automatic_selection_tracks_the_destination_and_explicit_formats_win() {
32        assert_eq!(Auto.resolve(true), Text);
33        assert_eq!(Auto.resolve(false), Json);
34        for terminal in [true, false] {
35            assert_eq!(Text.resolve(terminal), Text);
36            assert_eq!(Json.resolve(terminal), Json);
37        }
38    }
39}