moqtap_client/draft14/
publish.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum PublishState {
4 Idle,
6 Publishing,
8 Active,
10 Done,
12}
13
14#[derive(Debug, thiserror::Error, PartialEq, Eq)]
16pub enum PublishError {
17 #[error("invalid transition from {from:?} on event {event}")]
19 InvalidTransition {
20 from: PublishState,
22 event: String,
24 },
25}
26
27pub struct PublishStateMachine {
30 state: PublishState,
31}
32
33impl Default for PublishStateMachine {
34 fn default() -> Self {
35 Self::new()
36 }
37}
38
39impl PublishStateMachine {
40 pub fn new() -> Self {
42 Self { state: PublishState::Idle }
43 }
44
45 pub fn state(&self) -> PublishState {
47 self.state
48 }
49
50 pub fn on_publish_sent(&mut self) -> Result<(), PublishError> {
52 if self.state == PublishState::Idle {
53 self.state = PublishState::Publishing;
54 Ok(())
55 } else {
56 Err(PublishError::InvalidTransition {
57 from: self.state,
58 event: "on_publish_sent".to_string(),
59 })
60 }
61 }
62
63 pub fn on_publish_ok(&mut self) -> Result<(), PublishError> {
65 if self.state == PublishState::Publishing {
66 self.state = PublishState::Active;
67 Ok(())
68 } else {
69 Err(PublishError::InvalidTransition {
70 from: self.state,
71 event: "on_publish_ok".to_string(),
72 })
73 }
74 }
75
76 pub fn on_publish_error(&mut self) -> Result<(), PublishError> {
78 if self.state == PublishState::Publishing {
79 self.state = PublishState::Done;
80 Ok(())
81 } else {
82 Err(PublishError::InvalidTransition {
83 from: self.state,
84 event: "on_publish_error".to_string(),
85 })
86 }
87 }
88
89 pub fn on_publish_done_sent(&mut self) -> Result<(), PublishError> {
91 if self.state == PublishState::Active {
92 self.state = PublishState::Done;
93 Ok(())
94 } else {
95 Err(PublishError::InvalidTransition {
96 from: self.state,
97 event: "on_publish_done_sent".to_string(),
98 })
99 }
100 }
101}