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, Self::Idle, Self::Running, Self::Paused,
18        Self::Syncing, Self::Error, Self::Stopping,
19    ];
20
21    pub fn as_str(&self) -> &'static str {
22        match self {
23            Self::Starting => "starting",
24            Self::Idle => "idle",
25            Self::Running => "running",
26            Self::Paused => "paused",
27            Self::Syncing => "syncing",
28            Self::Error => "error",
29            Self::Stopping => "stopping",
30        }
31    }
32
33    pub fn from_str(s: &str) -> Option<Self> {
34        Self::ALL.iter().find(|v| v.as_str() == s).copied()
35    }
36}
37
38impl std::fmt::Display for ServiceStatus {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        f.write_str(self.as_str())
41    }
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
45#[serde(rename_all = "snake_case")]
46pub enum CommandStatus {
47    Pending,
48    Acknowledged,
49    Completed,
50    Failed,
51    Expired,
52}
53
54impl CommandStatus {
55    pub const fn as_str(&self) -> &'static str {
56        match self {
57            Self::Pending => "pending",
58            Self::Acknowledged => "acknowledged",
59            Self::Completed => "completed",
60            Self::Failed => "failed",
61            Self::Expired => "expired",
62        }
63    }
64}
65
66impl std::fmt::Display for CommandStatus {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        f.write_str(self.as_str())
69    }
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
73#[serde(rename_all = "snake_case")]
74pub enum SyncEventType {
75    Scheduled,
76    Manual,
77    Command,
78    Triggered,
79    FullSync,
80}
81
82impl SyncEventType {
83    pub const ALL: &[SyncEventType] = &[
84        Self::Scheduled, Self::Manual, Self::Command, Self::Triggered, Self::FullSync,
85    ];
86
87    pub fn as_str(&self) -> &'static str {
88        match self {
89            Self::Scheduled => "scheduled",
90            Self::Manual => "manual",
91            Self::Command => "command",
92            Self::Triggered => "triggered",
93            Self::FullSync => "full_sync",
94        }
95    }
96
97    pub fn from_str(s: &str) -> Option<Self> {
98        Self::ALL.iter().find(|v| v.as_str() == s).copied()
99    }
100}
101
102#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
103#[serde(rename_all = "snake_case")]
104pub enum SyncEventStatus {
105    Running,
106    Completed,
107    Partial,
108    Failed,
109    Cancelled,
110}
111
112impl SyncEventStatus {
113    pub const ALL: &[SyncEventStatus] = &[
114        Self::Running, Self::Completed, Self::Partial, Self::Failed, Self::Cancelled,
115    ];
116
117    pub fn as_str(&self) -> &'static str {
118        match self {
119            Self::Running => "running",
120            Self::Completed => "completed",
121            Self::Partial => "partial",
122            Self::Failed => "failed",
123            Self::Cancelled => "cancelled",
124        }
125    }
126
127    pub fn from_str(s: &str) -> Option<Self> {
128        Self::ALL.iter().find(|v| v.as_str() == s).copied()
129    }
130
131    pub fn is_terminal(&self) -> bool {
132        matches!(self, Self::Completed | Self::Partial | Self::Failed | Self::Cancelled)
133    }
134
135    pub fn is_success(&self) -> bool {
136        matches!(self, Self::Completed | Self::Partial)
137    }
138}