Skip to main content

cradle_shared/
checks.rs

1/// Check result struct
2/// Each cradle check should return this struct after execution
3#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
4pub struct CheckResult {
5    /// Calling plugin name
6    pub plugin: String,
7    /// Check name
8    pub check: String,
9    /// Result severity (see: [`Severity`])
10    pub severity: Severity,
11    /// Message included in the check's result
12    pub message: String,
13    /// Optional details for the message
14    pub details: Option<String>,
15}
16
17/// Check result severity
18/// Specifies how severe the check result should be classified as
19#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
20pub enum Severity {
21    /// Check passed
22    Pass,
23    /// Informational check
24    Info,
25    /// Warning check
26    Warn,
27    /// Check failed
28    Fail,
29    /// A critical error occurred
30    Critical,
31}
32
33impl CheckResult {
34    /// Small helper to create a pass result (`Severity::Pass`)
35    pub fn pass(plugin: &str, check: &str, message: impl Into<String>) -> Self {
36        Self {
37            plugin: plugin.into(),
38            check: check.into(),
39            severity: Severity::Pass,
40            message: message.into(),
41            details: None,
42        }
43    }
44
45    /// Small helper to create an informational result (`Severity::Info`)
46    pub fn info(plugin: &str, check: &str, message: impl Into<String>) -> Self {
47        Self {
48            plugin: plugin.into(),
49            check: check.into(),
50            severity: Severity::Info,
51            message: message.into(),
52            details: None,
53        }
54    }
55
56    /// Small helper to create a warning result (`Severity::Warn`)
57    pub fn warn(plugin: &str, check: &str, message: impl Into<String>) -> Self {
58        Self {
59            plugin: plugin.into(),
60            check: check.into(),
61            severity: Severity::Warn,
62            message: message.into(),
63            details: None,
64        }
65    }
66
67    /// Small helper to create a failure result (`Severity::Fail`)
68    pub fn fail(plugin: &str, check: &str, message: impl Into<String>) -> Self {
69        Self {
70            plugin: plugin.into(),
71            check: check.into(),
72            severity: Severity::Fail,
73            message: message.into(),
74            details: None,
75        }
76    }
77
78    /// Small helper to create a critical error result (`Severity::Critical`)
79    pub fn critical(plugin: &str, check: &str, message: impl Into<String>) -> Self {
80        Self {
81            plugin: plugin.into(),
82            check: check.into(),
83            severity: Severity::Critical,
84            message: message.into(),
85            details: None,
86        }
87    }
88
89    /// Adds details to the given check result
90    pub fn with_details(mut self, details: impl Into<String>) -> Self {
91        self.details = Some(details.into());
92        self
93    }
94}