use alloc::{boxed::Box, string::String};
use core::fmt;
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(Clone, PartialEq, Eq)]
pub struct Error(Repr);
impl core::error::Error for Error {}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Error").field(&self.0 .0).finish()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl Error {
pub fn new(s: impl fmt::Display) -> Self {
Self::_new("", &s)
}
pub fn parser(e: impl fmt::Display) -> Self {
Self::_new(if cfg!(feature = "std") { "parser error:\n" } else { "parser error: " }, &e)
}
pub fn invalid_type_string(ty: impl fmt::Display) -> Self {
Self::_new("invalid type string: ", &ty)
}
pub fn invalid_identifier_string(identifier: impl fmt::Display) -> Self {
Self::_new("invalid identifier string: ", &identifier)
}
pub fn invalid_size(ty: impl fmt::Display) -> Self {
Self::_new("invalid size for type: ", &ty)
}
#[doc(hidden)]
#[inline(never)]
#[cold]
pub fn _new(s: &str, e: &dyn fmt::Display) -> Self {
Self(Repr(Box::new(format!("{s}{e}"))))
}
}
#[derive(Clone, PartialEq, Eq)]
#[allow(clippy::box_collection)] struct Repr(Box<String>);
impl fmt::Display for Repr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}