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
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

// clippy complains about the Arbitrary impl for OutputFormat
#![allow(clippy::unit_arg)]

use crate::{errors::WriteTestListError, write_str::WriteStr};
use owo_colors::Style;
use serde::Serialize;

/// Output formats for nextest.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[non_exhaustive]
pub enum OutputFormat {
    /// A human-readable output format.
    Human {
        /// Whether to produce verbose output.
        verbose: bool,
    },

    /// Machine-readable output format.
    Serializable(SerializableFormat),
}

/// A serialized, machine-readable output format.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
#[non_exhaustive]
pub enum SerializableFormat {
    /// JSON with no whitespace.
    Json,
    /// JSON, prettified.
    JsonPretty,
}

impl SerializableFormat {
    /// Write this data in the given format to the writer.
    pub fn to_writer(
        self,
        value: &impl Serialize,
        writer: &mut dyn WriteStr,
    ) -> Result<(), WriteTestListError> {
        let out = match self {
            SerializableFormat::Json => {
                serde_json::to_string(value).map_err(WriteTestListError::Json)?
            }
            SerializableFormat::JsonPretty => {
                serde_json::to_string_pretty(value).map_err(WriteTestListError::Json)?
            }
        };

        writer.write_str(&out).map_err(WriteTestListError::Io)
    }
}

#[derive(Clone, Debug, Default)]
pub(crate) struct Styles {
    pub(crate) binary_id: Style,
    pub(crate) test_name: Style,
    pub(crate) module_path: Style,
    pub(crate) field: Style,
}

impl Styles {
    pub(crate) fn colorize(&mut self) {
        self.binary_id = Style::new().magenta().bold();
        self.test_name = Style::new().blue().bold();
        self.field = Style::new().yellow().bold();
        self.module_path = Style::new().cyan();
    }
}