use std::{fmt, io};
use thiserror::Error;
#[cfg(feature = "backtrace")]
use crate::proto::{trace, ExtBacktrace};
pub type Result<T> = ::std::result::Result<T, Error>;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ErrorKind {
#[error("io error: {0}")]
Io(#[from] io::Error),
#[error("toml decode error: {0}")]
TomlDecode(#[from] toml::de::Error),
#[error("failed to parse the zone file: {0}")]
ZoneParse(#[from] crate::proto::serialize::txt::ParseError),
}
#[derive(Debug)]
pub struct Error {
kind: Box<ErrorKind>,
#[cfg(feature = "backtrace")]
backtrack: Option<ExtBacktrace>,
}
impl Error {
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
cfg_if::cfg_if! {
if #[cfg(feature = "backtrace")] {
if let Some(ref backtrace) = self.backtrack {
fmt::Display::fmt(&self.kind, f)?;
fmt::Debug::fmt(backtrace, f)
} else {
fmt::Display::fmt(&self.kind, f)
}
} else {
fmt::Display::fmt(&self.kind, f)
}
}
}
}
impl<E> From<E> for Error
where
E: Into<ErrorKind>,
{
fn from(error: E) -> Self {
let kind: ErrorKind = error.into();
Self {
kind: Box::new(kind),
#[cfg(feature = "backtrace")]
backtrack: trace!(),
}
}
}