use core::fmt::{self, Display};
#[cfg(feature = "std")]
use std::boxed::Box;
#[derive(Debug, Default)]
pub struct Error {
#[cfg(feature = "std")]
cause: Option<Box<dyn std::error::Error>>,
}
impl Error {
pub fn new() -> Self {
Self::default()
}
#[cfg(feature = "std")]
pub fn from_cause<E>(cause: E) -> Self
where
E: Into<Box<dyn std::error::Error>>,
{
Self {
cause: Some(cause.into()),
}
}
#[cfg(feature = "std")]
pub fn cause(&self) -> Option<&dyn std::error::Error> {
self.cause.as_ref().map(|c| c.as_ref())
}
#[cfg(feature = "std")]
pub fn into_cause(self) -> Box<dyn std::error::Error> {
self.cause
.expect("into_cause called on an error with no cause")
}
#[cfg(feature = "std")]
pub fn downcast<T>(self) -> Result<Box<T>, Box<dyn std::error::Error>>
where
T: std::error::Error + 'static,
{
self.cause
.map(|cause| cause.downcast())
.unwrap_or_else(|| Err(Error::new().into()))
}
#[cfg(feature = "std")]
pub fn downcast_ref<T>(&self) -> Option<&T>
where
T: std::error::Error + 'static,
{
self.cause.as_ref().and_then(|cause| cause.downcast_ref())
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "signature error")?;
#[cfg(feature = "std")]
{
if let Some(ref cause) = self.cause {
write!(f, ": {}", cause)?;
}
}
Ok(())
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn std::error::Error> {
self.cause.as_ref().map(|cause| cause.as_ref())
}
}