Skip to main content

faker_rust/default/
code.rs

1//! Code generator - generates random ISBN and EAN codes
2
3use crate::base::bothify;
4use crate::locale::{fetch_locale_with_context, sample_with_resolve};
5
6/// Generate a random ASIN code
7pub fn asin() -> String {
8    fetch_locale_with_context("code.asin", "en", Some("code"))
9        .map(|v| sample_with_resolve(&v, Some("code")))
10        .unwrap_or_else(|| bothify("B000#######"))
11}
12
13/// Generate a random ISBN-10 code
14pub fn isbn() -> String {
15    fetch_locale_with_context("code.isbn", "en", Some("code"))
16        .map(|v| sample_with_resolve(&v, Some("code")))
17        .unwrap_or_else(|| bothify("#########-#"))
18}
19
20/// Generate a random EAN code
21pub fn ean() -> String {
22    fetch_locale_with_context("code.ean", "en", Some("code"))
23        .map(|v| sample_with_resolve(&v, Some("code")))
24        .unwrap_or_else(|| bothify("#############"))
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn test_isbn() {
33        assert!(!isbn().is_empty());
34    }
35
36    #[test]
37    fn test_ean() {
38        assert!(!ean().is_empty());
39    }
40}