-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuriosity.rs
More file actions
43 lines (36 loc) · 1.07 KB
/
curiosity.rs
File metadata and controls
43 lines (36 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//! Projection storing curiosity scores for quick lookup.
use std::collections::HashMap;
use uuid::Uuid;
use crate::domain::{CuriosityScoreUpdated, Event};
/// Read model mapping identifiers to curiosity scores.
#[derive(Debug, Default)]
pub struct CuriosityScoreProjection {
scores: HashMap<Uuid, f64>,
}
impl CuriosityScoreProjection {
/// Builds the projection by replaying events.
#[must_use]
pub fn from_events(events: &[Event]) -> Self {
let mut proj = Self::default();
for event in events {
proj.apply(event);
}
proj
}
/// Applies a new event to update a score.
pub fn apply(&mut self, event: &Event) {
if let Event::CuriosityScoreUpdated(CuriosityScoreUpdated {
target_id,
new_score,
..
}) = event
{
self.scores.insert(*target_id, *new_score);
}
}
/// Returns the curiosity score for the given identifier.
#[must_use]
pub fn get(&self, id: Uuid) -> Option<f64> {
self.scores.get(&id).copied()
}
}