Skip to main content

river_data_core/models/
status.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
4#[serde(rename_all = "snake_case")]
5pub enum ServiceStatus {
6    Starting,
7    Idle,
8    Running,
9    Paused,
10    Syncing,
11    Error,
12    Stopping,
13}
14
15impl ServiceStatus {
16    pub const ALL: &[ServiceStatus] = &[
17        Self::Starting,
18        Self::Idle,
19        Self::Running,
20        Self::Paused,
21        Self::Syncing,
22        Self::Error,
23        Self::Stopping,
24    ];
25
26    pub fn as_str(&self) -> &'static str {
27        match self {
28            Self::Starting => "starting",
29            Self::Idle => "idle",
30            Self::Running => "running",
31            Self::Paused => "paused",
32            Self::Syncing => "syncing",
33            Self::Error => "error",
34            Self::Stopping => "stopping",
35        }
36    }
37
38    pub fn parse(s: &str) -> Option<Self> {
39        Self::ALL.iter().find(|v| v.as_str() == s).copied()
40    }
41}
42
43impl std::fmt::Display for ServiceStatus {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        f.write_str(self.as_str())
46    }
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
50#[serde(rename_all = "snake_case")]
51pub enum CommandStatus {
52    Pending,
53    Acknowledged,
54    Completed,
55    Failed,
56    Expired,
57}
58
59impl CommandStatus {
60    pub const fn as_str(&self) -> &'static str {
61        match self {
62            Self::Pending => "pending",
63            Self::Acknowledged => "acknowledged",
64            Self::Completed => "completed",
65            Self::Failed => "failed",
66            Self::Expired => "expired",
67        }
68    }
69}
70
71impl std::fmt::Display for CommandStatus {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        f.write_str(self.as_str())
74    }
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
78#[serde(rename_all = "snake_case")]
79pub enum SyncEventType {
80    Scheduled,
81    Manual,
82    Command,
83    Triggered,
84    FullSync,
85}
86
87impl SyncEventType {
88    pub const ALL: &[SyncEventType] = &[
89        Self::Scheduled,
90        Self::Manual,
91        Self::Command,
92        Self::Triggered,
93        Self::FullSync,
94    ];
95
96    pub fn as_str(&self) -> &'static str {
97        match self {
98            Self::Scheduled => "scheduled",
99            Self::Manual => "manual",
100            Self::Command => "command",
101            Self::Triggered => "triggered",
102            Self::FullSync => "full_sync",
103        }
104    }
105
106    pub fn parse(s: &str) -> Option<Self> {
107        Self::ALL.iter().find(|v| v.as_str() == s).copied()
108    }
109}
110
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
112#[serde(rename_all = "snake_case")]
113pub enum SyncEventStatus {
114    Running,
115    Completed,
116    Partial,
117    Failed,
118    Cancelled,
119}
120
121impl SyncEventStatus {
122    pub const ALL: &[SyncEventStatus] = &[
123        Self::Running,
124        Self::Completed,
125        Self::Partial,
126        Self::Failed,
127        Self::Cancelled,
128    ];
129
130    pub fn as_str(&self) -> &'static str {
131        match self {
132            Self::Running => "running",
133            Self::Completed => "completed",
134            Self::Partial => "partial",
135            Self::Failed => "failed",
136            Self::Cancelled => "cancelled",
137        }
138    }
139
140    pub fn parse(s: &str) -> Option<Self> {
141        Self::ALL.iter().find(|v| v.as_str() == s).copied()
142    }
143
144    pub fn is_terminal(&self) -> bool {
145        matches!(
146            self,
147            Self::Completed | Self::Partial | Self::Failed | Self::Cancelled
148        )
149    }
150
151    pub fn is_success(&self) -> bool {
152        matches!(self, Self::Completed | Self::Partial)
153    }
154}
155
156impl std::str::FromStr for ServiceStatus {
157    type Err = super::UnknownValue;
158
159    fn from_str(s: &str) -> Result<Self, Self::Err> {
160        Self::parse(s)
161            .ok_or_else(|| super::UnknownValue::new(s, Self::ALL.iter().map(Self::as_str)))
162    }
163}
164
165impl std::str::FromStr for SyncEventType {
166    type Err = super::UnknownValue;
167
168    fn from_str(s: &str) -> Result<Self, Self::Err> {
169        Self::parse(s)
170            .ok_or_else(|| super::UnknownValue::new(s, Self::ALL.iter().map(Self::as_str)))
171    }
172}
173
174impl std::str::FromStr for SyncEventStatus {
175    type Err = super::UnknownValue;
176
177    fn from_str(s: &str) -> Result<Self, Self::Err> {
178        Self::parse(s)
179            .ok_or_else(|| super::UnknownValue::new(s, Self::ALL.iter().map(Self::as_str)))
180    }
181}