1use std::{
16 fmt::{self, Display},
17 io, num, str, string,
18 sync::Arc,
19};
20
21use serde::{de, ser};
22
23#[derive(Clone, Debug)]
24pub enum Error {
25 DryPop,
26 EmptyStack,
27 FromUtf8(string::FromUtf8Error),
28 Io(Arc<io::Error>),
29 Json(Arc<serde_json::Error>),
30 Message(String),
31 NoCurrentFieldMeta,
32 NoMessageMeta,
33 NoSuchField(&'static str),
34 NoSuchMessage(&'static str),
35 ParseBool(str::ParseBoolError),
36 ParseInt(num::ParseIntError),
37 Syn(syn::Error),
38 TryFromInt(num::TryFromIntError),
39 Utf8(str::Utf8Error),
40}
41
42impl std::error::Error for Error {}
43
44impl ser::Error for Error {
45 fn custom<T: Display>(msg: T) -> Self {
46 Error::Message(msg.to_string())
47 }
48}
49
50impl de::Error for Error {
51 fn custom<T: Display>(msg: T) -> Self {
52 Error::Message(msg.to_string())
53 }
54}
55
56impl From<io::Error> for Error {
57 fn from(value: io::Error) -> Self {
58 Self::Io(Arc::new(value))
59 }
60}
61
62impl From<str::Utf8Error> for Error {
63 fn from(value: str::Utf8Error) -> Self {
64 Self::Utf8(value)
65 }
66}
67
68impl From<string::FromUtf8Error> for Error {
69 fn from(value: string::FromUtf8Error) -> Self {
70 Self::FromUtf8(value)
71 }
72}
73
74impl From<num::TryFromIntError> for Error {
75 fn from(value: num::TryFromIntError) -> Self {
76 Self::TryFromInt(value)
77 }
78}
79
80impl From<str::ParseBoolError> for Error {
81 fn from(value: str::ParseBoolError) -> Self {
82 Self::ParseBool(value)
83 }
84}
85
86impl From<num::ParseIntError> for Error {
87 fn from(value: num::ParseIntError) -> Self {
88 Self::ParseInt(value)
89 }
90}
91
92impl From<serde_json::Error> for Error {
93 fn from(value: serde_json::Error) -> Self {
94 Self::Json(Arc::new(value))
95 }
96}
97
98impl From<syn::Error> for Error {
99 fn from(value: syn::Error) -> Self {
100 Self::Syn(value)
101 }
102}
103
104impl Display for Error {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 match self {
107 Error::Message(e) => f.write_str(e),
108 e => write!(f, "{e:?}"),
109 }
110 }
111}