rill_view/flow/control/
toggle.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 ToggleState {
7    // IMMUTABLE
8    pub caption: String,
9
10    // MUTABLE
11    pub active: bool,
12    pub last_toggle: Option<Timestamp>,
13}
14
15#[allow(clippy::new_without_default)]
16impl ToggleState {
17    pub fn new(caption: String, active: bool) -> Self {
18        Self {
19            caption,
20            active,
21            last_toggle: None,
22        }
23    }
24
25    pub fn toggle_action(&self) -> ToggleAction {
26        ToggleAction::new(!self.active)
27    }
28}
29
30impl Flow for ToggleState {
31    type Action = ToggleAction;
32    type Event = ToggleEvent;
33
34    fn stream_type() -> StreamType {
35        StreamType::from("rillrate.flow.control.toggle.v0")
36    }
37
38    fn apply(&mut self, event: TimedEvent<Self::Event>) {
39        self.active = event.event.active;
40        self.last_toggle = Some(event.timestamp);
41    }
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct ToggleAction {
46    pub set_active: bool,
47}
48
49impl ToggleAction {
50    pub fn new(set_active: bool) -> Self {
51        Self { set_active }
52    }
53
54    pub fn on() -> Self {
55        Self { set_active: true }
56    }
57
58    pub fn off() -> Self {
59        Self { set_active: false }
60    }
61
62    pub fn into_value(self) -> bool {
63        self.set_active
64    }
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct ToggleEvent {
69    pub active: bool,
70}