use std::fmt;
#[derive(Debug)]
pub enum CliError {
RuntimeError(anyhow::Error),
PagerExit(i32),
OnnxRuntimeMissing { hint: String },
}
impl CliError {
#[must_use]
pub fn exit_code(&self) -> i32 {
match self {
CliError::RuntimeError(_) => 1,
CliError::PagerExit(code) => *code,
CliError::OnnxRuntimeMissing { .. } => 65,
}
}
#[allow(dead_code)]
#[must_use]
pub fn runtime(err: impl Into<anyhow::Error>) -> Self {
CliError::RuntimeError(err.into())
}
#[must_use]
pub fn pager_exit(code: i32) -> Self {
CliError::PagerExit(code)
}
}
impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CliError::RuntimeError(err) => write!(f, "{err}"),
CliError::PagerExit(code) => write!(f, "pager exited with code {code}"),
CliError::OnnxRuntimeMissing { hint } => {
write!(f, "ONNX Runtime not found: {hint}")
}
}
}
}
impl std::error::Error for CliError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
CliError::RuntimeError(err) => err.source(),
CliError::PagerExit(_) | CliError::OnnxRuntimeMissing { .. } => None,
}
}
}
impl From<anyhow::Error> for CliError {
fn from(err: anyhow::Error) -> Self {
CliError::RuntimeError(err)
}
}