// file: kb_lib/src/db/types/analysis_signal_severity.rs

//! Analysis signal severity.

/// Severity for one analysis signal.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum AnalysisSignalSeverity {
    /// Informational signal.
    Info,
    /// Low-importance signal.
    Low,
    /// Medium-importance signal.
    Medium,
    /// High-importance signal.
    High,
    /// Critical signal.
    Critical,
}

impl AnalysisSignalSeverity {
    /// Converts the severity to its stable integer representation.
    pub fn to_i16(self) -> i16 {
        match self {
            Self::Info => return 0,
            Self::Low => return 1,
            Self::Medium => return 2,
            Self::High => return 3,
            Self::Critical => return 4,
        }
    }

    /// Restores a severity from its stable integer representation.
    pub fn from_i16(value: i16) -> Result<Self, crate::Error> {
        match value {
            0 => return Ok(Self::Info),
            1 => return Ok(Self::Low),
            2 => return Ok(Self::Medium),
            3 => return Ok(Self::High),
            4 => return Ok(Self::Critical),
            _ => {
                return Err(crate::Error::Db(format!(
                    "invalid AnalysisSignalSeverity value: {}",
                    value
                )));
            },
        }
    }
}
