nextest_runner/list/
output_format.rs

1// Copyright (c) The nextest Contributors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use crate::{errors::WriteTestListError, write_str::WriteStr};
5use owo_colors::Style;
6use serde::Serialize;
7
8/// Output formats for nextest.
9#[derive(Copy, Clone, Debug, Eq, PartialEq)]
10#[cfg_attr(test, derive(test_strategy::Arbitrary))]
11#[non_exhaustive]
12pub enum OutputFormat {
13    /// A human-readable output format.
14    Human {
15        /// Whether to produce verbose output.
16        verbose: bool,
17    },
18
19    /// One test per line output format.
20    Oneline {
21        /// Whether to produce verbose output.
22        verbose: bool,
23    },
24
25    /// Machine-readable output format.
26    Serializable(SerializableFormat),
27}
28
29/// A serialized, machine-readable output format.
30#[derive(Copy, Clone, Debug, Eq, PartialEq)]
31#[cfg_attr(test, derive(test_strategy::Arbitrary))]
32#[non_exhaustive]
33pub enum SerializableFormat {
34    /// JSON with no whitespace.
35    Json,
36    /// JSON, prettified.
37    JsonPretty,
38}
39
40impl SerializableFormat {
41    /// Write this data in the given format to the writer.
42    pub fn to_writer(
43        self,
44        value: &impl Serialize,
45        writer: &mut dyn WriteStr,
46    ) -> Result<(), WriteTestListError> {
47        let out = match self {
48            SerializableFormat::Json => {
49                serde_json::to_string(value).map_err(WriteTestListError::Json)?
50            }
51            SerializableFormat::JsonPretty => {
52                serde_json::to_string_pretty(value).map_err(WriteTestListError::Json)?
53            }
54        };
55
56        writer.write_str(&out).map_err(WriteTestListError::Io)
57    }
58}
59
60/// Styles for displaying test list output.
61#[derive(Clone, Debug, Default)]
62pub struct Styles {
63    pub(crate) binary_id: Style,
64    pub(crate) test_name: Style,
65    pub(crate) module_path: Style,
66    pub(crate) field: Style,
67    pub(crate) count: Style,
68}
69
70impl Styles {
71    /// Colorizes the styles for terminal output.
72    pub fn colorize(&mut self) {
73        self.binary_id = Style::new().magenta().bold();
74        self.test_name = Style::new().blue().bold();
75        self.field = Style::new().yellow().bold();
76        self.module_path = Style::new().cyan();
77        self.count = Style::new().bold();
78    }
79}