mirror of https://github.com/KaiserY/trpl-zh-cn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
953 B
39 lines
953 B
3 years ago
|
pub trait Messenger {
|
||
|
fn send(&self, msg: &str);
|
||
|
}
|
||
|
|
||
|
pub struct LimitTracker<'a, T: Messenger> {
|
||
|
messenger: &'a T,
|
||
|
value: usize,
|
||
|
max: usize,
|
||
|
}
|
||
|
|
||
|
impl<'a, T> LimitTracker<'a, T>
|
||
|
where
|
||
|
T: Messenger,
|
||
|
{
|
||
|
pub fn new(messenger: &T, max: usize) -> LimitTracker<T> {
|
||
|
LimitTracker {
|
||
|
messenger,
|
||
|
value: 0,
|
||
|
max,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn set_value(&mut self, value: usize) {
|
||
|
self.value = value;
|
||
|
|
||
|
let percentage_of_max = self.value as f64 / self.max as f64;
|
||
|
|
||
|
if percentage_of_max >= 1.0 {
|
||
|
self.messenger.send("Error: You are over your quota!");
|
||
|
} else if percentage_of_max >= 0.9 {
|
||
|
self.messenger
|
||
|
.send("Urgent warning: You've used up over 90% of your quota!");
|
||
|
} else if percentage_of_max >= 0.75 {
|
||
|
self.messenger
|
||
|
.send("Warning: You've used up over 75% of your quota!");
|
||
|
}
|
||
|
}
|
||
|
}
|