use std::fmt;
#[derive(Debug)]
pub enum BmpfError {
BadHeader,
BadMagic,
InvalidRecord { index: usize, message: String },
NoGlyphsFound,
Mismatch { bmpf_chars: usize, glyphs_found: usize },
Image(image::ImageError),
Io(String),
}
impl Clone for BmpfError {
fn clone(&self) -> Self {
match self {
Self::BadHeader => Self::BadHeader,
Self::BadMagic => Self::BadMagic,
Self::InvalidRecord { index, message } => Self::InvalidRecord {
index: *index,
message: message.clone(),
},
Self::NoGlyphsFound => Self::NoGlyphsFound,
Self::Mismatch {
bmpf_chars,
glyphs_found,
} => Self::Mismatch {
bmpf_chars: *bmpf_chars,
glyphs_found: *glyphs_found,
},
Self::Image(_) => Self::Io("image error (not cloneable)".into()),
Self::Io(msg) => Self::Io(msg.clone()),
}
}
}
impl fmt::Display for BmpfError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BadHeader => write!(f, "file too short for header"),
Self::BadMagic => write!(f, "not a .bmpf file: bad magic header"),
Self::InvalidRecord { index, message } => {
write!(f, "invalid record #{index}: {message}")
}
Self::NoGlyphsFound => write!(f, "no glyphs found in image"),
Self::Mismatch {
bmpf_chars,
glyphs_found,
} => {
write!(
f,
"bmpf has {bmpf_chars} chars but found {glyphs_found} glyphs"
)
}
Self::Image(e) => write!(f, "image error: {e}"),
Self::Io(msg) => write!(f, "{msg}"),
}
}
}
impl std::error::Error for BmpfError {}
impl From<image::ImageError> for BmpfError {
fn from(e: image::ImageError) -> Self {
Self::Image(e)
}
}