Skip to main content

fints_client/banks/
profile.rs

1//! What one bank does differently from the FinTS specification and its own
2//! parameter data, as a serialisable value.
3//!
4//! A profile holds only what the BPD and UPD cannot tell the client; job
5//! versions, statement formats, KTI form and TAN methods come from the
6//! bank's parameters. Evidence per entry: docs/banks.md.
7
8use std::borrow::Cow;
9
10use serde::{Deserialize, Serialize};
11
12use crate::protocol::TanOverrides;
13
14/// Which profile a [`BankProfile`] is: for logs, traces and callers that
15/// show bank-specific help.
16#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
17#[serde(rename_all = "lowercase")]
18pub enum ProfileId {
19    /// No bank-specific behaviour.
20    #[default]
21    Generic,
22    Dkb,
23    Comdirect,
24    Consorsbank,
25    Ing,
26}
27
28impl ProfileId {
29    /// The profile's name as serialised (`"dkb"`, `"generic"`, …).
30    pub fn as_str(self) -> &'static str {
31        match self {
32            Self::Generic => "generic",
33            Self::Dkb => "dkb",
34            Self::Comdirect => "comdirect",
35            Self::Consorsbank => "consorsbank",
36            Self::Ing => "ing",
37        }
38    }
39}
40
41impl std::fmt::Display for ProfileId {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        f.write_str(self.as_str())
44    }
45}
46
47/// How [`jobs::open`](crate::jobs::open) sets the session up.
48#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
49pub enum InitMode {
50    /// Synchronisation dialog when the session state has no system ID and
51    /// parameters, then the business dialog with its TAN if the bank asks.
52    #[default]
53    Standard,
54    /// No synchronisation dialog: the business dialog opens PIN-only with
55    /// the system ID of the session state (or a client-generated one) and
56    /// this security function. The bank sends its parameters in that
57    /// dialog and asks for no TAN.
58    Direct { security_function: String },
59}
60
61/// A bank's rule for completing the FinTS user ID the customer typed.
62///
63/// A user ID of only ASCII digits whose length is in
64/// `min_digits..=max_digits` gets `suffix` appended; any other user ID is
65/// sent as given.
66#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
67pub struct UserIdRule {
68    pub min_digits: u8,
69    pub max_digits: u8,
70    pub suffix: String,
71}
72
73impl UserIdRule {
74    /// The user ID as it is sent: `user_id` with `suffix` appended when it
75    /// is only ASCII digits and its length is in range, else unchanged.
76    pub fn apply<'a>(&self, user_id: &'a str) -> Cow<'a, str> {
77        let n = user_id.len();
78        let digits = !user_id.is_empty() && user_id.bytes().all(|b| b.is_ascii_digit());
79        if digits && (self.min_digits as usize..=self.max_digits as usize).contains(&n) {
80            Cow::Owned(format!("{user_id}{}", self.suffix))
81        } else {
82            Cow::Borrowed(user_id)
83        }
84    }
85}
86
87/// Which statement reference, if any, the bank keeps unique and stable
88/// for each booking: the source of [`Transaction::booking_id`].
89///
90/// [`Transaction::booking_id`]: crate::Transaction::booking_id
91#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
92pub enum BookingIdSource {
93    /// No reference is known to be unique and stable: `booking_id` stays
94    /// `None`. The MT940 `:61:` bank reference is never one (often
95    /// `NONREF` or repeated).
96    #[default]
97    None,
98    /// The entry-level camt `Ntry/AcctSvcrRef` of a booked entry. Pending
99    /// entries, MT940 statements and entries without the element get no ID.
100    CamtEntryAcctSvcrRef,
101}
102
103/// What one bank does differently. [`BankProfile::generic`] for banks
104/// without an entry.
105#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
106pub struct BankProfile {
107    pub id: ProfileId,
108    /// How the business dialog is opened. [`InitMode::Standard`] for most
109    /// banks.
110    pub init: InitMode,
111    /// Orders for which the profile fixes whether HKTAN is sent, beating
112    /// HIPINS. Empty for most banks.
113    pub tan_overrides: TanOverrides,
114    /// How the library completes the user ID before it is sent. `None`:
115    /// sent as given.
116    pub user_id: Option<UserIdRule>,
117    /// Which reference becomes [`Transaction::booking_id`]. `None` for
118    /// banks whose references are not known to be unique and stable.
119    ///
120    /// [`Transaction::booking_id`]: crate::Transaction::booking_id
121    pub booking_id: BookingIdSource,
122}
123
124impl BankProfile {
125    /// No bank-specific behaviour.
126    pub fn generic() -> Self {
127        Self::default()
128    }
129}