supa_mdx_lint/
output.rs

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
use std::{io::Write, str::FromStr};

use anyhow::Result;

use crate::{app_error, errors::LintError};

#[cfg(feature = "pretty")]
pub mod pretty;
pub mod rdf;
pub mod simple;

#[derive(Debug)]
pub struct LintOutput {
    file_path: String,
    errors: Vec<LintError>,
}

impl LintOutput {
    pub fn new(file_path: impl AsRef<str>, errors: Vec<LintError>) -> Self {
        Self {
            file_path: file_path.as_ref().to_string(),
            errors,
        }
    }

    pub fn file_path(&self) -> &str {
        &self.file_path
    }

    pub fn errors(&self) -> &[LintError] {
        &self.errors
    }
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum OutputFormatter {
    #[cfg(feature = "pretty")]
    Pretty(pretty::PrettyFormatter),
    Simple(simple::SimpleFormatter),
    Rdf(rdf::RdfFormatter),
}

impl OutputFormatter {
    pub fn format<Writer: Write>(&self, output: &[LintOutput], io: &mut Writer) -> Result<()> {
        match self {
            #[cfg(feature = "pretty")]
            Self::Pretty(formatter) => formatter.format(output, io),
            Self::Simple(formatter) => formatter.format(output, io),
            Self::Rdf(formatter) => formatter.format(output, io),
        }
    }

    pub fn should_log_metadata(&self) -> bool {
        match self {
            #[cfg(feature = "pretty")]
            Self::Pretty(formatter) => formatter.should_log_metadata(),
            Self::Simple(formatter) => formatter.should_log_metadata(),
            Self::Rdf(formatter) => formatter.should_log_metadata(),
        }
    }
}

impl FromStr for OutputFormatter {
    type Err = app_error::PublicError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            #[cfg(feature = "pretty")]
            "pretty" => Ok(Self::Pretty(pretty::PrettyFormatter)),
            "simple" => Ok(Self::Simple(simple::SimpleFormatter)),
            "rdf" => Ok(Self::Rdf(rdf::RdfFormatter)),
            other => Err(app_error::PublicError::VariantNotFound(other.to_string())),
        }
    }
}