1use std::fmt::{self, Display};
2
3use serde_json::{json, Value};
4use crate::ocpp_1_6::raw_ocpp_1_6_error::RawOcpp1_6Error;
5
6#[derive(Debug, Clone)]
8pub enum OCPP1_6Error {
9 NotImplemented { description: String, details: Value },
10 NotSupported { description: String, details: Value },
11 InternalError { description: String, details: Value },
12 ProtocolError { description: String, details: Value },
13 SecurityError { description: String, details: Value },
14 FormationViolation { description: String, details: Value },
15 PropertyConstraintViolation { description: String, details: Value },
16 OccurenceConstraintViolation { description: String, details: Value },
17 TypeConstraintViolation { description: String, details: Value },
18 GenericError { description: String, details: Value },
19}
20
21impl OCPP1_6Error {
22 pub fn new_not_implemented(description: &str) -> Self {
23 OCPP1_6Error::NotImplemented {
24 description: description.to_string(),
25 details: json!({}),
26 }
27 }
28
29 pub fn new_security_error(description: &str) -> Self {
30 OCPP1_6Error::SecurityError {
31 description: description.to_string(),
32 details: json!({}),
33 }
34 }
35
36 pub fn new_formation_violation(description: &str) -> Self {
37 OCPP1_6Error::FormationViolation {
38 description: description.to_string(),
39 details: json!({}),
40 }
41 }
42
43 pub fn new_internal<T: Display>(error: &T) -> Self {
44 OCPP1_6Error::InternalError {
45 description: error.to_string(),
46 details: json!({}),
47 }
48 }
49
50 pub fn new_internal_str(description: &str) -> Self {
51 OCPP1_6Error::InternalError {
52 description: description.to_string(),
53 details: json!({}),
54 }
55 }
56
57 pub fn code(&self) -> &str {
58 match self {
59 OCPP1_6Error::NotImplemented {
60 description: _,
61 details: _,
62 } => "NotImplemented",
63 OCPP1_6Error::NotSupported {
64 description: _,
65 details: _,
66 } => "NotSupported",
67 OCPP1_6Error::InternalError {
68 description: _,
69 details: _,
70 } => "InternalError",
71 OCPP1_6Error::ProtocolError {
72 description: _,
73 details: _,
74 } => "ProtocolError",
75 OCPP1_6Error::SecurityError {
76 description: _,
77 details: _,
78 } => "SecurityError",
79 OCPP1_6Error::FormationViolation {
80 description: _,
81 details: _,
82 } => "FormationViolation",
83 OCPP1_6Error::PropertyConstraintViolation {
84 description: _,
85 details: _,
86 } => "PropertyConstraintViolation",
87 OCPP1_6Error::OccurenceConstraintViolation {
88 description: _,
89 details: _,
90 } => "OccurenceConstraintViolation",
91 OCPP1_6Error::TypeConstraintViolation {
92 description: _,
93 details: _,
94 } => "TypeConstraintViolation",
95 OCPP1_6Error::GenericError {
96 description: _,
97 details: _,
98 } => "GenericError",
99 }
100 }
101
102 pub fn description(&self) -> &str {
103 match self {
104 OCPP1_6Error::NotImplemented {
105 description,
106 details: _,
107 } => description,
108 OCPP1_6Error::NotSupported {
109 description,
110 details: _,
111 } => description,
112 OCPP1_6Error::InternalError {
113 description,
114 details: _,
115 } => description,
116 OCPP1_6Error::ProtocolError {
117 description,
118 details: _,
119 } => description,
120 OCPP1_6Error::SecurityError {
121 description,
122 details: _,
123 } => description,
124 OCPP1_6Error::FormationViolation {
125 description,
126 details: _,
127 } => description,
128 OCPP1_6Error::PropertyConstraintViolation {
129 description,
130 details: _,
131 } => description,
132 OCPP1_6Error::OccurenceConstraintViolation {
133 description,
134 details: _,
135 } => description,
136 OCPP1_6Error::TypeConstraintViolation {
137 description,
138 details: _,
139 } => description,
140 OCPP1_6Error::GenericError {
141 description,
142 details: _,
143 } => description,
144 }
145 }
146
147 pub fn details(&self) -> &Value {
148 match self {
149 OCPP1_6Error::NotImplemented {
150 description: _,
151 details,
152 } => details,
153 OCPP1_6Error::NotSupported {
154 description: _,
155 details,
156 } => details,
157 OCPP1_6Error::InternalError {
158 description: _,
159 details,
160 } => details,
161 OCPP1_6Error::ProtocolError {
162 description: _,
163 details,
164 } => details,
165 OCPP1_6Error::SecurityError {
166 description: _,
167 details,
168 } => details,
169 OCPP1_6Error::FormationViolation {
170 description: _,
171 details,
172 } => details,
173 OCPP1_6Error::PropertyConstraintViolation {
174 description: _,
175 details,
176 } => details,
177 OCPP1_6Error::OccurenceConstraintViolation {
178 description: _,
179 details,
180 } => details,
181 OCPP1_6Error::TypeConstraintViolation {
182 description: _,
183 details,
184 } => details,
185 OCPP1_6Error::GenericError {
186 description: _,
187 details,
188 } => details,
189 }
190 }
191}
192
193impl fmt::Display for OCPP1_6Error {
194 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
195 match self {
196 OCPP1_6Error::NotImplemented {
197 description,
198 details,
199 } => write!(
200 f,
201 "NotImplemented: {} - {}",
202 description,
203 serde_json::to_string_pretty(details).unwrap()
204 ),
205 OCPP1_6Error::NotSupported {
206 description,
207 details,
208 } => write!(
209 f,
210 "NotSupported: {} - {}",
211 description,
212 serde_json::to_string_pretty(details).unwrap()
213 ),
214 OCPP1_6Error::InternalError {
215 description,
216 details,
217 } => write!(
218 f,
219 "InternalError: {} - {}",
220 description,
221 serde_json::to_string_pretty(details).unwrap()
222 ),
223 OCPP1_6Error::ProtocolError {
224 description,
225 details,
226 } => write!(
227 f,
228 "ProtocolError: {} - {}",
229 description,
230 serde_json::to_string_pretty(details).unwrap()
231 ),
232 OCPP1_6Error::SecurityError {
233 description,
234 details,
235 } => write!(
236 f,
237 "SecurityError: {} - {}",
238 description,
239 serde_json::to_string_pretty(details).unwrap()
240 ),
241 OCPP1_6Error::FormationViolation {
242 description,
243 details,
244 } => write!(
245 f,
246 "FormationViolation: {} - {}",
247 description,
248 serde_json::to_string_pretty(details).unwrap()
249 ),
250 OCPP1_6Error::PropertyConstraintViolation {
251 description,
252 details,
253 } => write!(
254 f,
255 "PropertyConstraintViolation: {} - {}",
256 description,
257 serde_json::to_string_pretty(details).unwrap()
258 ),
259 OCPP1_6Error::OccurenceConstraintViolation {
260 description,
261 details,
262 } => write!(
263 f,
264 "OccurenceConstraintViolation: {} - {}",
265 description,
266 serde_json::to_string_pretty(details).unwrap()
267 ),
268 OCPP1_6Error::TypeConstraintViolation {
269 description,
270 details,
271 } => write!(
272 f,
273 "TypeConstraintViolation: {} - {}",
274 description,
275 serde_json::to_string_pretty(details).unwrap()
276 ),
277 OCPP1_6Error::GenericError {
278 description,
279 details,
280 } => write!(
281 f,
282 "GenericError: {} - {}",
283 description,
284 serde_json::to_string_pretty(details).unwrap()
285 ),
286 }
287 }
288}
289
290impl std::error::Error for OCPP1_6Error {}
291
292impl From<RawOcpp1_6Error> for OCPP1_6Error {
293 fn from(value: RawOcpp1_6Error) -> Self {
294 match value.2.as_str() {
295 "NotImplemented" => {
296 Self::NotImplemented {
297 description: value.3,
298 details: value.4
299 }
300 },
301 "NotSupported" => {
302 Self::NotSupported {
303 description: value.3,
304 details: value.4
305 }
306 },
307 "InternalError" => {
308 Self::InternalError {
309 description: value.3,
310 details: value.4
311 }
312 },
313 "ProtocolError" => {
314 Self::ProtocolError {
315 description: value.3,
316 details: value.4
317 }
318 },
319 "SecurityError" => {
320 Self::SecurityError {
321 description: value.3,
322 details: value.4
323 }
324 },
325 "FormationViolation" => {
326 Self::FormationViolation {
327 description: value.3,
328 details: value.4
329 }
330 },
331 "PropertyConstraintViolation" => {
332 Self::PropertyConstraintViolation {
333 description: value.3,
334 details: value.4
335 }
336 },
337 "OccurenceConstraintViolation" => {
338 Self::OccurenceConstraintViolation {
339 description: value.3,
340 details: value.4
341 }
342 },
343 "TypeConstraintViolation" => {
344 Self::TypeConstraintViolation {
345 description: value.3,
346 details: value.4
347 }
348 },
349 "GenericError" => {
350 Self::GenericError {
351 description: value.3,
352 details: value.4
353 }
354 },
355 _ => {
356 Self::GenericError {
357 description: value.3,
358 details: value.4
359 }
360 }
361 }
362 }
363}