use alloc::string::String;
use core::fmt::{Display, Error, Formatter};
pub type PResult<T> = Result<T, PError>;
#[derive(Debug)]
pub enum PError {
Mismatch,
Terminate {
name: &'static str,
msg: String,
},
}
impl PError {
pub fn or<R, F>(self, f: F) -> Result<R, Self>
where
F: FnOnce() -> Result<R, Self>,
{
match self {
Self::Mismatch => f(),
Self::Terminate { .. } => Err(self),
}
}
}
impl Display for PError {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match self {
Self::Mismatch => write!(f, "not matched"),
Self::Terminate { name, msg } => {
write!(f, "invalid {}: \n\n{}", name, msg)
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for PError {}