use std::path::PathBuf;
use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum CheckStatus {
Ok,
Warn,
Fail,
}
#[derive(Debug, Clone, Serialize)]
pub struct CheckResult {
pub id: String,
pub status: CheckStatus,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub fix_suggestion: Option<String>,
#[serde(skip)]
pub safe_fix: Option<FixAction>,
}
impl CheckResult {
pub fn ok(id: impl Into<String>, message: impl Into<String>) -> Self {
Self {
id: id.into(),
status: CheckStatus::Ok,
message: message.into(),
fix_suggestion: None,
safe_fix: None,
}
}
pub fn warn(
id: impl Into<String>,
message: impl Into<String>,
fix_suggestion: impl Into<String>,
) -> Self {
Self {
id: id.into(),
status: CheckStatus::Warn,
message: message.into(),
fix_suggestion: Some(fix_suggestion.into()),
safe_fix: None,
}
}
pub fn fail(
id: impl Into<String>,
message: impl Into<String>,
fix_suggestion: impl Into<String>,
) -> Self {
Self {
id: id.into(),
status: CheckStatus::Fail,
message: message.into(),
fix_suggestion: Some(fix_suggestion.into()),
safe_fix: None,
}
}
pub fn with_safe_fix(mut self, fix: FixAction) -> Self {
self.safe_fix = Some(fix);
self
}
}
#[derive(Debug, Clone)]
pub enum FixAction {
InstallSkill(String),
RemoveStaleSupervisorPid { path: PathBuf, observed_pid: u32 },
}
impl FixAction {
pub fn describe(&self) -> (&'static str, &'static str, String) {
match self {
FixAction::InstallSkill(name) => ("install", "skill", name.clone()),
FixAction::RemoveStaleSupervisorPid { path, .. } => {
("remove", "supervisor-pid", path.display().to_string())
}
}
}
}
#[derive(Debug, Clone, Copy, Default, Serialize)]
pub struct Summary {
pub ok: usize,
pub warn: usize,
pub fail: usize,
}
impl Summary {
pub fn tally(results: &[CheckResult]) -> Self {
let mut s = Summary::default();
for r in results {
match r.status {
CheckStatus::Ok => s.ok += 1,
CheckStatus::Warn => s.warn += 1,
CheckStatus::Fail => s.fail += 1,
}
}
s
}
pub fn any_fail(&self) -> bool {
self.fail > 0
}
}