Решение на упр.03 задача 3 от Ивайло Андреев
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 2 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub enum Event {
Login { user: String, timestamp: u64 },
Logout { user: String, timestamp: u64 },
Purchase { user: String, item: String, amount: f64, timestamp: u64 },
Error { code: i32, message: String, timestamp: u64 },
}
pub struct EventLog {
events: Vec<Event>,
}
impl EventLog {
pub fn new() -> EventLog {
EventLog {
events: Vec::new(),
}
}
pub fn add_event(&mut self, event: Event) {
self.events.push(event);
}
pub fn user_spent(&self, user: &str) -> f64 {
let mut total_sum = 0.0;
for event in &self.events {
let amount_to_add = match event {
Event::Purchase { user: event_user, item: _, amount: user_amount, timestamp: _ } => {
if event_user == user {*user_amount} else {0.0}
},
_ => 0.0
};
total_sum = total_sum + amount_to_add
}
total_sum
}
pub fn summaries_by_type(&self) -> HashMap<String, usize> {
let mut summary = HashMap::new();
for event in &self.events {
let event_type = match event {
Event::Login { .. } => "Login",
Event::Logout { .. } => "Logout",
Event::Purchase { .. } => "Purchase",
Event::Error { .. } => "Error",
};
*summary.entry(event_type.to_string()).or_insert(0) += 1;
}
summary
}
pub fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
let mut result = Vec::new();
for event in &self.events {
let user_match = match user {
Some(filter_user) => match event {
Event::Login { user: event_user, .. } => event_user == filter_user,
Event::Logout { user: event_user, .. } => event_user == filter_user,
Event::Purchase { user: event_user, .. } => event_user == filter_user,
Event::Error { .. } => false,
},
None => true,
};
let time_match = match after {
Some(after_time) => match event {
Event::Login { timestamp, .. } => *timestamp > after_time,
Event::Logout { timestamp, .. } => *timestamp > after_time,
Event::Purchase { timestamp, .. } => *timestamp > after_time,
Event::Error { timestamp, .. } => *timestamp > after_time,
},
None => true,
};
if user_match && time_match {
result.push(event);
}
}
result
}
}
Лог от изпълнението
Updating crates.io index
Locking 17 packages to latest compatible versions
Compiling proc-macro2 v1.0.103
Compiling quote v1.0.41
Compiling unicode-ident v1.0.22
Compiling futures-core v0.3.31
Compiling futures-sink v0.3.31
Compiling futures-channel v0.3.31
Compiling futures-io v0.3.31
Compiling pin-utils v0.1.0
Compiling syn v2.0.108
Compiling slab v0.4.11
Compiling memchr v2.7.6
Compiling futures-task v0.3.31
Compiling pin-project-lite v0.2.16
Compiling solution v0.1.0 (/tmp/d20251030-1757769-7cydol/solution)
Compiling futures-macro v0.3.31
Compiling futures-util v0.3.31
Compiling futures-executor v0.3.31
Compiling futures v0.3.31
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.87s
Running tests/solution_test.rs (target/debug/deps/solution_test-bfd50394249726db)
running 2 tests
test solution_test::test_empty ... ok
test solution_test::test_basic ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
