1use serde::{Deserialize, Serialize};
8use serde_json::{Map, Value};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15#[non_exhaustive]
16pub enum MessageType {
17 Pain00100103,
19 Pain00100104,
21 Pain00100105,
23 Pain00100106,
25 Pain00100107,
27 Pain00100108,
29 Pain00100109,
31 Pain00100110,
33 Pain00100111,
35 Pain00100112,
37 Pain00100113,
39 Pain00800102,
41 Pain00800108,
43}
44
45impl MessageType {
46 pub const ALL: [MessageType; 13] = [
48 MessageType::Pain00100103,
49 MessageType::Pain00100104,
50 MessageType::Pain00100105,
51 MessageType::Pain00100106,
52 MessageType::Pain00100107,
53 MessageType::Pain00100108,
54 MessageType::Pain00100109,
55 MessageType::Pain00100110,
56 MessageType::Pain00100111,
57 MessageType::Pain00100112,
58 MessageType::Pain00100113,
59 MessageType::Pain00800102,
60 MessageType::Pain00800108,
61 ];
62
63 pub fn as_str(self) -> &'static str {
65 WIRE_NAMES[self as usize]
66 }
67}
68
69const WIRE_NAMES: [&str; 13] = [
71 "pain.001.001.03",
72 "pain.001.001.04",
73 "pain.001.001.05",
74 "pain.001.001.06",
75 "pain.001.001.07",
76 "pain.001.001.08",
77 "pain.001.001.09",
78 "pain.001.001.10",
79 "pain.001.001.11",
80 "pain.001.001.12",
81 "pain.001.001.13",
82 "pain.008.001.02",
83 "pain.008.001.08",
84];
85
86impl AsRef<str> for MessageType {
87 fn as_ref(&self) -> &str {
88 self.as_str()
89 }
90}
91
92impl std::fmt::Display for MessageType {
93 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94 f.write_str(self.as_str())
95 }
96}
97
98#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
100#[non_exhaustive]
101pub struct HealthResponse {
102 pub status: String,
104 pub version: String,
106 pub message: String,
108}
109
110#[derive(Debug, Clone, PartialEq, Deserialize)]
116#[non_exhaustive]
117pub struct RowError {
118 pub field: String,
120 pub message: String,
122 #[serde(default)]
124 pub value: Option<Value>,
125}
126
127#[derive(Debug, Clone, PartialEq, Deserialize)]
130#[non_exhaustive]
131pub struct RequestError {
132 pub loc: Vec<Value>,
134 pub msg: String,
136 #[serde(rename = "type")]
138 pub kind: String,
139 #[serde(default)]
141 pub input: Option<Value>,
142 #[serde(default)]
144 pub ctx: Option<Value>,
145}
146
147#[derive(Debug, Clone, PartialEq, Deserialize)]
149#[non_exhaustive]
150pub struct ValidationResponse {
151 pub is_valid: bool,
153 pub total_rows: u64,
155 #[serde(default)]
157 pub valid_rows: u64,
158 #[serde(default)]
160 pub invalid_rows: u64,
161 #[serde(default)]
163 pub errors: Vec<RowError>,
164 #[serde(default)]
166 pub scheme_violations: Vec<Map<String, Value>>,
167}
168
169#[derive(Debug, Clone, PartialEq, Deserialize)]
171#[non_exhaustive]
172pub struct GenerateResponse {
173 pub success: bool,
175 pub message: String,
177 #[serde(default)]
179 pub filename: Option<String>,
180 #[serde(default)]
182 pub xml: Option<String>,
183 #[serde(default)]
185 pub validation_errors: Vec<RowError>,
186 #[serde(default)]
188 pub scheme_violations: Vec<Map<String, Value>>,
189}
190
191#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
193#[non_exhaustive]
194pub struct CheckFinding {
195 pub rung: String,
197 pub rule: String,
199 pub severity: String,
201 pub message: String,
203 #[serde(default)]
205 pub location: Option<String>,
206}
207
208#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
210#[non_exhaustive]
211pub struct CheckResponse {
212 pub message_type: String,
214 pub passed: bool,
216 pub errors: u64,
218 pub warnings: u64,
220 pub rungs: Vec<String>,
222 pub findings: Vec<CheckFinding>,
224}
225
226#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
228#[non_exhaustive]
229pub struct FaceInfo {
230 pub name: String,
232 pub description: String,
234 pub bindings: Vec<String>,
236}
237
238#[derive(Debug, Clone, PartialEq, Deserialize)]
240#[non_exhaustive]
241pub struct LossReport {
242 pub face: String,
244 pub total_source_elements: u64,
246 pub mapped_count: u64,
248 pub unmapped_count: u64,
250 pub is_lossless: bool,
252 pub loss_ratio: f64,
254 pub mapped_paths: Vec<String>,
256 pub unmapped_paths: Vec<String>,
258}
259
260#[derive(Debug, Clone, PartialEq, Deserialize)]
262#[non_exhaustive]
263pub struct FaceResponse {
264 pub face: String,
266 pub version: String,
268 pub payload: Value,
270 pub loss_report: LossReport,
272}
273
274#[derive(Serialize)]
276pub(crate) struct FilePayload<'a> {
277 pub content: &'a str,
278 pub filename: &'a str,
279 pub message_type: &'a str,
280 #[serde(skip_serializing_if = "Option::is_none")]
281 pub scheme: Option<&'a str>,
282}
283
284#[derive(Serialize)]
286pub(crate) struct CheckRequest<'a> {
287 pub xml: &'a str,
288 #[serde(skip_serializing_if = "Option::is_none")]
289 pub scheme: Option<&'a str>,
290 #[serde(skip_serializing_if = "Option::is_none")]
291 pub schematron: Option<&'a str>,
292}
293
294#[derive(Serialize)]
296pub(crate) struct FaceRequest<'a> {
297 pub xml: &'a str,
298 #[serde(skip_serializing_if = "Option::is_none")]
299 pub version: Option<&'a str>,
300 #[serde(skip_serializing_if = "Option::is_none")]
301 pub bindings: Option<&'a Map<String, Value>>,
302}
303
304#[cfg(test)]
305mod tests {
306 use super::*;
307
308 #[test]
309 fn message_types_match_the_openapi_enum() {
310 let spec: Value = serde_json::from_str(include_str!("../conformance/openapi.json"))
311 .expect("openapi.json parses");
312 let wanted: Vec<&str> = spec["components"]["schemas"]["MessageType"]["enum"]
313 .as_array()
314 .expect("MessageType enum")
315 .iter()
316 .filter_map(Value::as_str)
317 .collect();
318 let ours: Vec<&str> = MessageType::ALL.iter().map(|m| m.as_str()).collect();
319 assert_eq!(ours, wanted);
320 assert_eq!(MessageType::Pain00800108.to_string(), "pain.008.001.08");
321 assert_eq!(MessageType::Pain00100109.as_ref(), "pain.001.001.09");
322 }
323
324 #[test]
325 fn optional_request_fields_are_left_out() {
326 let body = FilePayload {
327 content: "a",
328 filename: "p.csv",
329 message_type: "pain.001.001.09",
330 scheme: None,
331 };
332 let json = serde_json::to_value(&body).expect("serialises");
333 assert_eq!(json.as_object().map(Map::len), Some(3));
334 let check = CheckRequest {
335 xml: "<x/>",
336 scheme: Some("sepa-sct"),
337 schematron: None,
338 };
339 let json = serde_json::to_value(&check).expect("serialises");
340 assert_eq!(
341 json,
342 serde_json::json!({"xml": "<x/>", "scheme": "sepa-sct"})
343 );
344 }
345
346 #[test]
347 fn validation_response_defaults_the_optional_fields() {
348 let parsed: ValidationResponse =
349 serde_json::from_str(r#"{"is_valid": false, "total_rows": 2, "extra": 1}"#)
350 .expect("decodes");
351 assert_eq!(parsed.valid_rows, 0);
352 assert!(parsed.errors.is_empty() && parsed.scheme_violations.is_empty());
353 }
354
355 #[test]
356 fn row_errors_decode_the_server_shape() {
357 let parsed: GenerateResponse = serde_json::from_str(
358 r#"{"success": false, "message": "invalid",
359 "validation_errors": [{"field": "currency", "message": "bad", "value": "EUX"},
360 {"field": "id", "message": "missing", "value": null}]}"#,
361 )
362 .expect("decodes");
363 assert_eq!(parsed.validation_errors[0].field, "currency");
364 assert_eq!(parsed.validation_errors[0].value, Some(Value::from("EUX")));
365 assert_eq!(parsed.validation_errors[1].value, None);
366 }
367}