use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum CheckResult {
Passed { message: String },
Failed { message: String, details: Option<String> },
Skipped { reason: String },
Warning { message: String },
}
impl CheckResult {
pub fn passed(message: impl Into<String>) -> Self {
Self::Passed { message: message.into() }
}
pub fn failed(message: impl Into<String>) -> Self {
Self::Failed { message: message.into(), details: None }
}
pub fn failed_with_details(message: impl Into<String>, details: impl Into<String>) -> Self {
Self::Failed { message: message.into(), details: Some(details.into()) }
}
pub fn skipped(reason: impl Into<String>) -> Self {
Self::Skipped { reason: reason.into() }
}
pub fn warning(message: impl Into<String>) -> Self {
Self::Warning { message: message.into() }
}
pub fn is_passed(&self) -> bool {
matches!(self, Self::Passed { .. })
}
pub fn is_failed(&self) -> bool {
matches!(self, Self::Failed { .. })
}
pub fn is_warning(&self) -> bool {
matches!(self, Self::Warning { .. })
}
pub fn is_skipped(&self) -> bool {
matches!(self, Self::Skipped { .. })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_check_result_passed() {
let result = CheckResult::passed("test passed");
assert!(result.is_passed());
assert!(!result.is_failed());
}
#[test]
fn test_check_result_failed() {
let result = CheckResult::failed("test failed");
assert!(result.is_failed());
assert!(!result.is_passed());
}
#[test]
fn test_check_result_failed_with_details() {
let result = CheckResult::failed_with_details("failed", "some details");
assert!(result.is_failed());
if let CheckResult::Failed { details, .. } = result {
assert_eq!(details, Some("some details".to_string()));
}
}
#[test]
fn test_check_result_warning() {
let result = CheckResult::warning("warning message");
assert!(result.is_warning());
assert!(!result.is_failed());
}
#[test]
fn test_check_result_skipped() {
let result = CheckResult::skipped("skipped reason");
assert!(result.is_skipped());
}
}