Skip to content

Commit 2117e89

Browse files
committed
task: use std::sync::LazyLock instead of once_cell
1 parent bb111df commit 2117e89

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ license = "MIT"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
once_cell = { version = "~1", default-features = true, features = [] }
1110
regex = { version = "~1", default-features = false, features = [] }
1211
rust_decimal = { version = "~1", default-features = false, features = [] }
1312

src/payment_method.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::LazyLock;
2+
13
use regex::Regex;
24

35
use 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+
4255
impl<'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+
176189
impl<'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

357368
impl<'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
));

src/xml.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use std::borrow::Cow;
1+
use std::{borrow::Cow, sync::LazyLock};
22

3-
use once_cell::sync::Lazy;
43
use regex::Regex;
54

6-
static XML_ESCAPE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new("[&\"'<>]").unwrap());
7-
85
fn xml_escape(s: &mut Cow<str>) {
6+
static XML_ESCAPE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new("[&\"'<>]").unwrap());
7+
98
let r = s.as_ref();
109
if let Some(m) = XML_ESCAPE_REGEX.find(r) {
1110
let mut o = r[0..m.start()].to_string();

0 commit comments

Comments
 (0)