Skip to main content

ephemeris_report/
format.rs

1use std::fs;
2use std::path::Path;
3
4use facet::Facet;
5
6use crate::{ConvertError, Converted, clippy, diff, junit};
7
8/// The converters of native output.
9#[derive(Clone, Copy, Debug, Eq, Facet, PartialEq)]
10#[facet(rename_all = "lowercase")]
11#[repr(u8)]
12pub enum Format {
13    /// `JUnit` XML.
14    Junit,
15    /// Cargo JSON messages of clippy.
16    Clippy,
17    /// `cargo fmt --check` diffs.
18    Diff,
19}
20
21impl Format {
22    /// The name of the attachment that holds the native output.
23    #[must_use]
24    pub const fn attachment(self) -> &'static str {
25        match self {
26            Self::Junit => "junit.xml",
27            Self::Clippy => "clippy.json",
28            Self::Diff => "diff.txt",
29        }
30    }
31
32    /// Convert native output. `root` is the working directory of the check,
33    /// which makes paths in diffs relative.
34    ///
35    /// # Errors
36    ///
37    /// Returns an error when the output is not valid for the format.
38    pub fn convert(self, native: &[u8], root: &Path) -> Result<Converted, ConvertError> {
39        match self {
40            Self::Junit => junit::convert(native),
41            Self::Clippy => clippy::convert(native),
42            Self::Diff => {
43                let root = fs::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
44                diff::convert(native, &root)
45            }
46        }
47    }
48}