use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Mode {
Sw,
Nw,
Hw,
Ov,
}
impl Mode {
#[must_use]
pub const fn is_local(self) -> bool {
matches!(self, Mode::Sw)
}
#[must_use]
pub const fn code(self) -> &'static str {
match self {
Mode::Sw => "SW",
Mode::Nw => "NW",
Mode::Hw => "HW",
Mode::Ov => "OV",
}
}
}
impl fmt::Display for Mode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.code())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_sw_is_local() {
assert!(Mode::Sw.is_local());
for m in [Mode::Nw, Mode::Hw, Mode::Ov] {
assert!(!m.is_local(), "{m} should not be local");
}
}
#[test]
fn code_and_display_agree_for_every_mode() {
for (m, code) in [
(Mode::Sw, "SW"),
(Mode::Nw, "NW"),
(Mode::Hw, "HW"),
(Mode::Ov, "OV"),
] {
assert_eq!(m.code(), code);
assert_eq!(m.to_string(), code);
}
}
#[test]
fn modes_are_distinct() {
let all = [Mode::Sw, Mode::Nw, Mode::Hw, Mode::Ov];
for (i, a) in all.iter().enumerate() {
for (j, b) in all.iter().enumerate() {
assert_eq!(i == j, a == b, "equality mismatch for {a} vs {b}");
}
}
}
}