use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
    Info,
    Warning,
    Error,
}

impl std::fmt::Display for Severity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Info => write!(f, "info"),
            Self::Warning => write!(f, "warning"),
            Self::Error => write!(f, "error"),
        }
    }
}

#[derive(Clone, Deserialize, Serialize)]
pub struct Message {
    pub severity: Severity,
    pub content: String,
    pub filename: String,
    pub line: u64,
}

#[cfg(test)]
mod tests {
    use crate::message::Severity;

    #[test]
    fn test_severity_serialisation() {
        assert_eq!(serde_yml::to_string(&Severity::Error).unwrap(), "error\n");
        assert_eq!(
            serde_yml::to_string(&Severity::Warning).unwrap(),
            "warning\n"
        );
        assert_eq!(serde_yml::to_string(&Severity::Info).unwrap(), "info\n");
    }
}
