Skip to main content

entrenar/storage/preflight/
check_result.rs

1//! Check result types for preflight validation.
2
3use serde::{Deserialize, Serialize};
4
5/// Result of a single preflight check
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub enum CheckResult {
8    /// Check passed
9    Passed { message: String },
10    /// Check failed
11    Failed { message: String, details: Option<String> },
12    /// Check was skipped
13    Skipped { reason: String },
14    /// Check produced a warning (non-fatal)
15    Warning { message: String },
16}
17
18impl CheckResult {
19    /// Create a passed result
20    pub fn passed(message: impl Into<String>) -> Self {
21        Self::Passed { message: message.into() }
22    }
23
24    /// Create a failed result
25    pub fn failed(message: impl Into<String>) -> Self {
26        Self::Failed { message: message.into(), details: None }
27    }
28
29    /// Create a failed result with details
30    pub fn failed_with_details(message: impl Into<String>, details: impl Into<String>) -> Self {
31        Self::Failed { message: message.into(), details: Some(details.into()) }
32    }
33
34    /// Create a skipped result
35    pub fn skipped(reason: impl Into<String>) -> Self {
36        Self::Skipped { reason: reason.into() }
37    }
38
39    /// Create a warning result
40    pub fn warning(message: impl Into<String>) -> Self {
41        Self::Warning { message: message.into() }
42    }
43
44    /// Check if the result is passed
45    pub fn is_passed(&self) -> bool {
46        matches!(self, Self::Passed { .. })
47    }
48
49    /// Check if the result is failed
50    pub fn is_failed(&self) -> bool {
51        matches!(self, Self::Failed { .. })
52    }
53
54    /// Check if the result is a warning
55    pub fn is_warning(&self) -> bool {
56        matches!(self, Self::Warning { .. })
57    }
58
59    /// Check if the result is skipped
60    pub fn is_skipped(&self) -> bool {
61        matches!(self, Self::Skipped { .. })
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    // =========================================================================
70    // CheckResult Tests
71    // =========================================================================
72
73    #[test]
74    fn test_check_result_passed() {
75        let result = CheckResult::passed("test passed");
76        assert!(result.is_passed());
77        assert!(!result.is_failed());
78    }
79
80    #[test]
81    fn test_check_result_failed() {
82        let result = CheckResult::failed("test failed");
83        assert!(result.is_failed());
84        assert!(!result.is_passed());
85    }
86
87    #[test]
88    fn test_check_result_failed_with_details() {
89        let result = CheckResult::failed_with_details("failed", "some details");
90        assert!(result.is_failed());
91        if let CheckResult::Failed { details, .. } = result {
92            assert_eq!(details, Some("some details".to_string()));
93        }
94    }
95
96    #[test]
97    fn test_check_result_warning() {
98        let result = CheckResult::warning("warning message");
99        assert!(result.is_warning());
100        assert!(!result.is_failed());
101    }
102
103    #[test]
104    fn test_check_result_skipped() {
105        let result = CheckResult::skipped("skipped reason");
106        assert!(result.is_skipped());
107    }
108}