use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum CheckStatus {
Pass,
Warn,
Fail,
}
#[derive(Debug, Clone, Serialize)]
pub struct CheckItem {
pub category: String,
pub name: String,
pub status: CheckStatus,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub hint: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub detail: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct DoctorSummary {
pub total: usize,
pub passed: usize,
pub warned: usize,
pub failed: usize,
}
impl DoctorSummary {
#[must_use]
pub fn from_items(items: &[CheckItem]) -> Self {
let total = items.len();
let passed = items
.iter()
.filter(|i| i.status == CheckStatus::Pass)
.count();
let warned = items
.iter()
.filter(|i| i.status == CheckStatus::Warn)
.count();
let failed = items
.iter()
.filter(|i| i.status == CheckStatus::Fail)
.count();
Self {
total,
passed,
warned,
failed,
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct DoctorReport {
pub items: Vec<CheckItem>,
pub summary: DoctorSummary,
pub timestamp: String,
}
impl DoctorReport {
#[must_use]
pub fn from_items(items: Vec<CheckItem>) -> Self {
let summary = DoctorSummary::from_items(&items);
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or_else(|_| "unknown".to_string(), |d| format!("{}s", d.as_secs()));
Self {
items,
summary,
timestamp,
}
}
}
pub trait HealthCheck: Send + Sync {
fn category(&self) -> &'static str;
fn run(&self) -> Vec<CheckItem>;
}
impl CheckItem {
#[must_use]
pub fn pass(
category: impl Into<String>,
name: impl Into<String>,
message: impl Into<String>,
) -> Self {
Self {
category: category.into(),
name: name.into(),
status: CheckStatus::Pass,
message: message.into(),
hint: None,
detail: None,
}
}
#[must_use]
pub fn warn(
category: impl Into<String>,
name: impl Into<String>,
message: impl Into<String>,
hint: impl Into<String>,
) -> Self {
Self {
category: category.into(),
name: name.into(),
status: CheckStatus::Warn,
message: message.into(),
hint: Some(hint.into()),
detail: None,
}
}
#[must_use]
pub fn fail(
category: impl Into<String>,
name: impl Into<String>,
message: impl Into<String>,
hint: impl Into<String>,
) -> Self {
Self {
category: category.into(),
name: name.into(),
status: CheckStatus::Fail,
message: message.into(),
hint: Some(hint.into()),
detail: None,
}
}
#[must_use]
pub fn with_detail(mut self, detail: impl Into<String>) -> Self {
self.detail = Some(detail.into());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_should_calculate_summary_counts() {
let items = vec![
CheckItem::pass("cat", "a", "ok"),
CheckItem::pass("cat", "b", "ok"),
CheckItem::warn("cat", "c", "meh", "fix it"),
CheckItem::fail("cat", "d", "bad", "fix now"),
];
let summary = DoctorSummary::from_items(&items);
assert_eq!(summary.total, 4);
assert_eq!(summary.passed, 2);
assert_eq!(summary.warned, 1);
assert_eq!(summary.failed, 1);
}
#[test]
fn test_should_create_report_from_items() {
let items = vec![
CheckItem::pass("cat", "a", "ok"),
CheckItem::fail("cat", "b", "bad", "fix"),
];
let report = DoctorReport::from_items(items);
assert_eq!(report.summary.total, 2);
assert_eq!(report.summary.passed, 1);
assert_eq!(report.summary.failed, 1);
assert!(!report.timestamp.is_empty());
}
#[test]
fn test_should_serialize_report_to_json() {
let items = vec![
CheckItem::pass("platform_cli", "gh installed", "gh found").with_detail("v2.65.0"),
CheckItem::fail(
"platform_cli",
"gc auth",
"not authenticated",
"run gc auth login",
),
];
let report = DoctorReport::from_items(items);
let json = serde_json::to_string(&report).expect("serialize");
assert!(json.contains("\"status\":\"pass\""));
assert!(json.contains("\"status\":\"fail\""));
assert!(json.contains("\"detail\":\"v2.65.0\""));
assert!(json.contains("gc auth login"));
}
#[test]
fn test_should_skip_none_fields_in_json() {
let item = CheckItem::pass("cat", "name", "msg");
let json = serde_json::to_string(&item).expect("serialize");
assert!(!json.contains("\"hint\""));
assert!(!json.contains("\"detail\""));
}
#[test]
fn test_should_create_pass_item() {
let item = CheckItem::pass("cat", "name", "message");
assert_eq!(item.status, CheckStatus::Pass);
assert!(item.hint.is_none());
}
#[test]
fn test_should_create_warn_item_with_hint() {
let item = CheckItem::warn("cat", "name", "message", "hint text");
assert_eq!(item.status, CheckStatus::Warn);
assert_eq!(item.hint.as_deref(), Some("hint text"));
}
#[test]
fn test_should_create_fail_item_with_hint() {
let item = CheckItem::fail("cat", "name", "message", "fix this");
assert_eq!(item.status, CheckStatus::Fail);
assert_eq!(item.hint.as_deref(), Some("fix this"));
}
#[test]
fn test_should_attach_detail_via_builder() {
let item = CheckItem::pass("cat", "name", "msg").with_detail("v1.0.0");
assert_eq!(item.detail.as_deref(), Some("v1.0.0"));
}
}