1mod transition;
5
6use std::fmt;
7
8use serde::{Deserialize, Serialize};
9
10pub use transition::{Cause, Desired, IllegalTransition, Transition};
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, schemars::JsonSchema)]
14#[serde(rename_all = "snake_case")]
15#[non_exhaustive]
16pub enum Op {
17 Up,
19 Down,
21 Park,
23 Resume,
25 Check,
27 Provision,
29 Attach,
31 Detach,
33 Update,
35 Retire,
37 Package,
39}
40
41impl Op {
42 pub(crate) const ALL: [Op; 11] = [
44 Op::Up,
45 Op::Down,
46 Op::Park,
47 Op::Resume,
48 Op::Check,
49 Op::Provision,
50 Op::Attach,
51 Op::Detach,
52 Op::Update,
53 Op::Retire,
54 Op::Package,
55 ];
56
57 #[must_use]
59 pub fn as_str(self) -> &'static str {
60 match self {
61 Op::Up => "up",
62 Op::Down => "down",
63 Op::Park => "park",
64 Op::Resume => "resume",
65 Op::Check => "check",
66 Op::Provision => "provision",
67 Op::Attach => "attach",
68 Op::Detach => "detach",
69 Op::Update => "update",
70 Op::Retire => "retire",
71 Op::Package => "package",
72 }
73 }
74
75 pub(crate) fn during(self) -> Option<RunState> {
77 match self {
78 Op::Up => Some(RunState::Creating),
79 Op::Down => Some(RunState::Destroying),
80 Op::Park => Some(RunState::Parking),
81 Op::Resume => Some(RunState::Resuming),
82 Op::Update => Some(RunState::Updating),
83 Op::Check | Op::Provision | Op::Attach | Op::Detach | Op::Retire | Op::Package => None,
86 }
87 }
88}
89
90impl fmt::Display for Op {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 f.write_str(self.as_str())
93 }
94}
95
96#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, schemars::JsonSchema)]
98#[serde(rename_all = "snake_case")]
99#[non_exhaustive]
100pub enum RunState {
101 Creating,
103 Ready,
105 Failed,
107 Parking,
109 Parked,
111 Resuming,
113 Updating,
115 Destroying,
117 Absent,
119}
120
121impl RunState {
122 pub(crate) fn is_moving(self) -> bool {
125 self.moved_by().is_some()
126 }
127
128 pub(crate) fn moved_by(self) -> Option<Op> {
131 Op::ALL.into_iter().find(|op| op.during() == Some(self))
132 }
133
134 #[must_use]
136 pub fn as_str(self) -> &'static str {
137 match self {
138 RunState::Creating => "creating",
139 RunState::Ready => "ready",
140 RunState::Failed => "failed",
141 RunState::Parking => "parking",
142 RunState::Parked => "parked",
143 RunState::Resuming => "resuming",
144 RunState::Updating => "updating",
145 RunState::Destroying => "destroying",
146 RunState::Absent => "absent",
147 }
148 }
149}
150
151impl fmt::Display for RunState {
152 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153 f.write_str(self.as_str())
154 }
155}
156
157#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
159#[serde(rename_all = "snake_case")]
160#[non_exhaustive]
161pub enum Park {
162 Pause,
164 Shutdown,
166}
167
168impl Park {
169 #[must_use]
171 pub fn as_str(self) -> &'static str {
172 match self {
173 Park::Pause => "pause",
174 Park::Shutdown => "shutdown",
175 }
176 }
177}
178
179impl fmt::Display for Park {
180 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181 f.write_str(self.as_str())
182 }
183}
184
185#[cfg(test)]
186mod tests {
187 use super::*;
188
189 #[test]
190 fn every_op_state_and_park_mode_is_written_as_serde_writes_it() {
191 crate::testing::assert_named_as_serde(Op::as_str);
192 crate::testing::assert_named_as_serde(RunState::as_str);
193 crate::testing::assert_named_as_serde(Park::as_str);
194 }
195
196 #[test]
197 fn every_op_is_listed_and_the_moving_states_are_those_an_op_holds() {
198 assert_eq!(crate::testing::every_variant::<Op>(), Op::ALL);
199 let moving: Vec<RunState> = crate::testing::every_variant::<RunState>().into_iter().filter(|s| s.is_moving()).collect();
200 assert_eq!(
201 moving,
202 [
203 RunState::Creating,
204 RunState::Parking,
205 RunState::Resuming,
206 RunState::Updating,
207 RunState::Destroying
208 ]
209 );
210 }
211}