1use crate::config::FakerConfig;
4
5pub fn alphanumeric(length: usize) -> String {
7 let config = FakerConfig::current();
8 let chars: Vec<char> = (0..length)
9 .map(|_| {
10 let choice = config.rand_range(0, 3);
11 match choice {
12 0 => config.rand_char(&['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']),
13 1 => config.rand_char(&['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']),
14 _ => config.rand_char(&['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']),
15 }
16 })
17 .collect();
18 chars.into_iter().collect()
19}
20
21pub fn alpha(length: usize) -> String {
23 let config = FakerConfig::current();
24 let chars: Vec<char> = (0..length)
25 .map(|_| {
26 let choice = config.rand_bool();
27 if choice {
28 config.rand_char(&['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'])
29 } else {
30 config.rand_char(&['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'])
31 }
32 })
33 .collect();
34 chars.into_iter().collect()
35}
36
37pub fn numeric(length: usize) -> String {
39 let config = FakerConfig::current();
40 let chars: Vec<char> = (0..length)
41 .map(|_| config.rand_char(&['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']))
42 .collect();
43 chars.into_iter().collect()
44}
45
46pub fn hex(length: usize) -> String {
48 let config = FakerConfig::current();
49 let chars: Vec<char> = (0..length)
50 .map(|_| config.rand_char(&['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']))
51 .collect();
52 chars.into_iter().collect()
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_alphanumeric() {
61 let s = alphanumeric(10);
62 assert_eq!(s.len(), 10);
63 }
64
65 #[test]
66 fn test_alpha() {
67 let s = alpha(10);
68 assert_eq!(s.len(), 10);
69 assert!(s.chars().all(|c| c.is_alphabetic()));
70 }
71
72 #[test]
73 fn test_numeric() {
74 let s = numeric(10);
75 assert_eq!(s.len(), 10);
76 assert!(s.chars().all(|c| c.is_numeric()));
77 }
78
79 #[test]
80 fn test_hex() {
81 let s = hex(8);
82 assert_eq!(s.len(), 8);
83 assert!(s.chars().all(|c| c.is_ascii_hexdigit()));
84 }
85}