use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Diag {
pub code: &'static str,
pub message: String,
pub offset: Option<usize>,
}
impl Diag {
pub fn new(code: &'static str, message: impl Into<String>, offset: Option<usize>) -> Self {
Self {
code,
message: message.into(),
offset,
}
}
}
impl fmt::Display for Diag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.code, self.message)?;
if let Some(o) = self.offset {
write!(f, " (offset {o})")?;
}
Ok(())
}
}
impl std::error::Error for Diag {}