use std::fmt;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum DetectionError {
ToolNotFound { tool: String },
ToolFailed {
tool: String,
exit_code: Option<i32>,
stderr: String,
},
Timeout { tool: String, timeout_secs: f64 },
ParseError { backend: String, message: String },
SysfsReadError { path: String, message: String },
}
impl fmt::Display for DetectionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ToolNotFound { tool } => {
write!(f, "{}: tool not found on $PATH", tool)
}
Self::ToolFailed {
tool,
exit_code,
stderr,
} => {
write!(
f,
"{}: exited with code {} — {}",
tool,
exit_code
.map(|c| c.to_string())
.unwrap_or_else(|| "signal".into()),
stderr.lines().next().unwrap_or("(no output)")
)
}
Self::Timeout { tool, timeout_secs } => {
write!(f, "{}: timed out after {:.1}s", tool, timeout_secs)
}
Self::ParseError { backend, message } => {
write!(f, "{}: parse error — {}", backend, message)
}
Self::SysfsReadError { path, message } => {
write!(f, "sysfs {}: {}", path, message)
}
}
}
}
impl std::error::Error for DetectionError {}