use std::fmt;
#[derive(Debug)]
#[non_exhaustive]
pub enum ChartError {
EmptyData,
LengthMismatch {
expected: usize,
got: usize,
},
InvalidData {
layer: usize,
detail: String,
},
DimensionMismatch {
layer: usize,
x_len: usize,
y_len: usize,
},
InvalidParameter(String),
Gfx(esoc_gfx::error::GfxError),
Io(std::io::Error),
}
impl fmt::Display for ChartError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptyData => f.write_str("no data provided"),
Self::LengthMismatch { expected, got } => {
write!(f, "length mismatch: expected {expected}, got {got}")
}
Self::InvalidData { layer, detail } => {
write!(f, "layer {layer}: {detail}")
}
Self::DimensionMismatch {
layer,
x_len,
y_len,
} => {
write!(f, "layer {layer}: x has {x_len} elements but y has {y_len}")
}
Self::InvalidParameter(msg) => write!(f, "invalid parameter: {msg}"),
Self::Gfx(err) => write!(f, "graphics error: {err}"),
Self::Io(err) => write!(f, "I/O error: {err}"),
}
}
}
impl std::error::Error for ChartError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Gfx(err) => Some(err),
Self::Io(err) => Some(err),
_ => None,
}
}
}
impl From<esoc_gfx::error::GfxError> for ChartError {
fn from(err: esoc_gfx::error::GfxError) -> Self {
Self::Gfx(err)
}
}
impl From<std::io::Error> for ChartError {
fn from(err: std::io::Error) -> Self {
Self::Io(err)
}
}
pub type Result<T> = std::result::Result<T, ChartError>;