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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::{
    errors::{TebError, BAD_SPEC_FILE_EXTENSION},
    util::open_spec_file,
    DEFAULT_SPEC_PATH,
};
use std::path::PathBuf;

cfg_if::cfg_if! {
    if #[cfg(feature = "yaml")] {
        mod yaml;
        pub(crate) use yaml::*;
    }
}

cfg_if::cfg_if! {
    if #[cfg(feature = "toml")] {
        mod toml;
        pub(crate) use self::toml::*;
    }
}

/// A lint level.
///
/// Denotes how harmful a lint message is.
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub enum LintLevel {
    /// definitive error
    Error,
    /// not an immediate error but may become one in a close release
    Warning,
    /// something to notice
    Notice,
}

/// A lint message.
#[derive(Debug)]
#[non_exhaustive]
pub struct LintMsg {
    /// The level of the message.
    pub level: LintLevel,
    /// The lint message itself.
    pub msg: String,
}

impl LintMsg {
    #[inline]
    pub(crate) fn error(msg: String) -> LintMsg {
        LintMsg {
            level: LintLevel::Error,
            msg,
        }
    }
}

/// A lint report.
///
/// A lint report represents a list of lint messages generated during
/// parsing or linting of a specification file.
#[derive(Debug, Default)]
pub struct LintReport {
    msgs: Vec<LintMsg>,
}

impl LintReport {
    /// Returns the list of lint messages.
    #[inline]
    pub fn messages(&self) -> &[LintMsg] {
        &self.msgs
    }

    #[inline]
    pub(crate) fn new() -> Self {
        Self::default()
    }

    #[inline]
    pub(crate) fn is_empty(&self) -> bool {
        self.msgs.is_empty()
    }

    #[inline]
    pub(crate) fn error(&mut self, msg: String) {
        self.msgs.push(LintMsg::error(msg));
    }

    #[inline]
    pub(crate) fn from_error(msg: String) -> Self {
        Self {
            msgs: vec![LintMsg::error(msg)],
        }
    }
}

/// Checks a specification file for errors.
pub fn lint(spec: Option<PathBuf>) -> Result<LintReport, TebError> {
    let path = match spec {
        Some(pb) => pb,
        None => DEFAULT_SPEC_PATH.into(),
    };

    match path.extension() {
        #[cfg(feature = "yaml")]
        Some(e) if e == "yaml" => YamlLinter::from_file(open_spec_file(&path)?),
        #[cfg(feature = "toml")]
        Some(e) if e == "toml" => TomlLinter::from_file(open_spec_file(&path)?),
        Some(e) => {
            log::error!(
                "specification file extension {:?} isn't supported: {:?}",
                e,
                path
            );
            BAD_SPEC_FILE_EXTENSION.into()
        }
        None => {
            log::error!(
                "specification file name must have a markup language extension: {:?}",
                path
            );
            BAD_SPEC_FILE_EXTENSION.into()
        }
    }
}