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
44
45
46
47
48
49
use super::{Metric, TimedEvent};
use crate::io::provider::StreamType;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

#[derive(Debug)]
pub struct DictMetric;

impl Metric for DictMetric {
    type State = DictState;
    type Event = DictEvent;

    fn stream_type() -> StreamType {
        StreamType::from("rillrate.dict.v0")
    }

    fn apply(state: &mut Self::State, event: TimedEvent<Self::Event>) {
        match event.event {
            DictEvent::Assign { key, value } => {
                state.map.insert(key, value);
            }
            DictEvent::Remove { key } => {
                state.map.remove(&key);
            }
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DictState {
    pub map: BTreeMap<String, String>,
}

#[allow(clippy::new_without_default)]
impl DictState {
    pub fn new() -> Self {
        Self {
            map: BTreeMap::new(),
        }
    }
}

pub type DictDelta = Vec<TimedEvent<DictEvent>>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DictEvent {
    Assign { key: String, value: String },
    Remove { key: String },
}