use core::fmt;
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
pub enum WireVmState {
#[default]
NotStarted,
Running,
Paused,
}
impl fmt::Display for WireVmState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotStarted => f.write_str("Not started"),
Self::Running => f.write_str("Running"),
Self::Paused => f.write_str("Paused"),
}
}
}
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
pub enum LifecyclePhase {
#[default]
Uninitialized,
NotStarted,
Starting,
Running,
Paused,
Shutdown,
}
impl LifecyclePhase {
#[must_use]
pub const fn wire_state(self) -> WireVmState {
match self {
Self::Uninitialized | Self::NotStarted | Self::Starting | Self::Shutdown => {
WireVmState::NotStarted
}
Self::Running => WireVmState::Running,
Self::Paused => WireVmState::Paused,
}
}
#[must_use]
pub const fn is_post_boot(self) -> bool {
matches!(self, Self::Running | Self::Paused)
}
#[must_use]
pub const fn is_pre_boot(self) -> bool {
matches!(self, Self::Uninitialized | Self::NotStarted)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wire_state_collapses_per_spec_11_3_1() {
assert_eq!(
LifecyclePhase::Uninitialized.wire_state(),
WireVmState::NotStarted
);
assert_eq!(
LifecyclePhase::NotStarted.wire_state(),
WireVmState::NotStarted
);
assert_eq!(
LifecyclePhase::Starting.wire_state(),
WireVmState::NotStarted
);
assert_eq!(
LifecyclePhase::Shutdown.wire_state(),
WireVmState::NotStarted
);
assert_eq!(LifecyclePhase::Running.wire_state(), WireVmState::Running);
assert_eq!(LifecyclePhase::Paused.wire_state(), WireVmState::Paused);
}
#[test]
fn wire_state_displays_upstream_strings_verbatim() {
assert_eq!(WireVmState::NotStarted.to_string(), "Not started");
assert_eq!(WireVmState::Running.to_string(), "Running");
assert_eq!(WireVmState::Paused.to_string(), "Paused");
}
#[test]
fn pre_post_boot_predicates() {
assert!(LifecyclePhase::Uninitialized.is_pre_boot());
assert!(LifecyclePhase::NotStarted.is_pre_boot());
assert!(!LifecyclePhase::Starting.is_pre_boot());
assert!(LifecyclePhase::Running.is_post_boot());
assert!(LifecyclePhase::Paused.is_post_boot());
assert!(!LifecyclePhase::Shutdown.is_post_boot());
}
}