1+ use std:: sync:: LazyLock ;
2+
13use regex:: Regex ;
24
35use crate :: xml:: { ToXml , XmlElement } ;
@@ -39,6 +41,17 @@ pub struct PaymentMethodSEPADirectDebit<'a> {
3941 debit_collection_date : Option < & ' a str > ,
4042}
4143
44+ const BIC_REGEX_STR : & str = r"^[0-9A-Za-z]{8}([0-9A-Za-z]{3})?$" ;
45+
46+ trait MatchBicRegex {
47+ fn match_bic_regex ( bic : & str ) -> bool {
48+ static BIC_REGEX : LazyLock < Regex > = LazyLock :: new ( || Regex :: new ( BIC_REGEX_STR ) . unwrap ( ) ) ;
49+ !BIC_REGEX . is_match ( bic)
50+ }
51+ }
52+
53+ impl MatchBicRegex for PaymentMethodSEPADirectDebit < ' _ > { }
54+
4255impl < ' a > PaymentMethodSEPADirectDebit < ' a > {
4356 pub fn new ( ) -> PaymentMethodSEPADirectDebit < ' a > {
4457 PaymentMethodSEPADirectDebit {
@@ -52,10 +65,8 @@ impl<'a> PaymentMethodSEPADirectDebit<'a> {
5265 }
5366
5467 pub fn with_bic ( mut self , bic : & ' a str ) -> Result < Self , String > {
55- let bic_regex_str = r"^[0-9A-Za-z]{8}([0-9A-Za-z]{3})?$" ;
56- let bic_regex = Regex :: new ( bic_regex_str) . unwrap ( ) ;
57- if !bic_regex. is_match ( bic) {
58- return Err ( format ! ( "BIC {bic} doesn't match regex {bic_regex_str}!" ) ) ;
68+ if !PaymentMethodSEPADirectDebit :: match_bic_regex ( bic) {
69+ return Err ( format ! ( "BIC {bic} doesn't match regex {BIC_REGEX_STR}!" ) ) ;
5970 }
6071 self . bic = Some ( bic) ;
6172 Ok ( self )
@@ -99,11 +110,11 @@ impl<'a> PaymentMethodSEPADirectDebit<'a> {
99110 mut self ,
100111 debit_collection_date : & ' a str ,
101112 ) -> Result < Self , String > {
102- let date_regex_str = r"^[0-9]{4}-[0-9]{2}-[0-9]{2}$" ;
103- let date_regex = Regex :: new ( date_regex_str ) . unwrap ( ) ;
104- if !date_regex . is_match ( debit_collection_date) {
113+ static DATE_REGEX_STR : & str = r"^[0-9]{4}-[0-9]{2}-[0-9]{2}$" ;
114+ static DATE_REGEX : LazyLock < Regex > = LazyLock :: new ( || Regex :: new ( DATE_REGEX_STR ) . unwrap ( ) ) ;
115+ if !DATE_REGEX . is_match ( debit_collection_date) {
105116 return Err ( format ! (
106- "DebitCollectionDate {debit_collection_date} doesn't match regex {date_regex_str }!"
117+ "DebitCollectionDate {debit_collection_date} doesn't match regex {DATE_REGEX_STR }!"
107118 ) ) ;
108119 }
109120 self . debit_collection_date = Some ( debit_collection_date) ;
@@ -173,6 +184,8 @@ pub struct PaymentMethodUniversalBankTransactionBeneficiaryAccount<'a> {
173184 bank_account_owner : Option < & ' a str > ,
174185}
175186
187+ impl MatchBicRegex for PaymentMethodUniversalBankTransactionBeneficiaryAccount < ' _ > { }
188+
176189impl < ' a > PaymentMethodUniversalBankTransactionBeneficiaryAccount < ' a > {
177190 pub fn new ( ) -> PaymentMethodUniversalBankTransactionBeneficiaryAccount < ' a > {
178191 PaymentMethodUniversalBankTransactionBeneficiaryAccount {
@@ -203,10 +216,8 @@ impl<'a> PaymentMethodUniversalBankTransactionBeneficiaryAccount<'a> {
203216 }
204217
205218 pub fn with_bic ( mut self , bic : & ' a str ) -> Result < Self , String > {
206- let bic_regex_str = r"^[0-9A-Za-z]{8}([0-9A-Za-z]{3})?$" ;
207- let bic_regex = Regex :: new ( bic_regex_str) . unwrap ( ) ;
208- if !bic_regex. is_match ( bic) {
209- return Err ( format ! ( "BIC {bic} doesn't match regex {bic_regex_str}!" ) ) ;
219+ if !PaymentMethodUniversalBankTransactionBeneficiaryAccount :: match_bic_regex ( bic) {
220+ return Err ( format ! ( "BIC {bic} doesn't match regex {BIC_REGEX_STR}!" ) ) ;
210221 }
211222 self . bic = Some ( bic) ;
212223 Ok ( self )
@@ -356,9 +367,10 @@ pub struct PaymentMethodPaymentCard<'a> {
356367
357368impl < ' a > PaymentMethodPaymentCard < ' a > {
358369 pub fn new ( primary_account_number : & ' a str ) -> Result < PaymentMethodPaymentCard < ' a > , String > {
359- let payment_card_regex_str = r"^[0-9]{0,6}\*[0-9]{0,4}$" ;
360- let payment_card_regex = Regex :: new ( payment_card_regex_str) . unwrap ( ) ;
361- if !payment_card_regex. is_match ( primary_account_number) {
370+ static PAYMENT_CARD_REGEX_STR : & str = r"^[0-9]{0,6}\*[0-9]{0,4}$" ;
371+ static PAYMENT_CARD_REGEX : LazyLock < Regex > =
372+ LazyLock :: new ( || Regex :: new ( PAYMENT_CARD_REGEX_STR ) . unwrap ( ) ) ;
373+ if !PAYMENT_CARD_REGEX . is_match ( primary_account_number) {
362374 return Err ( format ! (
363375 "Invalid primary account number \" {primary_account_number}\" . Only provide at most the first 6 and last 4 digits, separated with a \" *\" ." ,
364376 ) ) ;
0 commit comments