use std::fmt::{self, Display};
use thiserror::Error;
use tor_error::{ErrorKind, HasKind};
#[derive(Error, Clone, Debug)]
pub(crate) struct Error {
#[source]
detail: Box<ErrorDetail>,
}
impl From<ErrorDetail> for Error {
fn from(detail: ErrorDetail) -> Error {
Error {
detail: detail.into(),
}
}
}
#[cfg_attr(test, derive(strum::EnumDiscriminants))]
#[cfg_attr(test, strum_discriminants(vis(pub(crate))))]
#[derive(Error, Clone, Debug)]
#[non_exhaustive]
pub(crate) enum ErrorDetail {
#[error("Programming problem")]
Bug(#[from] tor_error::Bug),
#[error("Problem with configuration")]
Configuration(#[from] tor_config::ConfigBuildError),
#[error("KeyMgr error")]
KeyMgr(#[from] tor_keymgr::Error),
}
impl Error {
#[allow(unused)]
pub(crate) fn into_detail(self) -> ErrorDetail {
*self.detail
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "tor: {}: {}", self.detail.kind(), &self.detail)
}
}
impl tor_error::HasKind for Error {
fn kind(&self) -> ErrorKind {
self.detail.kind()
}
}
impl tor_error::HasKind for ErrorDetail {
fn kind(&self) -> ErrorKind {
match self {
ErrorDetail::Bug(e) => e.kind(),
ErrorDetail::Configuration(e) => e.kind(),
ErrorDetail::KeyMgr(e) => e.kind(),
}
}
}
#[cfg(test)]
mod test {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::mixed_attributes_style)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_duration_subtraction)]
#![allow(clippy::useless_vec)]
#![allow(clippy::needless_pass_by_value)]
use super::*;
#[test]
fn traits_ok() {
fn assert<
T: Send + Sync + Clone + std::fmt::Debug + Display + std::error::Error + 'static,
>() {
}
fn check() {
assert::<Error>();
assert::<ErrorDetail>();
}
check(); }
}