use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Io {
path: std::path::PathBuf,
source: std::io::Error,
},
Parse {
line: usize,
message: String,
},
EmptyModel,
Unsupported {
message: String,
},
UnsupportedFormat {
ext: String,
supported: &'static [&'static str],
},
FeatureCount {
expected: usize,
got: usize,
},
BatchShape {
len: usize,
n_features: usize,
},
Backend {
backend: &'static str,
message: String,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io { path, source } => {
write!(f, "reading {}: {source}", path.display())
}
Error::Parse { line, message } => {
write!(f, "parse error on line {line}: {message}")
}
Error::EmptyModel => write!(f, "no decision trees found in model file"),
Error::Unsupported { message } => {
write!(f, "unsupported model: {message}")
}
Error::FeatureCount { expected, got } => write!(
f,
"model expects {expected} features, got {got}"
),
Error::BatchShape { len, n_features } => write!(
f,
"flat batch of length {len} cannot be split into rows of {n_features} features"
),
Error::UnsupportedFormat { ext, supported } => write!(
f,
"unsupported model format '.{ext}' in this build (supported: {supported:?})"
),
Error::Backend { backend, message } => write!(f, "{backend} backend: {message}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io { source, .. } => Some(source),
_ => None,
}
}
}