#[cfg(debug_assertions)]
use alloc::borrow::Cow;
use core::fmt::{Debug, Display, Formatter};
pub fn err<T>(msg: &'static str) -> Result<T, Error> {
Err(error(msg))
}
pub fn error(_msg: &'static str) -> Error {
#[cfg(debug_assertions)]
return Error(Cow::Borrowed(_msg));
#[cfg(not(debug_assertions))]
Error(())
}
#[cfg(feature = "serde")]
pub fn error_from_display(_t: impl Display) -> Error {
#[cfg(debug_assertions)]
use alloc::string::ToString;
#[cfg(debug_assertions)]
return Error(Cow::Owned(_t.to_string()));
#[cfg(not(debug_assertions))]
Error(())
}
#[cfg(debug_assertions)]
type ErrorImpl = Cow<'static, str>;
#[cfg(not(debug_assertions))]
type ErrorImpl = ();
#[cfg_attr(test, derive(PartialEq))]
pub struct Error(ErrorImpl);
impl Debug for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
#[cfg(debug_assertions)]
return write!(f, "Error({:?})", self.0);
#[cfg(not(debug_assertions))]
f.write_str("Error(\"bitcode error\")")
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
#[cfg(debug_assertions)]
return f.write_str(&self.0);
#[cfg(not(debug_assertions))]
f.write_str("bitcode error")
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}