Решение на упр.03 задача 3 от Божидар Виденов
Към профила на Божидар Виденов
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 2 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::{collections::HashMap, usize};
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,
    },
}
impl Event {}
pub struct EventLog {
    events: Vec<Event>,
}
impl EventLog {
    fn new() -> EventLog {
        Self { events: vec![] }
    }
    pub fn add_event(&mut self, event: Event) {
        self.events.push(event);
    }
    pub fn user_spent(&self, user: &str) -> f64 {
        self.events
            .iter()
            .filter_map(|e| match e {
                Event::Purchase {
                    user: e_user,
                    amount: price,
                    ..
                } if user == e_user => Some(price),
                _ => None,
            })
            .sum()
    }
    fn event_key_to_str(e: &Event) -> String {
        return match e {
            Event::Login { .. } => String::from("Login"),
            Event::Error { .. } => String::from("Error"),
            Event::Logout { .. } => String::from("Logout"),
            Event::Purchase { .. } => String::from("Purchase"),
        };
    }
    fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
        let user_v = user.unwrap_or_else(|| return "");
        let after_v = after.unwrap_or_else(|| return 0);
        return self
            .events
            .iter()
            .filter(|e| match e {
                Event::Login {
                    user: user_to_check,
                    timestamp: timestamp_to_check,
                } if (user_v == "" || user_to_check == user_v)
                    && (*timestamp_to_check > after_v) =>
                {
                    true
                }
                Event::Error {
                    timestamp: timestamp_to_check,
                    ..
                } if *timestamp_to_check > after_v && user_v == "" => true,
                Event::Logout {
                    user: user_to_check,
                    timestamp: timestamp_to_check,
                } if (user_v == "" || user_to_check == user_v)
                    && (*timestamp_to_check > after_v) =>
                {
                    true
                }
                Event::Purchase {
                    user: user_to_check,
                    timestamp: timestamp_to_check,
                    ..
                } if (user_v == "" || user_to_check == user_v)
                    && (*timestamp_to_check > after_v) =>
                {
                    true
                }
                _ => false,
            })
            .collect();
    }
    pub fn summaries_by_type(&self) -> HashMap<String, usize> {
        let mut summary: HashMap<String, usize> = HashMap::new();
        for event in self.events.iter() {
            let event_str = &EventLog::event_key_to_str(event);
            match summary.get(event_str) {
                Some(rec) => _ = summary.insert(event_str.to_string(), rec + 1),
                None => _ = summary.insert(event_str.to_string(), 1),
            }
        }
        summary
    }
}
Лог от изпълнението
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 pin-utils v0.1.0
   Compiling memchr v2.7.6
   Compiling syn v2.0.108
   Compiling slab v0.4.11
   Compiling futures-task v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling futures-io v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-1wlvp9u/solution)
warning: associated items `new` and `filter_events` are never used
  --> src/lib.rs:33:8
   |
32 | impl EventLog {
   | ------------- associated items in this implementation
33 |     fn new() -> EventLog {
   |        ^^^
...
64 |     fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
   |        ^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default
warning: `solution` (lib) generated 1 warning
   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.82s
     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
