rill_protocol/flow/
core.rs1use crate::encoding;
2use crate::io::provider::{PackedAction, PackedEvent, PackedState, ProviderReqId, StreamType};
3use anyhow::Error;
4use serde::{de::DeserializeOwned, Deserialize, Serialize};
5use std::fmt;
6
7pub trait DataFraction:
10 DeserializeOwned + Serialize + Clone + fmt::Debug + Sync + Send + 'static
11{
12}
13
14impl<T> DataFraction for T where
15 T: DeserializeOwned + Serialize + Clone + fmt::Debug + Sync + Send + 'static
16{
17}
18
19pub trait Flow: DataFraction {
21 type Action: DataFraction;
23
24 type Event: DataFraction;
26
27 fn stream_type() -> StreamType;
28
29 fn apply(&mut self, event: Self::Event);
30
31 fn pack_state(&self) -> Result<PackedState, Error> {
32 encoding::pack(self)
33 }
34
35 fn unpack_state(data: &PackedState) -> Result<Self, Error> {
36 encoding::unpack(data)
37 }
38
39 fn pack_event(delta: &Self::Event) -> Result<PackedEvent, Error> {
40 encoding::pack(delta)
41 }
42
43 fn unpack_event(data: &PackedEvent) -> Result<Self::Event, Error> {
44 encoding::unpack(data)
45 }
46
47 fn pack_action(action: &Self::Action) -> Result<PackedAction, Error> {
48 encoding::pack(action)
49 }
50
51 fn unpack_action(data: &PackedAction) -> Result<Self::Action, Error> {
52 encoding::unpack(data)
53 }
54}
55
56#[derive(Debug, Clone)]
58pub struct ActionEnvelope<T: Flow> {
59 pub origin: ProviderReqId,
61 pub activity: Activity,
63 pub action: Option<T::Action>,
67}
68
69#[derive(Debug, Clone)]
74pub enum Activity {
75 Suspend = 0, Awake = 1, Disconnected = 2, Connected = 3, Action = 4, }
88
89impl Activity {
90 pub fn is_action(&self) -> bool {
91 matches!(self, Self::Action)
92 }
93}
94
95#[derive(Debug, Clone, Deserialize, Serialize)]
96pub enum FlowMode {
97 Realtime,
98 Throttle { ms: u64 },
99 FlushOnly,
100}
101
102impl Default for FlowMode {
103 fn default() -> Self {
104 Self::Realtime
105 }
106}