use crate::text_size::TextSize; use std::fmt::Display; #[derive(Debug, PartialEq, Eq)] pub struct BaseError { pub error: T, pub offset: TextSize, pub source_path: String, } impl std::ops::Deref for BaseError { type Target = T; fn deref(&self) -> &Self::Target { &self.error } } impl std::error::Error for BaseError where T: std::error::Error + 'static, { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.error) } } impl Display for BaseError where T: std::fmt::Display, { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!( f, "{} at byte offset {}", &self.error, u32::from(self.offset) ) } } impl BaseError { pub fn error(self) -> T { self.error } pub fn from(obj: BaseError) -> Self where U: Into, { Self { error: obj.error.into(), offset: obj.offset, source_path: obj.source_path, } } pub fn into(self) -> BaseError where T: Into, { BaseError::from(self) } }