use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DslError {
pub message: String,
pub offset: Option<usize>,
}
impl DslError {
pub fn at(offset: usize, message: impl Into<String>) -> Self {
Self {
message: message.into(),
offset: Some(offset),
}
}
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
offset: None,
}
}
}
impl fmt::Display for DslError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.offset {
Some(offset) => write!(f, "dsl parse error at byte {offset}: {}", self.message),
None => write!(f, "dsl error: {}", self.message),
}
}
}
impl std::error::Error for DslError {}
impl serde::ser::Error for DslError {
fn custom<T: fmt::Display>(msg: T) -> Self {
Self::new(msg.to_string())
}
}
impl serde::de::Error for DslError {
fn custom<T: fmt::Display>(msg: T) -> Self {
Self::new(msg.to_string())
}
}