1use crate::account::AccountId;
2use crate::collections::ResourceId;
3use crate::error::{M10Error, M10Result};
4use crate::types::PublicKey;
5use m10_protos::sdk;
6use m10_protos::sdk::BankAccountRef;
7use parse_display::helpers::once_cell::sync::Lazy;
8use parse_display::helpers::regex::Regex;
9use reqwest::Url;
10use serde::Serialize;
11
12static BIC_REGEX: Lazy<Regex> =
13 Lazy::new(|| Regex::new(r"^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$").unwrap());
14
15#[derive(Clone, Debug, Serialize)]
16pub struct Bank {
17 pub id: ResourceId,
18 pub owner: PublicKey,
19 pub short_name: String,
20 pub display_name: String,
21 pub accounts: Vec<BankAccount>,
22 pub status: BankStatus,
23 pub country_code: CountryCode,
24 pub endpoint: Endpoint,
25 pub logo_url: String,
26 pub description: String,
27 pub bic_swift_code: BicSwiftCode,
28}
29
30#[cfg(feature = "format")]
31impl std::fmt::Display for Bank {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 let Self {
34 id,
35 owner,
36 short_name,
37 display_name,
38 accounts,
39 status,
40 country_code,
41 endpoint,
42 logo_url,
43 description,
44 bic_swift_code,
45 } = self;
46 write!(
47 f,
48 "Bank{{ id={id} owner={owner} short_name={short_name} display_name={display_name} accounts=[",
49 )?;
50 for account in accounts {
51 write!(f, "{account},")?;
52 }
53 write!(f, "] status={status} country_code={country_code} endpoint={endpoint} logo_url={logo_url} description={description} bic_swift_code={bic_swift_code} }}")
54 }
55}
56
57#[cfg_attr(feature = "format", derive(parse_display::Display))]
58#[cfg_attr(
59 feature = "format",
60 display("BankAccount{{ id={id} type={account_type} }}")
61)]
62#[derive(Clone, Debug, Serialize)]
63pub struct BankAccount {
64 pub id: AccountId,
65 pub account_type: BankAccountType,
66}
67
68#[cfg_attr(feature = "format", derive(parse_display::Display))]
69#[derive(Clone, Debug, Copy, Serialize)]
70pub enum BankAccountType {
71 CentralBankDigitalCurrency,
72 DigitalRegulatedMoney,
73}
74
75#[cfg_attr(feature = "format", derive(parse_display::Display))]
76#[derive(Clone, Debug, PartialEq, Serialize)]
77pub enum BankStatus {
78 Active,
79 Pending,
80 Suspended,
81 Terminated,
82}
83
84#[cfg_attr(feature = "format", derive(parse_display::Display))]
85#[derive(Clone, Debug, PartialEq, Serialize)]
86pub struct CountryCode(String);
87
88#[cfg_attr(feature = "format", derive(parse_display::Display))]
89#[derive(Clone, Debug, PartialEq, Serialize)]
90pub struct Endpoint(String);
91
92#[cfg_attr(feature = "format", derive(parse_display::Display))]
93#[derive(Clone, Debug, PartialEq, Serialize)]
94pub struct BicSwiftCode(String);
95
96impl TryFrom<sdk::Bank> for Bank {
97 type Error = M10Error;
98
99 fn try_from(bank: sdk::Bank) -> Result<Self, Self::Error> {
100 Ok(Self {
101 id: ResourceId::try_from(bank.id.as_slice())?,
102 owner: PublicKey(bank.owner),
103 short_name: bank.short_name,
104 display_name: bank.display_name,
105 accounts: bank
106 .accounts
107 .into_iter()
108 .map(BankAccount::try_from)
109 .collect::<M10Result<_>>()?,
110 status: BankStatus::from(
111 sdk::bank::BankStatus::try_from(bank.status)
112 .map_err(|_| M10Error::InvalidTransaction)?,
113 ),
114 country_code: CountryCode::try_from(bank.country_code)?,
115 endpoint: Endpoint::try_from(bank.endpoint)?,
116 logo_url: bank.logo_url,
117 description: bank.description,
118 bic_swift_code: BicSwiftCode::try_from(bank.bic_swift_code)?,
119 })
120 }
121}
122
123impl From<sdk::bank::BankStatus> for BankStatus {
124 fn from(status: sdk::bank::BankStatus) -> Self {
125 match status {
126 sdk::bank::BankStatus::Active => BankStatus::Active,
127 sdk::bank::BankStatus::Pending => BankStatus::Pending,
128 sdk::bank::BankStatus::Suspended => BankStatus::Suspended,
129 sdk::bank::BankStatus::Terminated => BankStatus::Terminated,
130 }
131 }
132}
133
134impl TryFrom<BankAccountRef> for BankAccount {
135 type Error = M10Error;
136
137 fn try_from(account: BankAccountRef) -> Result<Self, Self::Error> {
138 Ok(Self {
139 id: AccountId::try_from(account.account_id.as_slice())?,
140 account_type: BankAccountType::from(
141 sdk::bank_account_ref::BankAccountType::try_from(account.account_type)
142 .map_err(|_| M10Error::InvalidTransaction)?,
143 ),
144 })
145 }
146}
147
148impl From<sdk::bank_account_ref::BankAccountType> for BankAccountType {
149 fn from(account_type: sdk::bank_account_ref::BankAccountType) -> Self {
150 match account_type {
151 sdk::bank_account_ref::BankAccountType::Cbdc => {
152 BankAccountType::CentralBankDigitalCurrency
153 }
154 sdk::bank_account_ref::BankAccountType::Drm => BankAccountType::DigitalRegulatedMoney,
155 }
156 }
157}
158
159impl TryFrom<String> for CountryCode {
160 type Error = M10Error;
161
162 fn try_from(s: String) -> Result<Self, Self::Error> {
163 if s.is_empty() {
164 return Ok(Self(String::new()));
165 }
166
167 if s.len() != 2 || !s.chars().all(|c| c.is_ascii_uppercase()) {
168 return Err(M10Error::InvalidTransaction);
169 }
170
171 Ok(Self(s))
172 }
173}
174
175impl TryFrom<String> for Endpoint {
176 type Error = M10Error;
177
178 fn try_from(s: String) -> Result<Self, Self::Error> {
179 if s.is_empty() {
180 return Ok(Self(String::new()));
181 }
182
183 let url = Url::parse(&s).map_err(|_| M10Error::InvalidTransaction)?;
184
185 if url.scheme().is_empty() || url.host_str().is_none() {
186 return Err(M10Error::InvalidTransaction);
187 }
188
189 Ok(Self(s))
190 }
191}
192
193impl TryFrom<String> for BicSwiftCode {
194 type Error = M10Error;
195
196 fn try_from(s: String) -> Result<Self, Self::Error> {
197 if s.is_empty() {
198 return Ok(Self(String::new()));
199 }
200
201 if !BIC_REGEX.is_match(&s) {
202 return Err(M10Error::InvalidTransaction);
203 }
204
205 Ok(Self(s))
206 }
207}