use crate::error::StateTransition;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CgroupState {
Nonexistent,
Created,
Configured,
Attached,
Monitoring,
Removed,
}
impl StateTransition for CgroupState {
fn can_transition_to(&self, next: &CgroupState) -> bool {
use CgroupState::*;
matches!(
(self, next),
(Nonexistent, Created)
| (Created, Configured)
| (Configured, Attached)
| (Attached, Monitoring)
| (Monitoring, Removed)
| (Created, Removed)
| (Configured, Removed)
| (Attached, Removed)
| (Nonexistent, Nonexistent)
| (Created, Created)
| (Configured, Configured)
| (Attached, Attached)
| (Monitoring, Monitoring)
| (Removed, Removed)
)
}
fn is_terminal(&self) -> bool {
matches!(self, CgroupState::Removed)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_valid_transitions() {
assert!(CgroupState::Nonexistent.can_transition_to(&CgroupState::Created));
assert!(CgroupState::Created.can_transition_to(&CgroupState::Configured));
assert!(CgroupState::Configured.can_transition_to(&CgroupState::Attached));
assert!(CgroupState::Attached.can_transition_to(&CgroupState::Monitoring));
assert!(CgroupState::Monitoring.can_transition_to(&CgroupState::Removed));
}
#[test]
fn test_error_paths() {
assert!(CgroupState::Created.can_transition_to(&CgroupState::Removed));
assert!(CgroupState::Configured.can_transition_to(&CgroupState::Removed));
}
#[test]
fn test_invalid_transitions() {
assert!(!CgroupState::Nonexistent.can_transition_to(&CgroupState::Configured));
assert!(!CgroupState::Created.can_transition_to(&CgroupState::Attached));
assert!(!CgroupState::Configured.can_transition_to(&CgroupState::Created));
assert!(!CgroupState::Removed.can_transition_to(&CgroupState::Monitoring));
}
#[test]
fn test_terminal_state() {
assert!(!CgroupState::Nonexistent.is_terminal());
assert!(!CgroupState::Created.is_terminal());
assert!(!CgroupState::Configured.is_terminal());
assert!(!CgroupState::Attached.is_terminal());
assert!(!CgroupState::Monitoring.is_terminal());
assert!(CgroupState::Removed.is_terminal());
}
}