1use std::fmt;
5
6use einvoice_xdm::{ErrorCode as XdmCode, XdmError};
7
8use crate::limit::LimitExceeded;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12#[non_exhaustive]
13pub enum ErrorCode {
14 Xpst0008,
16 Xpst0017,
18 Xpdy0002,
20 Xpdy0050,
22 Xpty0004,
24 Xpty0018,
26 Xpty0019,
28 Xpty0020,
30 Foar0001,
32 Foar0002,
34 Foca0002,
36 Foch0001,
38 Fodc0001,
40 Fodt0001,
42 Forg0001,
44 Forg0006,
46 Forx0001,
48 Forx0002,
50 Forx0003,
52 Forx0004,
54 Xtde1360,
56 Xtde1390,
58 Host,
61 Unsupported,
64 Internal,
66 Limit,
69}
70
71impl ErrorCode {
72 #[must_use]
74 pub const fn as_str(self) -> &'static str {
75 match self {
76 Self::Xpst0008 => "XPST0008",
77 Self::Xpst0017 => "XPST0017",
78 Self::Xpdy0002 => "XPDY0002",
79 Self::Xpdy0050 => "XPDY0050",
80 Self::Xpty0004 => "XPTY0004",
81 Self::Xpty0018 => "XPTY0018",
82 Self::Xpty0019 => "XPTY0019",
83 Self::Xpty0020 => "XPTY0020",
84 Self::Foar0001 => "FOAR0001",
85 Self::Foar0002 => "FOAR0002",
86 Self::Foca0002 => "FOCA0002",
87 Self::Foch0001 => "FOCH0001",
88 Self::Fodc0001 => "FODC0001",
89 Self::Fodt0001 => "FODT0001",
90 Self::Forg0001 => "FORG0001",
91 Self::Forg0006 => "FORG0006",
92 Self::Forx0001 => "FORX0001",
93 Self::Forx0002 => "FORX0002",
94 Self::Forx0003 => "FORX0003",
95 Self::Forx0004 => "FORX0004",
96 Self::Xtde1360 => "XTDE1360",
97 Self::Xtde1390 => "XTDE1390",
98 Self::Host => "HOST",
99 Self::Unsupported => "UNSUPPORTED",
100 Self::Internal => "INTERNAL",
101 Self::Limit => "LIMIT",
102 }
103 }
104
105 #[must_use]
107 pub const fn is_static(self) -> bool {
108 matches!(self, Self::Xpst0008 | Self::Xpst0017)
109 }
110}
111
112impl fmt::Display for ErrorCode {
113 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114 f.write_str(self.as_str())
115 }
116}
117
118#[derive(Debug, Clone, PartialEq, Eq)]
121pub struct Error(Box<Inner>);
122
123#[derive(Debug, Clone, PartialEq, Eq)]
124struct Inner {
125 code: ErrorCode,
126 message: String,
127 limit: Option<LimitExceeded>,
128}
129
130impl Error {
131 #[must_use]
133 pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
134 Self(Box::new(Inner {
135 code,
136 message: message.into(),
137 limit: None,
138 }))
139 }
140
141 #[must_use]
143 pub fn limit_exceeded(limit: LimitExceeded) -> Self {
144 Self(Box::new(Inner {
145 code: ErrorCode::Limit,
146 message: limit.to_string(),
147 limit: Some(limit),
148 }))
149 }
150
151 #[must_use]
153 pub fn limit(&self) -> Option<LimitExceeded> {
154 self.0.limit
155 }
156
157 #[must_use]
159 pub fn code(&self) -> ErrorCode {
160 self.0.code
161 }
162
163 #[must_use]
165 pub fn message(&self) -> &str {
166 &self.0.message
167 }
168}
169
170impl fmt::Display for Error {
171 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172 write!(f, "{}: {}", self.0.code, self.0.message)
173 }
174}
175
176impl std::error::Error for Error {}
177
178impl From<XdmError> for Error {
179 fn from(e: XdmError) -> Self {
180 let code = match e.code() {
181 XdmCode::Foar0001 => ErrorCode::Foar0001,
182 XdmCode::Foar0002 => ErrorCode::Foar0002,
183 XdmCode::Foca0002 => ErrorCode::Foca0002,
184 XdmCode::Fodt0001 => ErrorCode::Fodt0001,
185 XdmCode::Forg0001 => ErrorCode::Forg0001,
186 XdmCode::Forg0006 => ErrorCode::Forg0006,
187 XdmCode::Forx0001 => ErrorCode::Forx0001,
188 XdmCode::Forx0002 => ErrorCode::Forx0002,
189 XdmCode::Forx0003 => ErrorCode::Forx0003,
190 XdmCode::Forx0004 => ErrorCode::Forx0004,
191 XdmCode::Xpty0004 => ErrorCode::Xpty0004,
192 _ => ErrorCode::Unsupported,
193 };
194 Self::new(code, e.message())
195 }
196}
197
198pub type Result<T> = std::result::Result<T, Error>;
200
201pub(crate) fn err<T>(code: ErrorCode, message: impl Into<String>) -> Result<T> {
202 Err(Error::new(code, message))
203}