use core::fmt;
pub type DecoderResult<T, E = DecoderError> = std::result::Result<T, E>;
#[derive(Debug)]
pub enum DecoderError {
Internal(String),
NotSupported(String),
InvalidShape(String),
Yaml(serde_yaml::Error),
Json(serde_json::Error),
NoConfig,
InvalidConfig(String),
NDArrayShape(ndarray::ShapeError),
}
impl fmt::Display for DecoderError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self:?}")
}
}
impl std::error::Error for DecoderError {}
impl From<serde_yaml::Error> for DecoderError {
fn from(err: serde_yaml::Error) -> Self {
DecoderError::Yaml(err)
}
}
impl From<serde_json::Error> for DecoderError {
fn from(err: serde_json::Error) -> Self {
DecoderError::Json(err)
}
}
impl From<ndarray::ShapeError> for DecoderError {
fn from(err: ndarray::ShapeError) -> Self {
DecoderError::NDArrayShape(err)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_decoder_error_display() {
let e = DecoderError::Internal("something broke".to_string());
let msg = e.to_string();
assert!(!msg.is_empty());
assert!(
msg.contains("Internal") && msg.contains("something broke"),
"unexpected Internal message: {msg}"
);
let e = DecoderError::NotSupported("yolov99".to_string());
let msg = e.to_string();
assert!(!msg.is_empty());
assert!(
msg.contains("NotSupported") && msg.contains("yolov99"),
"unexpected NotSupported message: {msg}"
);
let e = DecoderError::InvalidShape("expected 3D".to_string());
let msg = e.to_string();
assert!(!msg.is_empty());
assert!(
msg.contains("InvalidShape") && msg.contains("expected 3D"),
"unexpected InvalidShape message: {msg}"
);
let e = DecoderError::NoConfig;
let msg = e.to_string();
assert!(!msg.is_empty());
assert!(
msg.contains("NoConfig"),
"unexpected NoConfig message: {msg}"
);
let e = DecoderError::InvalidConfig("missing field".to_string());
let msg = e.to_string();
assert!(!msg.is_empty());
assert!(
msg.contains("InvalidConfig") && msg.contains("missing field"),
"unexpected InvalidConfig message: {msg}"
);
}
}