use std::fmt;
#[derive(Debug)]
#[non_exhaustive]
pub enum MattenError {
Shape {
operation: &'static str,
message: String,
},
Broadcast {
left: Vec<usize>,
right: Vec<usize>,
},
Allocation {
requested_elements: usize,
message: String,
},
Slice {
input: Option<String>,
message: String,
},
Parse {
format: DataFormat,
message: String,
},
Io {
path: std::path::PathBuf,
source: std::io::Error,
},
Unsupported {
operation: &'static str,
message: String,
},
InvalidArgument {
operation: &'static str,
argument: &'static str,
message: String,
},
}
impl fmt::Display for MattenError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MattenError::Shape { operation, message } => {
write!(f, "matten shape error in {operation}: {message}")
}
MattenError::Broadcast { left, right } => write!(
f,
"matten broadcast error: shapes {left:?} and {right:?} are not compatible"
),
MattenError::Allocation {
requested_elements,
message,
} => write!(
f,
"matten allocation error: {message} (requested {requested_elements} elements)"
),
MattenError::Slice { input, message } => match input {
Some(spec) => write!(f, "matten slice error for {spec:?}: {message}"),
None => write!(f, "matten slice error: {message}"),
},
MattenError::Parse { format, message } => {
write!(f, "matten {format} parse error: {message}")
}
MattenError::Io { path, source } => {
write!(f, "matten io error for {}: {source}", path.display())
}
MattenError::Unsupported { operation, message } => {
write!(f, "matten unsupported error in {operation}: {message}")
}
MattenError::InvalidArgument {
operation,
argument,
message,
} => write!(
f,
"matten invalid argument error in {operation}: {argument}: {message}"
),
}
}
}
impl std::error::Error for MattenError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
MattenError::Io { source, .. } => Some(source),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum DataFormat {
Json,
Csv,
}
impl fmt::Display for DataFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DataFormat::Json => f.write_str("json"),
DataFormat::Csv => f.write_str("csv"),
}
}
}