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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) The diem-devtools Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

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

use crate::errors::OutputFormatParseError;
use serde::Serialize;
use std::{fmt, io, str::FromStr};

/// Output formats for nextest.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
pub enum OutputFormat {
    /// A plain, user-readable output format.
    Plain,

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

impl OutputFormat {
    /// Returns string representations of all currently-known, valid variants.
    pub fn variants() -> &'static [&'static str] {
        &["plain", "json", "json-pretty"]
    }
}

impl fmt::Display for OutputFormat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OutputFormat::Plain => write!(f, "plain"),
            OutputFormat::Serializable(SerializableFormat::Json) => write!(f, "json"),
            OutputFormat::Serializable(SerializableFormat::JsonPretty) => write!(f, "json-pretty"),
        }
    }
}

impl FromStr for OutputFormat {
    type Err = OutputFormatParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let val = match s {
            "plain" => OutputFormat::Plain,
            "json" => OutputFormat::Serializable(SerializableFormat::Json),
            "json-pretty" => OutputFormat::Serializable(SerializableFormat::JsonPretty),
            other => return Err(OutputFormatParseError::new(other)),
        };
        Ok(val)
    }
}

impl Default for OutputFormat {
    fn default() -> Self {
        OutputFormat::Plain
    }
}

/// A serialized, machine-readable output format.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
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: impl io::Write,
    ) -> serde_json::Result<()> {
        match self {
            SerializableFormat::Json => serde_json::to_writer(writer, value),
            SerializableFormat::JsonPretty => serde_json::to_writer_pretty(writer, value),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn output_format_variants() {
        for &variant in OutputFormat::variants() {
            variant.parse::<OutputFormat>().expect("variant is valid");
        }
    }
}