rill_view/flow/control/
selector.rs

1use rill_protocol::flow::core::{Flow, TimedEvent};
2use rill_protocol::io::provider::{StreamType, Timestamp};
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct SelectorState {
7    // IMMUTABLE
8    pub label: String,
9    /// It's `Vec` to keep the order.
10    pub options: Vec<String>,
11
12    // MUTABLE
13    pub selected: String,
14    pub updated: Option<Timestamp>,
15}
16
17#[allow(clippy::new_without_default)]
18impl SelectorState {
19    pub fn new(label: String, options: Vec<String>, selected: String) -> Self {
20        Self {
21            label,
22            options,
23            selected,
24            updated: None,
25        }
26    }
27}
28
29impl Flow for SelectorState {
30    type Action = SelectorAction;
31    type Event = SelectorEvent;
32
33    fn stream_type() -> StreamType {
34        StreamType::from("rillrate.flow.control.selector.v0")
35    }
36
37    fn apply(&mut self, event: TimedEvent<Self::Event>) {
38        let new_value = event.event.selected;
39        if self.options.contains(&new_value) {
40            self.selected = new_value;
41        } else {
42            log::error!("No option {} in the selector: {}.", new_value, self.label);
43        }
44        self.updated = Some(event.timestamp);
45    }
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct SelectorAction {
50    pub select: String,
51}
52
53impl SelectorAction {
54    pub fn select(value: String) -> Self {
55        Self { select: value }
56    }
57
58    pub fn into_value(self) -> String {
59        self.select
60    }
61}
62
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct SelectorEvent {
65    pub selected: String,
66}