pub mod mt535;
pub mod mt536;
pub(crate) mod read;
pub(crate) mod request;
pub mod response;
mod swift;
use crate::codec::{self, DataElement, RawSegment, Scalar};
use crate::error::{FinTSError, Result};
use crate::types::SegmentType;
pub(crate) fn verbatim<T: Scalar>(value: &str, what: &str) -> Result<T> {
T::from_wire(&DataElement::Text(value.to_owned()))
.map_err(|e| FinTSError::Serialize(format!("{what}: {e}")))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Order {
Balance,
Transactions,
Camt,
Holdings,
DepotTransactions,
Transfer,
InstantTransfer,
VopCheck,
VopExecute,
InstantStatus,
}
impl Order {
#[cfg(test)]
pub const ALL: &'static [Order] = &[
Order::Balance,
Order::Transactions,
Order::Camt,
Order::Holdings,
Order::DepotTransactions,
Order::Transfer,
Order::InstantTransfer,
Order::VopCheck,
Order::VopExecute,
Order::InstantStatus,
];
pub fn code(self) -> &'static str {
match self {
Order::Balance => "HKSAL",
Order::Transactions => "HKKAZ",
Order::Camt => "HKCAZ",
Order::Holdings => "HKWPD",
Order::DepotTransactions => "HKWDU",
Order::Transfer => "HKCCS",
Order::InstantTransfer => "HKIPZ",
Order::VopCheck => "HKVPP",
Order::VopExecute => "HKVPA",
Order::InstantStatus => "HKIPS",
}
}
pub fn implemented(self) -> &'static [u16] {
match self {
Order::Balance => &[5, 6, 7, 8],
Order::Transactions => &[5, 6, 7],
Order::Camt => &[1],
Order::Holdings => &[5, 6],
Order::DepotTransactions => &[5],
Order::Transfer => &[1],
Order::InstantTransfer => &[1, 2],
Order::VopCheck => &[1],
Order::VopExecute => &[1],
Order::InstantStatus => &[1],
}
}
}
pub(crate) fn unbuildable(code: &str, version: u16) -> FinTSError {
FinTSError::Internal(format!("{code} v{version} has no builder"))
}
pub(crate) fn not_addressable(
code: &str,
version: u16,
reason: crate::error::AddressingReason,
) -> FinTSError {
FinTSError::AccountNotAddressable {
code: SegmentType::new(code),
version,
reason,
}
}
pub(crate) fn encode<T: codec::Segment>(value: &T, number: u16) -> Result<RawSegment> {
codec::encode_segment(value, number, None).map_err(|e| FinTSError::Serialize(e.to_string()))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::protocol::Account;
use crate::types::Blz;
use jiff::civil::Date;
fn build(order: Order, version: u16) -> Result<(String, String)> {
let giro = Account::new("DE00120300009900012345", "BYLADEM1001")?;
let depot = Account::national("0123456789", None, &Blz::new("12030000"), "BYLADEM1001")?;
let day = Date::new(2026, 1, 1).unwrap();
let degs = match order {
Order::Balance => request::hksal(3, version, &giro, None)?.degs,
Order::Transactions => request::hkkaz(3, version, &giro, day, day, None)?.degs,
Order::Camt => {
request::hkcaz(3, version, &giro, request::CAMT052_DEFAULT, day, day, None)?.degs
}
Order::Holdings => request::hkwpd(3, version, &depot, None, None)?.degs,
Order::DepotTransactions => request::hkwdu(3, version, &depot, None, None, None)?.degs,
Order::Transfer => request::hkccs(3, version, &giro, "pain", b"<x/>")?.degs,
Order::InstantTransfer => {
request::hkipz(3, version, &giro, "pain", b"<x/>", Some(true))?.degs
}
Order::VopCheck => request::hkvpp(3, version, &["pain.002".into()], None, None)?.degs,
Order::VopExecute => request::hkvpa(3, version, b"VOPID")?.degs,
Order::InstantStatus => {
request::hkips(3, version, &giro, &["pain".into()], "ORDER-1")?.degs
}
};
Ok((
degs[0].get_str(0).to_string(),
degs[0].get_str(2).to_string(),
))
}
#[test]
fn implemented_versions_match_the_builders() {
for &order in Order::ALL.iter() {
let implemented = order.implemented();
assert!(implemented.is_sorted());
let registered = codec::versions(order.code());
assert!(
registered.is_empty() || implemented.iter().all(|v| registered.contains(v)),
"{order:?}: {implemented:?} not all in the codec registry {registered:?}"
);
for &v in implemented {
let (code, version) =
build(order, v).unwrap_or_else(|e| panic!("{order:?} v{v}: {e}"));
assert_eq!((code.as_str(), version), (order.code(), v.to_string()));
}
let next = implemented.last().unwrap() + 1;
assert!(
matches!(build(order, next), Err(FinTSError::Internal(_))),
"{order:?} v{next}"
);
}
}
#[test]
fn registry_versions_without_builder_are_not_claimed() {
assert_eq!(codec::versions("HKSPA"), [1]);
assert!(Order::ALL.iter().all(|o| o.code() != "HKSPA"));
}
}