Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ISIN Faker for finance module #152

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ DirPath();

```rust
Bic();
Isin();
```

### UUID
Expand Down
2 changes: 1 addition & 1 deletion fake/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fake"
version = "2.9.0"
version = "2.10.0"
authors = ["cksac <[email protected]>"]
description = "An easy to use library for generating fake data like name, number, address, lorem, dates, etc."
keywords = ["faker", "data", "generator", "random"]
Expand Down
11 changes: 11 additions & 0 deletions fake/examples/fakers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ fn phone_number_faker() {
println!("{:?}", val);
}

fn finance_faker() {
use fake::faker::finance::raw::*;

let val: String = Bic(EN).fake();
println!("{:?}", val);

let val: String = Isin(EN).fake();
println!("{:?}", val);
}

#[cfg(feature = "http")]
fn http_faker() {
use fake::faker::http::raw::*;
Expand Down Expand Up @@ -534,6 +544,7 @@ fn main() {
internet_faker();
number_faker();
phone_number_faker();
finance_faker();
currency_faker();
creditcard_faker();
barcode_faker();
Expand Down
44 changes: 44 additions & 0 deletions fake/src/faker/impls/finance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
];

const ALPHANUMERIC: &[char; 36] = &[
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
];

const ISO3166: &[&str] = &[
"AC", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN", "AO", "AQ", "AR", "AS", "AT", "AU", "AW",
"AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ",
Expand Down Expand Up @@ -71,3 +76,42 @@
)
}
}

fn split_number_to_digits(x: u32) -> Vec<u32> {
x.to_string()
.chars()
.map(|x| x.to_digit(10).unwrap())
.collect::<Vec<u32>>()
}

impl<L: Data> Dummy<Isin<L>> for String {
fn dummy_with_rng<R: Rng + ?Sized>(_: &Isin<L>, rng: &mut R) -> Self {
let country_code = *ISO3166.choose(rng).unwrap();
let nsin = (1..10)
.map(|_x| *ALPHANUMERIC.choose(rng).unwrap())
.collect::<String>();
// Checksum calculation according to Luhn algorithm
let char_to_num: Vec<u32> = country_code
.chars()
.chain(nsin.chars())
.map(|x| {
if x.is_ascii_digit() {
x.to_digit(10).unwrap()
} else {
10 + ((x as u32) - ('A' as u32))
}
})
.flat_map(split_number_to_digits)
.collect();
let checksum_even = char_to_num.iter().rev().skip(1).step_by(2).sum::<u32>();
let checksum_odd = char_to_num
.iter()
.rev()
.step_by(2)
.map(|&x| x * 2)
.flat_map(split_number_to_digits)
.sum::<u32>();
let checksum_digit = (10 - ((checksum_even + checksum_odd) % 10)) % 10;
return format!("{}{}{}", country_code, nsin, checksum_digit);

Check failure on line 115 in fake/src/faker/impls/finance.rs

View workflow job for this annotation

GitHub Actions / Clippy

unneeded `return` statement
}
}
2 changes: 1 addition & 1 deletion fake/src/faker/impls/internet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::faker::lorem::raw::Word;
use crate::faker::name::raw::FirstName;
use crate::locales::Data;
use crate::{Dummy, Fake, Faker};
use deunicode::AsciiChars;
use rand::distributions::{Distribution, Uniform};
use rand::seq::SliceRandom;
use rand::Rng;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use deunicode::AsciiChars;

impl<L: Data> Dummy<FreeEmailProvider<L>> for String {
fn dummy_with_rng<R: Rng + ?Sized>(_: &FreeEmailProvider<L>, rng: &mut R) -> Self {
Expand Down
1 change: 1 addition & 0 deletions fake/src/faker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ pub mod currency {
pub mod finance {
def_fakers! {
Bic();
Isin();
}
}

Expand Down
1 change: 1 addition & 0 deletions fake/tests/determinism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ check_determinism! { one fake_semver_en, String, Semver(EN) }
use fake::faker::finance::raw::*;

check_determinism! { l10d Bic; String, fake_bic_en, fake_bic_fr, fake_bic_cn, fake_bic_tw, fake_bic_jp }
check_determinism! { l10d Isin; String, fake_isin_en, fake_isin_fr, fake_isin_cn, fake_isin_tw, fake_isin_jp}

// HTTP
#[cfg(feature = "http")]
Expand Down
Loading