update at 2020-09-02 18:08:02 by ehlxr
This commit is contained in:
parent
24cb5393e6
commit
af12dad539
82
src/main.rs
82
src/main.rs
@ -344,13 +344,13 @@ fn main() {
|
|||||||
// .filter(|x| x % 3 == 0)
|
// .filter(|x| x % 3 == 0)
|
||||||
.sum();
|
.sum();
|
||||||
println!("Counter sum: {}", sum);
|
println!("Counter sum: {}", sum);
|
||||||
let mut zip = Counter::new().zip(Counter::new());
|
let mut zip = Counter::new().zip(Counter::new());
|
||||||
|
|
||||||
println!("{:?}",zip.next().unwrap());
|
println!("{:?}", zip.next().unwrap());
|
||||||
println!("{:?}",zip.next().unwrap());
|
println!("{:?}", zip.next().unwrap());
|
||||||
println!("{:?}",zip.next().unwrap());
|
println!("{:?}", zip.next().unwrap());
|
||||||
println!("{:?}",zip.next().unwrap());
|
println!("{:?}", zip.next().unwrap());
|
||||||
println!("{:?}",zip.next().unwrap());
|
println!("{:?}", zip.next().unwrap());
|
||||||
|
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
println!("----------------------------------------");
|
println!("----------------------------------------");
|
||||||
@ -1632,6 +1632,45 @@ impl Iterator for Counter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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: &'a 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!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
@ -1687,4 +1726,35 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(total, 6);
|
assert_eq!(total, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct MockMessenger {
|
||||||
|
// sent_messages: Vec<String>,
|
||||||
|
sent_messages: RefCell<Vec<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MockMessenger {
|
||||||
|
fn new() -> MockMessenger {
|
||||||
|
MockMessenger {
|
||||||
|
// sent_messages: vec![],
|
||||||
|
sent_messages: RefCell::new(vec![]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Messenger for MockMessenger {
|
||||||
|
fn send(&self, message: &str) {
|
||||||
|
self.sent_messages.borrow_mut().push(String::from(message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_sends_an_over_75_percent_warning_message() {
|
||||||
|
let mock_messenger = MockMessenger::new();
|
||||||
|
let mut limit_tracker = LimitTracker::new(&mock_messenger, 100);
|
||||||
|
|
||||||
|
limit_tracker.set_value(80);
|
||||||
|
|
||||||
|
// assert_eq!(mock_messenger.sent_messages.len(), 1);
|
||||||
|
assert_eq!(mock_messenger.sent_messages.borrow().len(), 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user