taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

iban.rs (2460B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2025 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 
     17 use std::str::FromStr;
     18 
     19 use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
     20 use rand::{RngExt, distr::Alphanumeric, seq::IndexedRandom};
     21 use taler_common::types::iban::{
     22     Country::{self, *},
     23     IBAN,
     24 };
     25 
     26 const COUNTRIES: [Country; 89] = [
     27     AD, AE, AL, AT, AZ, BA, BE, BG, BH, BI, BR, BY, CH, CR, CY, CZ, DE, DJ, DK, DO, EE, EG, ES, FI,
     28     FK, FO, FR, GB, GE, GI, GL, GR, GT, HN, HR, HU, IE, IL, IQ, IS, IT, JO, KW, KZ, LB, LC, LI, LT,
     29     LU, LV, LY, MC, MD, ME, MK, MN, MR, MT, MU, NI, NL, NO, OM, PK, PL, PS, PT, QA, RO, RS, RU, SA,
     30     SC, SD, SE, SI, SK, SM, SO, ST, SV, TL, TN, TR, UA, VA, VG, XK, YE,
     31 ];
     32 
     33 fn parser(c: &mut Criterion) {
     34     c.bench_function("iban_random_all", |b| {
     35         b.iter_batched(
     36             || {
     37                 rand::rng()
     38                     .sample_iter::<char, _>(rand::distr::StandardUniform)
     39                     .take(40)
     40                     .collect::<String>()
     41             },
     42             |case| IBAN::from_str(&case),
     43             BatchSize::SmallInput,
     44         )
     45     });
     46     c.bench_function("iban_random_alphanumeric", |b| {
     47         b.iter_batched(
     48             || {
     49                 rand::rng()
     50                     .sample_iter(&Alphanumeric)
     51                     .take(40)
     52                     .map(char::from)
     53                     .collect::<String>()
     54             },
     55             |case| IBAN::from_str(&case),
     56             BatchSize::SmallInput,
     57         )
     58     });
     59 
     60     c.bench_function("iban_valid", |b| {
     61         b.iter_batched(
     62             || IBAN::random(*COUNTRIES.choose(&mut rand::rng()).unwrap()).to_string(),
     63             |case| IBAN::from_str(&case).unwrap(),
     64             BatchSize::SmallInput,
     65         )
     66     });
     67 }
     68 
     69 criterion_group!(benches, parser);
     70 criterion_main!(benches);