warlocks_cauldron/providers/
code.rs

1use super::dependencies::*;
2
3
4/// Methods collection which provides methods for generating codes
5pub struct Code;
6
7impl Code {
8    /// Get a random locale code (MS-LCID)
9    ///
10    /// return example: ru
11    pub fn locale_code() -> &'static str {
12        get_random_element(LOCALE_CODES.iter())
13    }
14
15    /// Generate a random ISSN
16    ///
17    /// return example: 1313-6666
18    pub fn issn() -> String {
19        custom_code("####-####", '@', '#')
20    }
21
22    /// Generate ISBN for current locale
23    ///
24    /// return example: isbn formatted string
25    /// 
26    /// # Arguments
27    /// * `fmt` - ISBN format
28    /// * `locale` - Locale code from enum
29    pub fn isbn(fmt: Option<ISBNFormat>, locale: Locale) -> String {
30        let data = locale.get_data();
31
32        let fmt_key = validate_enum(fmt, None);
33
34        let mask = ISBN_MASKS.get(fmt_key).expect("ISBN_MASKS doesnt have current ISBNFormat!")
35            .replace("{0}", ISBN_GROUPS.get(&data.lang_code[..]).unwrap_or_else(|| ISBN_GROUPS.get("default").unwrap()));
36
37        custom_code(&mask, '@', '#')
38    }
39
40    /// Generate EAN
41    /// 
42    /// return example: ean formatted string
43    /// 
44    /// # Arguments
45    /// * `fmt` - Format of EAN
46    pub fn ean(fmt: Option<EANFormat>) -> String {
47        custom_code(EAN_MASKS.get(validate_enum(fmt, None)).expect("EAN_MASKS doesnt have current EANFormat!"), '@', '#')
48    }
49
50    /// Generate a random IMEI
51    /// 
52    /// return example: imei string
53    pub fn imei() -> String {
54        let num = format!("{}{}", get_random_element(IMEI_TACS.iter()), randint(100000, 999999));
55        format!("{num}{}", luhn::checksum(num.as_bytes()))
56    }
57
58    /// Generate a random PIN code
59    /// 
60    /// return example: pin string
61    pub fn pin() -> String {
62        custom_code("####", '@', '#')
63    }
64}