use std::error::Error as StdError;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::result::Result as StdResult;
pub type Error = Box<dyn StdError>;
pub type Result<T> = StdResult<T, Box<dyn StdError>>;
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Fail(pub String);
impl Fail {
pub fn new<E>(err: E) -> Box<Self>
where
E: Display,
{
Box::new(Fail(err.to_string()))
}
pub fn from<T, E>(err: E) -> Result<T>
where
E: Display,
{
Err(Self::new(err))
}
pub fn std<T, E>(err: E) -> StdResult<T, Self>
where
E: Display,
{
Err(Fail(err.to_string()))
}
pub fn err_msg(&self) -> &str {
&self.0
}
}
impl StdError for Fail {}
impl Display for Fail {
fn fmt(&self, formatter: &mut Formatter) -> FmtResult {
write!(formatter, "{}", self.0)
}
}