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::*;
    }
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub enum LintLevel {
    Error,
    Warning,
    Notice,
}
#[derive(Debug)]
#[non_exhaustive]
pub struct LintMsg {
    pub level: LintLevel,
    pub msg: String,
}
impl LintMsg {
    #[inline]
    pub(crate) fn error(msg: String) -> LintMsg {
        LintMsg {
            level: LintLevel::Error,
            msg,
        }
    }
}
#[derive(Debug, Default)]
pub struct LintReport {
    msgs: Vec<LintMsg>,
}
impl LintReport {
    #[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)],
        }
    }
}
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()
        }
    }
}