use std::{borrow::Cow, fmt};
#[derive(Debug)]
pub enum PslError {
Io(std::io::Error),
Format {
offset: usize,
msg: Cow<'static, str>,
},
Unsupported {
msg: Cow<'static, str>,
},
Invariant {
line: usize,
msg: Cow<'static, str>,
},
}
impl From<std::io::Error> for PslError {
fn from(value: std::io::Error) -> Self {
PslError::Io(value)
}
}
impl fmt::Display for PslError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PslError::Io(err) => write!(f, "I/O error: {err}"),
PslError::Format { offset, msg } => {
write!(f, "format error at byte {offset}: {msg}")
}
PslError::Unsupported { msg } => write!(f, "unsupported: {msg}"),
PslError::Invariant { line, msg } => {
write!(f, "invariant violation at line {line}: {msg}")
}
}
}
}
impl std::error::Error for PslError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
PslError::Io(err) => Some(err),
_ => None,
}
}
}