Skip to main content

rskit_component/
state.rs

1use std::fmt;
2
3/// Lifecycle state tracked for each registered component.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum State {
7    /// The component has been registered but not started.
8    Created,
9    /// The component is currently starting.
10    Starting,
11    /// The component is running.
12    Running,
13    /// The component is currently stopping.
14    Stopping,
15    /// The component has stopped cleanly.
16    Stopped,
17    /// The component failed to start or stop cleanly.
18    Failed,
19}
20
21impl fmt::Display for State {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            Self::Created => f.write_str("created"),
25            Self::Starting => f.write_str("starting"),
26            Self::Running => f.write_str("running"),
27            Self::Stopping => f.write_str("stopping"),
28            Self::Stopped => f.write_str("stopped"),
29            Self::Failed => f.write_str("failed"),
30        }
31    }
32}
33
34impl State {
35    pub(crate) const fn can_start(self) -> bool {
36        matches!(self, Self::Created | Self::Stopped | Self::Failed)
37    }
38
39    pub(crate) const fn should_stop(self) -> bool {
40        matches!(self, Self::Running)
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::State;
47
48    #[test]
49    fn display_matches_expected_values() {
50        assert_eq!(State::Created.to_string(), "created");
51        assert_eq!(State::Starting.to_string(), "starting");
52        assert_eq!(State::Running.to_string(), "running");
53        assert_eq!(State::Stopping.to_string(), "stopping");
54        assert_eq!(State::Stopped.to_string(), "stopped");
55        assert_eq!(State::Failed.to_string(), "failed");
56    }
57}