use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UtilityVmRole {
Native,
Rosetta,
}
impl UtilityVmRole {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Native => "native",
Self::Rosetta => "rosetta",
}
}
#[must_use]
pub const fn all() -> [Self; 2] {
[Self::Native, Self::Rosetta]
}
#[must_use]
pub fn from_str_ascii(s: &str) -> Option<Self> {
match s {
"native" => Some(Self::Native),
"rosetta" => Some(Self::Rosetta),
_ => None,
}
}
}
impl fmt::Display for UtilityVmRole {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trips_through_string() {
for role in UtilityVmRole::all() {
assert_eq!(UtilityVmRole::from_str_ascii(role.as_str()), Some(role));
}
}
#[test]
fn from_str_rejects_unknown() {
assert!(UtilityVmRole::from_str_ascii("kvm").is_none());
assert!(UtilityVmRole::from_str_ascii("").is_none());
}
#[test]
fn all_lists_every_variant_once() {
let all = UtilityVmRole::all();
assert_eq!(all.len(), 2);
assert!(all.contains(&UtilityVmRole::Native));
assert!(all.contains(&UtilityVmRole::Rosetta));
}
}