use alloc::{borrow::Cow, string::String};
use core::fmt::{self, Debug, Display};
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MsgError(Cow<'static, str>);
impl core::error::Error for MsgError {}
impl MsgError {
#[inline]
pub const fn from_static(message: &'static str) -> Self {
Self(Cow::Borrowed(message))
}
#[inline]
pub fn from_owned(message: String) -> Self {
Self(Cow::Owned(message))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Debug for MsgError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl Display for MsgError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl From<&'static str> for MsgError {
fn from(s: &'static str) -> Self {
Self::from_static(s)
}
}
impl From<String> for MsgError {
fn from(s: String) -> Self {
Self::from_owned(s)
}
}
impl From<Cow<'static, str>> for MsgError {
fn from(s: Cow<'static, str>) -> Self {
Self(s)
}
}