1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::collections::HashMap;
use rand::Rng;
use num_traits::Pow;
pub struct MobileCode {
mobile_codes: HashMap<u64, u64>,
length: u8,
}
impl MobileCode {
pub fn new(len: u8) -> Self {
MobileCode {
mobile_codes: HashMap::new(),
length: len,
}
}
fn code_generator(&self) -> u64 {
let low = Pow::pow(10u64, self.length - 1);
let high = Pow::pow(10u64, self.length) + 1;
rand::thread_rng().gen_range(low, high)
}
pub fn list(&self) {
for (key, value) in &self.mobile_codes {
println!("{} -> {}", key, value);
}
}
pub fn add(&mut self, mobile: u64) {
self.mobile_codes.insert(mobile, self.code_generator());
}
pub fn add_list(&mut self, list: &Vec<u64>) {
for mobile in list {
self.add(*mobile);
}
}
pub fn get(&self, mobile: &u64) -> Option<&u64> {
self.mobile_codes.get(mobile)
}
pub fn has(&self, mobile: &u64) -> bool {
self.mobile_codes.contains_key(mobile)
}
pub fn verify(&self, mobile: &u64, code: Option<&u64>) -> bool {
if self.mobile_codes.contains_key(mobile) {
self.mobile_codes.get(mobile) == code
}
else {
false
}
}
pub fn remove(&mut self, mobile: &u64) {
self.mobile_codes.remove(mobile);
}
pub fn clear(&mut self) {
self.mobile_codes.clear();
}
pub fn is_empty(&self) -> bool {
self.mobile_codes.is_empty()
}
}