Решение на упр.03 задача 3 от Нели Илиева
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 2 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::collections::HashMap;
#[derive(Debug, Clone)]
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 },
}
struct EventLog {
events: Vec<Event>,
}
impl EventLog {
fn new() -> Self {
EventLog {
events: Vec::new(),
}
}
fn add_event(&mut self, event: Event) {
self.events.push(event);
}
fn user_spent(&self, user: &str) -> f64 {
self.events.iter().filter_map(|event| {
match event {
Event::Purchase { user: purchase_user, amount, .. }
if purchase_user == user => Some(*amount),
_ => None,
}
}).sum()
}
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",
}.to_string();
*summary.entry(event_type).or_insert(0) += 1;
}
summary
}
fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
self.events.iter().filter(|event| {
let user_match = match user {
Some(username) => {
match event {
Event::Login { user, .. } => user == username,
Event::Logout { user, .. } => user == username,
Event::Purchase { user: purchase_user, .. } => purchase_user == username,
Event::Error { .. } => false,
}
}
None => true,
};
let time_match = match after {
Some(min_timestamp) => {
let event_timestamp = match event {
Event::Login { timestamp, .. } => timestamp,
Event::Logout { timestamp, .. } => timestamp,
Event::Purchase { timestamp, .. } => timestamp,
Event::Error { timestamp, .. } => timestamp,
};
event_timestamp > &min_timestamp
}
None => true,
};
user_match && time_match
}).collect()
}
}
Лог от изпълнението
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 memchr v2.7.6
Compiling syn v2.0.108
Compiling pin-project-lite v0.2.16
Compiling pin-utils v0.1.0
Compiling slab v0.4.11
Compiling futures-task v0.3.31
Compiling futures-io v0.3.31
Compiling solution v0.1.0 (/tmp/d20251030-1757769-pvjbw7/solution)
warning: enum `Event` is never used
--> src/lib.rs:4:6
|
4 | enum Event {
| ^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: struct `EventLog` is never constructed
--> src/lib.rs:11:8
|
11 | struct EventLog {
| ^^^^^^^^
warning: associated items `new`, `add_event`, `user_spent`, `summaries_by_type`, and `filter_events` are never used
--> src/lib.rs:16:8
|
15 | impl EventLog {
| ------------- associated items in this implementation
16 | fn new() -> Self {
| ^^^
...
22 | fn add_event(&mut self, event: Event) {
| ^^^^^^^^^
...
26 | fn user_spent(&self, user: &str) -> f64 {
| ^^^^^^^^^^
...
36 | fn summaries_by_type(&self) -> HashMap<String, usize> {
| ^^^^^^^^^^^^^^^^^
...
53 | fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
| ^^^^^^^^^^^^^
warning: `solution` (lib) generated 3 warnings
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.63s
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
