cradle-shared 0.1.0

Shared utilities in use by the Cradle agent and cli
Documentation
/// Check result struct
/// Each cradle check should return this struct after execution
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CheckResult {
    /// Calling plugin name
    pub plugin: String,
    /// Check name
    pub check: String,
    /// Result severity (see: [`Severity`])
    pub severity: Severity,
    /// Message included in the check's result
    pub message: String,
    /// Optional details for the message
    pub details: Option<String>,
}

/// Check result severity
/// Specifies how severe the check result should be classified as
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum Severity {
    /// Check passed
    Pass,
    /// Informational check
    Info,
    /// Warning check
    Warn,
    /// Check failed
    Fail,
    /// A critical error occurred
    Critical,
}

impl CheckResult {
    /// Small helper to create a pass result (`Severity::Pass`)
    pub fn pass(plugin: &str, check: &str, message: impl Into<String>) -> Self {
        Self {
            plugin: plugin.into(),
            check: check.into(),
            severity: Severity::Pass,
            message: message.into(),
            details: None,
        }
    }

    /// Small helper to create an informational result (`Severity::Info`)
    pub fn info(plugin: &str, check: &str, message: impl Into<String>) -> Self {
        Self {
            plugin: plugin.into(),
            check: check.into(),
            severity: Severity::Info,
            message: message.into(),
            details: None,
        }
    }

    /// Small helper to create a warning result (`Severity::Warn`)
    pub fn warn(plugin: &str, check: &str, message: impl Into<String>) -> Self {
        Self {
            plugin: plugin.into(),
            check: check.into(),
            severity: Severity::Warn,
            message: message.into(),
            details: None,
        }
    }

    /// Small helper to create a failure result (`Severity::Fail`)
    pub fn fail(plugin: &str, check: &str, message: impl Into<String>) -> Self {
        Self {
            plugin: plugin.into(),
            check: check.into(),
            severity: Severity::Fail,
            message: message.into(),
            details: None,
        }
    }

    /// Small helper to create a critical error result (`Severity::Critical`)
    pub fn critical(plugin: &str, check: &str, message: impl Into<String>) -> Self {
        Self {
            plugin: plugin.into(),
            check: check.into(),
            severity: Severity::Critical,
            message: message.into(),
            details: None,
        }
    }

    /// Adds details to the given check result
    pub fn with_details(mut self, details: impl Into<String>) -> Self {
        self.details = Some(details.into());
        self
    }
}