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    /// Machine-readable output format.
20    Serializable(SerializableFormat),
21}
22
23/// A serialized, machine-readable output format.
24#[derive(Copy, Clone, Debug, Eq, PartialEq)]
25#[cfg_attr(test, derive(test_strategy::Arbitrary))]
26#[non_exhaustive]
27pub enum SerializableFormat {
28    /// JSON with no whitespace.
29    Json,
30    /// JSON, prettified.
31    JsonPretty,
32}
33
34impl SerializableFormat {
35    /// Write this data in the given format to the writer.
36    pub fn to_writer(
37        self,
38        value: &impl Serialize,
39        writer: &mut dyn WriteStr,
40    ) -> Result<(), WriteTestListError> {
41        let out = match self {
42            SerializableFormat::Json => {
43                serde_json::to_string(value).map_err(WriteTestListError::Json)?
44            }
45            SerializableFormat::JsonPretty => {
46                serde_json::to_string_pretty(value).map_err(WriteTestListError::Json)?
47            }
48        };
49
50        writer.write_str(&out).map_err(WriteTestListError::Io)
51    }
52}
53
54#[derive(Clone, Debug, Default)]
55pub(crate) struct Styles {
56    pub(crate) binary_id: Style,
57    pub(crate) test_name: Style,
58    pub(crate) module_path: Style,
59    pub(crate) field: Style,
60}
61
62impl Styles {
63    pub(crate) fn colorize(&mut self) {
64        self.binary_id = Style::new().magenta().bold();
65        self.test_name = Style::new().blue().bold();
66        self.field = Style::new().yellow().bold();
67        self.module_path = Style::new().cyan();
68    }
69}