swift_mt_message/traits.rs
1//! # Core Traits
2//!
3//! Fundamental traits for SWIFT message parsing and serialization.
4//!
5//! - **SwiftField**: Field-level parsing and serialization
6//! - **SwiftMessageBody**: Message-level operations and validation
7
8use crate::Result;
9use serde::{Deserialize, Serialize};
10use std::fmt::Debug;
11
12/// Trait for SWIFT field types
13///
14/// Implemented by all field types for parsing and serialization.
15/// Enum fields (Field50, Field59) support variant-based parsing.
16pub trait SwiftField: Serialize + for<'de> Deserialize<'de> + Clone + std::fmt::Debug {
17 /// Parse field from SWIFT format (without `:TAG:` prefix)
18 fn parse(value: &str) -> Result<Self>
19 where
20 Self: Sized;
21
22 /// Parse field with variant (e.g., 50A, 50K) for enum fields
23 fn parse_with_variant(
24 value: &str,
25 _variant: Option<&str>,
26 _field_tag: Option<&str>,
27 ) -> Result<Self>
28 where
29 Self: Sized,
30 {
31 Self::parse(value)
32 }
33
34 /// Convert to SWIFT format (includes `:TAG:` prefix)
35 fn to_swift_string(&self) -> String;
36
37 /// Get variant tag (e.g., "A", "K") for enum fields, None for simple fields
38 fn get_variant_tag(&self) -> Option<&'static str> {
39 None
40 }
41}
42
43/// Trait for SWIFT message types (MT103, MT202, etc.)
44///
45/// Provides parsing, serialization, and validation for complete messages.
46pub trait SwiftMessageBody: Debug + Clone + Send + Sync + Serialize + std::any::Any {
47 /// Get the message type identifier (e.g., "103", "202", "940")
48 fn message_type() -> &'static str;
49
50 /// Parse message from Block 4 content (fields only)
51 fn parse_from_block4(_block4: &str) -> Result<Self>
52 where
53 Self: Sized,
54 {
55 panic!("parse_from_block4 not implemented for message type")
56 }
57
58 /// Convert to SWIFT MT format (Block 4 content, no wrapper braces)
59 fn to_mt_string(&self) -> String;
60
61 /// Validate SWIFT network rules (C/D/E series) for this message
62 fn validate_network_rules(
63 &self,
64 _stop_on_first_error: bool,
65 ) -> Vec<crate::errors::SwiftValidationError> {
66 Vec::new()
67 }
68}