use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MessageType {
Pain00100103,
Pain00100104,
Pain00100105,
Pain00100106,
Pain00100107,
Pain00100108,
Pain00100109,
Pain00100110,
Pain00100111,
Pain00100112,
Pain00100113,
Pain00800102,
Pain00800108,
}
impl MessageType {
pub const ALL: [MessageType; 13] = [
MessageType::Pain00100103,
MessageType::Pain00100104,
MessageType::Pain00100105,
MessageType::Pain00100106,
MessageType::Pain00100107,
MessageType::Pain00100108,
MessageType::Pain00100109,
MessageType::Pain00100110,
MessageType::Pain00100111,
MessageType::Pain00100112,
MessageType::Pain00100113,
MessageType::Pain00800102,
MessageType::Pain00800108,
];
pub fn as_str(self) -> &'static str {
WIRE_NAMES[self as usize]
}
}
const WIRE_NAMES: [&str; 13] = [
"pain.001.001.03",
"pain.001.001.04",
"pain.001.001.05",
"pain.001.001.06",
"pain.001.001.07",
"pain.001.001.08",
"pain.001.001.09",
"pain.001.001.10",
"pain.001.001.11",
"pain.001.001.12",
"pain.001.001.13",
"pain.008.001.02",
"pain.008.001.08",
];
impl AsRef<str> for MessageType {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for MessageType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[non_exhaustive]
pub struct HealthResponse {
pub status: String,
pub version: String,
pub message: String,
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[non_exhaustive]
pub struct RowError {
pub field: String,
pub message: String,
#[serde(default)]
pub value: Option<Value>,
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[non_exhaustive]
pub struct RequestError {
pub loc: Vec<Value>,
pub msg: String,
#[serde(rename = "type")]
pub kind: String,
#[serde(default)]
pub input: Option<Value>,
#[serde(default)]
pub ctx: Option<Value>,
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[non_exhaustive]
pub struct ValidationResponse {
pub is_valid: bool,
pub total_rows: u64,
#[serde(default)]
pub valid_rows: u64,
#[serde(default)]
pub invalid_rows: u64,
#[serde(default)]
pub errors: Vec<RowError>,
#[serde(default)]
pub scheme_violations: Vec<Map<String, Value>>,
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[non_exhaustive]
pub struct GenerateResponse {
pub success: bool,
pub message: String,
#[serde(default)]
pub filename: Option<String>,
#[serde(default)]
pub xml: Option<String>,
#[serde(default)]
pub validation_errors: Vec<RowError>,
#[serde(default)]
pub scheme_violations: Vec<Map<String, Value>>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[non_exhaustive]
pub struct CheckFinding {
pub rung: String,
pub rule: String,
pub severity: String,
pub message: String,
#[serde(default)]
pub location: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[non_exhaustive]
pub struct CheckResponse {
pub message_type: String,
pub passed: bool,
pub errors: u64,
pub warnings: u64,
pub rungs: Vec<String>,
pub findings: Vec<CheckFinding>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[non_exhaustive]
pub struct FaceInfo {
pub name: String,
pub description: String,
pub bindings: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[non_exhaustive]
pub struct LossReport {
pub face: String,
pub total_source_elements: u64,
pub mapped_count: u64,
pub unmapped_count: u64,
pub is_lossless: bool,
pub loss_ratio: f64,
pub mapped_paths: Vec<String>,
pub unmapped_paths: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[non_exhaustive]
pub struct FaceResponse {
pub face: String,
pub version: String,
pub payload: Value,
pub loss_report: LossReport,
}
#[derive(Serialize)]
pub(crate) struct FilePayload<'a> {
pub content: &'a str,
pub filename: &'a str,
pub message_type: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
pub scheme: Option<&'a str>,
}
#[derive(Serialize)]
pub(crate) struct CheckRequest<'a> {
pub xml: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
pub scheme: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub schematron: Option<&'a str>,
}
#[derive(Serialize)]
pub(crate) struct FaceRequest<'a> {
pub xml: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bindings: Option<&'a Map<String, Value>>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn message_types_match_the_openapi_enum() {
let spec: Value = serde_json::from_str(include_str!("../conformance/openapi.json"))
.expect("openapi.json parses");
let wanted: Vec<&str> = spec["components"]["schemas"]["MessageType"]["enum"]
.as_array()
.expect("MessageType enum")
.iter()
.filter_map(Value::as_str)
.collect();
let ours: Vec<&str> = MessageType::ALL.iter().map(|m| m.as_str()).collect();
assert_eq!(ours, wanted);
assert_eq!(MessageType::Pain00800108.to_string(), "pain.008.001.08");
assert_eq!(MessageType::Pain00100109.as_ref(), "pain.001.001.09");
}
#[test]
fn optional_request_fields_are_left_out() {
let body = FilePayload {
content: "a",
filename: "p.csv",
message_type: "pain.001.001.09",
scheme: None,
};
let json = serde_json::to_value(&body).expect("serialises");
assert_eq!(json.as_object().map(Map::len), Some(3));
let check = CheckRequest {
xml: "<x/>",
scheme: Some("sepa-sct"),
schematron: None,
};
let json = serde_json::to_value(&check).expect("serialises");
assert_eq!(
json,
serde_json::json!({"xml": "<x/>", "scheme": "sepa-sct"})
);
}
#[test]
fn validation_response_defaults_the_optional_fields() {
let parsed: ValidationResponse =
serde_json::from_str(r#"{"is_valid": false, "total_rows": 2, "extra": 1}"#)
.expect("decodes");
assert_eq!(parsed.valid_rows, 0);
assert!(parsed.errors.is_empty() && parsed.scheme_violations.is_empty());
}
#[test]
fn row_errors_decode_the_server_shape() {
let parsed: GenerateResponse = serde_json::from_str(
r#"{"success": false, "message": "invalid",
"validation_errors": [{"field": "currency", "message": "bad", "value": "EUX"},
{"field": "id", "message": "missing", "value": null}]}"#,
)
.expect("decodes");
assert_eq!(parsed.validation_errors[0].field, "currency");
assert_eq!(parsed.validation_errors[0].value, Some(Value::from("EUX")));
assert_eq!(parsed.validation_errors[1].value, None);
}
}