#![allow(clippy::default_trait_access)]
use core::fmt;
use std::fmt::{Display, Formatter};
use crate::xdoc::error::XDocErr;
use crate::ParseError;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Parse(ParseError),
XdocErr(XDocErr),
Other(OtherError),
}
impl Display for crate::error::Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Error::Parse(pe) => pe.fmt(f),
Error::XdocErr(xe) => xe.fmt(f),
Error::Other(oe) => oe.fmt(f),
}
}
}
impl std::error::Error for crate::error::Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Parse(e) => convert_err(&e.source),
Error::XdocErr(e) => convert_err(&e.source),
Error::Other(e) => convert_err(&e.source),
}
}
}
impl From<XDocErr> for Error {
fn from(xe: XDocErr) -> Self {
Error::XdocErr(xe)
}
}
fn convert_err<'a>(
e: &'a Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
) -> Option<&'a (dyn std::error::Error + 'static)> {
e.as_ref().map(|e| e.as_ref() as _)
}
#[derive(Debug, Clone, Eq, PartialOrd, PartialEq, Hash, Default)]
pub struct ThrowSite {
pub file: String,
pub line: u32,
}
impl Display for ThrowSite {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.file, self.line)
}
}
#[derive(Debug, Default)]
pub struct OtherError {
pub throw_site: ThrowSite,
pub message: Option<String>,
pub source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
}
impl Display for OtherError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.throw_site.fmt(f)?;
if let Some(msg) = &self.message {
if !msg.is_empty() {
write!(f, " - {}", msg)?;
}
}
if let Some(e) = &self.source {
write!(f, " - caused by: ")?;
e.fmt(f)?;
}
Ok(())
}
}
impl From<ParseError> for Error {
fn from(pe: ParseError) -> Self {
Self::Parse(pe)
}
}