#[derive(Debug)]
pub enum Error {
Http(ureq::Error),
Json(serde_json::Error),
Io(std::io::Error),
Provider { status: u16, message: String },
Config { field: String },
DimensionMismatch { expected: usize, got: usize },
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Http(e) => write!(f, "HTTP error: {e}"),
Self::Json(e) => write!(f, "JSON error: {e}"),
Self::Io(e) => write!(f, "IO error: {e}"),
Self::Provider { status, message } => {
write!(f, "provider error ({status}): {message}")
}
Self::Config { field } => write!(f, "missing config: {field}"),
Self::DimensionMismatch { expected, got } => {
write!(f, "dimension mismatch: expected {expected}, got {got}")
}
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Http(e) => Some(e),
Self::Json(e) => Some(e),
Self::Io(e) => Some(e),
Self::Provider { .. } | Self::Config { .. } | Self::DimensionMismatch { .. } => None,
}
}
}
impl From<ureq::Error> for Error {
fn from(e: ureq::Error) -> Self { Self::Http(e) }
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self { Self::Json(e) }
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self { Self::Io(e) }
}