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

//! Runtime event severity level.

/// Runtime event level used by the local database layer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum DbRuntimeEventLevel {
    /// Diagnostic trace event.
    Trace,
    /// Diagnostic debug event.
    Debug,
    /// Informational event.
    Info,
    /// Warning event.
    Warn,
    /// Error event.
    Error,
}

impl DbRuntimeEventLevel {
    /// Converts the level to its stable integer representation.
    pub fn to_i16(self) -> i16 {
        match self {
            Self::Trace => return 0,
            Self::Debug => return 1,
            Self::Info => return 2,
            Self::Warn => return 3,
            Self::Error => return 4,
        }
    }

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