alloy_sol_type_parser/
error.rs
1use alloc::{boxed::Box, string::String};
2use core::fmt;
3
4pub type Result<T, E = Error> = core::result::Result<T, E>;
6
7#[derive(Clone, PartialEq, Eq)]
9pub struct Error(Repr);
10
11impl core::error::Error for Error {}
12
13impl fmt::Debug for Error {
14 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15 f.debug_tuple("Error").field(&self.0 .0).finish()
16 }
17}
18
19impl fmt::Display for Error {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 self.0.fmt(f)
22 }
23}
24
25impl Error {
26 pub fn new(s: impl fmt::Display) -> Self {
28 Self::_new("", &s)
29 }
30
31 pub fn parser(e: impl fmt::Display) -> Self {
33 Self::_new(if cfg!(feature = "std") { "parser error:\n" } else { "parser error: " }, &e)
34 }
35
36 pub fn invalid_type_string(ty: impl fmt::Display) -> Self {
39 Self::_new("invalid type string: ", &ty)
40 }
41
42 pub fn invalid_identifier_string(identifier: impl fmt::Display) -> Self {
46 Self::_new("invalid identifier string: ", &identifier)
47 }
48
49 pub fn invalid_size(ty: impl fmt::Display) -> Self {
53 Self::_new("invalid size for type: ", &ty)
54 }
55
56 #[doc(hidden)]
58 #[inline(never)]
59 #[cold]
60 pub fn _new(s: &str, e: &dyn fmt::Display) -> Self {
61 Self(Repr(Box::new(format!("{s}{e}"))))
62 }
63}
64
65#[derive(Clone, PartialEq, Eq)]
66#[allow(clippy::box_collection)] struct Repr(Box<String>);
68
69impl fmt::Display for Repr {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 f.write_str(&self.0)
72 }
73}