Решение на упр.03 задача 3 от Мариян Момчилов

Обратно към всички решения

Към профила на Мариян Момчилов

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 1 успешни тест(а)
  • 1 неуспешни тест(а)

Код

use std::collections::HashMap;
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 s = 0.0;
for event in self.events.iter() {
s += match event {
Event::Purchase {
user: _user,
item,
amount,
timestamp,
} => *amount,
_ => 0.0,
}
}
s
}
pub fn summaries_by_type(&self) -> HashMap<String, usize> {
let mut map = HashMap::new();
for event in self.events.iter() {
let key = String::from(match event {
Event::Error {
code,
message,
timestamp,
} => "Error",
Event::Login { user, timestamp } => "Login",
Event::Logout { user, timestamp } => "Logout",
Event::Purchase {
user,
item,
amount,
timestamp,
} => "Purchase",
});
if let Some(val) = map.get_mut(&key) {
*val += 1;
} else {
map.insert(key, 1);
}
}
map
}
pub fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
let user_filter = !user.is_none();
let after_filter = !after.is_none();
if !user_filter && !after_filter {
return self.events.iter().collect();
}
let mut events: Vec<&Event> = Vec::new();
for event in self.events.iter() {
let add_event = match event {
Event::Login {
user: _user,
timestamp,
} => {
let user_match = if user_filter {
_user == user.unwrap()
} else {
true
};
let timestamp_match = if after_filter {
*timestamp >= after.unwrap()
} else {
true
};
user_match && timestamp_match
}
Event::Logout {
user: _user,
timestamp,
} => {
let user_match = if user_filter {
_user == user.unwrap()
} else {
true
};
let timestamp_match = if after_filter {
*timestamp >= after.unwrap()
} else {
true
};
user_match && timestamp_match
}
Event::Purchase {
user: _user,
item,
amount,
timestamp,
} => {
let user_match = if user_filter {
_user == user.unwrap()
} else {
true
};
let timestamp_match = if after_filter {
*timestamp >= after.unwrap()
} else {
true
};
user_match && timestamp_match
}
Event::Error {
code,
message,
timestamp,
} => {
!user_filter
&& if after_filter {
*timestamp >= after.unwrap()
} else {
true
}
}
};
if add_event {
events.push(event);
}
}
events
}
}

Лог от изпълнението

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-task v0.3.31
   Compiling futures-io v0.3.31
   Compiling slab v0.4.11
   Compiling syn v2.0.108
   Compiling memchr v2.7.6
   Compiling pin-utils v0.1.0
   Compiling pin-project-lite v0.2.16
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-gw8dbp/solution)
warning: unused variable: `item`
  --> src/lib.rs:44:21
   |
44 |                     item,
   |                     ^^^^ help: try ignoring the field: `item: _`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `timestamp`
  --> src/lib.rs:46:21
   |
46 |                     timestamp,
   |                     ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `user`
  --> src/lib.rs:38:30
   |
38 |     pub fn user_spent(&self, user: &str) -> f64 {
   |                              ^^^^ help: if this is intentional, prefix it with an underscore: `_user`

warning: unused variable: `code`
  --> src/lib.rs:59:21
   |
59 |                     code,
   |                     ^^^^ help: try ignoring the field: `code: _`

warning: unused variable: `message`
  --> src/lib.rs:60:21
   |
60 |                     message,
   |                     ^^^^^^^ help: try ignoring the field: `message: _`

warning: unused variable: `timestamp`
  --> src/lib.rs:61:21
   |
61 |                     timestamp,
   |                     ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `user`
  --> src/lib.rs:63:32
   |
63 |                 Event::Login { user, timestamp } => "Login",
   |                                ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `timestamp`
  --> src/lib.rs:63:38
   |
63 |                 Event::Login { user, timestamp } => "Login",
   |                                      ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `user`
  --> src/lib.rs:64:33
   |
64 |                 Event::Logout { user, timestamp } => "Logout",
   |                                 ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `timestamp`
  --> src/lib.rs:64:39
   |
64 |                 Event::Logout { user, timestamp } => "Logout",
   |                                       ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `user`
  --> src/lib.rs:66:21
   |
66 |                     user,
   |                     ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `item`
  --> src/lib.rs:67:21
   |
67 |                     item,
   |                     ^^^^ help: try ignoring the field: `item: _`

warning: unused variable: `amount`
  --> src/lib.rs:68:21
   |
68 |                     amount,
   |                     ^^^^^^ help: try ignoring the field: `amount: _`

warning: unused variable: `timestamp`
  --> src/lib.rs:69:21
   |
69 |                     timestamp,
   |                     ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `item`
   --> src/lib.rs:128:21
    |
128 |                     item,
    |                     ^^^^ help: try ignoring the field: `item: _`

warning: unused variable: `amount`
   --> src/lib.rs:129:21
    |
129 |                     amount,
    |                     ^^^^^^ help: try ignoring the field: `amount: _`

warning: unused variable: `code`
   --> src/lib.rs:145:21
    |
145 |                     code,
    |                     ^^^^ help: try ignoring the field: `code: _`

warning: unused variable: `message`
   --> src/lib.rs:146:21
    |
146 |                     message,
    |                     ^^^^^^^ help: try ignoring the field: `message: _`

warning: `solution` (lib) generated 18 warnings
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
warning: unused variable: `item`
  --> tests/../src/lib.rs:44:21
   |
44 |                     item,
   |                     ^^^^ help: try ignoring the field: `item: _`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `timestamp`
  --> tests/../src/lib.rs:46:21
   |
46 |                     timestamp,
   |                     ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `user`
  --> tests/../src/lib.rs:38:30
   |
38 |     pub fn user_spent(&self, user: &str) -> f64 {
   |                              ^^^^ help: if this is intentional, prefix it with an underscore: `_user`

warning: unused variable: `code`
  --> tests/../src/lib.rs:59:21
   |
59 |                     code,
   |                     ^^^^ help: try ignoring the field: `code: _`

warning: unused variable: `message`
  --> tests/../src/lib.rs:60:21
   |
60 |                     message,
   |                     ^^^^^^^ help: try ignoring the field: `message: _`

warning: unused variable: `timestamp`
  --> tests/../src/lib.rs:61:21
   |
61 |                     timestamp,
   |                     ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `user`
  --> tests/../src/lib.rs:63:32
   |
63 |                 Event::Login { user, timestamp } => "Login",
   |                                ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `timestamp`
  --> tests/../src/lib.rs:63:38
   |
63 |                 Event::Login { user, timestamp } => "Login",
   |                                      ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `user`
  --> tests/../src/lib.rs:64:33
   |
64 |                 Event::Logout { user, timestamp } => "Logout",
   |                                 ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `timestamp`
  --> tests/../src/lib.rs:64:39
   |
64 |                 Event::Logout { user, timestamp } => "Logout",
   |                                       ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `user`
  --> tests/../src/lib.rs:66:21
   |
66 |                     user,
   |                     ^^^^ help: try ignoring the field: `user: _`

warning: unused variable: `item`
  --> tests/../src/lib.rs:67:21
   |
67 |                     item,
   |                     ^^^^ help: try ignoring the field: `item: _`

warning: unused variable: `amount`
  --> tests/../src/lib.rs:68:21
   |
68 |                     amount,
   |                     ^^^^^^ help: try ignoring the field: `amount: _`

warning: unused variable: `timestamp`
  --> tests/../src/lib.rs:69:21
   |
69 |                     timestamp,
   |                     ^^^^^^^^^ help: try ignoring the field: `timestamp: _`

warning: unused variable: `item`
   --> tests/../src/lib.rs:128:21
    |
128 |                     item,
    |                     ^^^^ help: try ignoring the field: `item: _`

warning: unused variable: `amount`
   --> tests/../src/lib.rs:129:21
    |
129 |                     amount,
    |                     ^^^^^^ help: try ignoring the field: `amount: _`

warning: unused variable: `code`
   --> tests/../src/lib.rs:145:21
    |
145 |                     code,
    |                     ^^^^ help: try ignoring the field: `code: _`

warning: unused variable: `message`
   --> tests/../src/lib.rs:146:21
    |
146 |                     message,
    |                     ^^^^^^^ help: try ignoring the field: `message: _`

warning: `solution` (test "solution_test") generated 18 warnings
    Finished `test` profile [unoptimized + debuginfo] target(s) in 8.94s
     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 ... FAILED

failures:

---- solution_test::test_basic stdout ----
thread 'solution_test::test_basic' panicked at tests/solution_test.rs:82:5:
assertion `left == right` failed
  left: 34.0
 right: 0.0
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    solution_test::test_basic

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass `--test solution_test`

История (1 версия и 0 коментара)

Мариян качи първо решение на 25.10.2025 23:42 (преди 5 дена)