use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub enum AutomationState {
#[default]
Off,
Play,
Write,
Touch,
Latch,
}
impl AutomationState {
#[must_use]
#[inline]
pub fn reads_automation(&self) -> bool {
matches!(self, Self::Play | Self::Touch | Self::Latch)
}
#[must_use]
#[inline]
pub fn can_record(&self) -> bool {
matches!(self, Self::Write | Self::Touch | Self::Latch)
}
#[must_use]
#[inline]
pub fn starts_on_touch(&self) -> bool {
matches!(self, Self::Touch | Self::Latch)
}
#[must_use]
#[inline]
pub fn stops_on_release(&self) -> bool {
matches!(self, Self::Touch)
}
#[must_use]
pub fn all() -> &'static [AutomationState] {
&[Self::Off, Self::Play, Self::Write, Self::Touch, Self::Latch]
}
#[must_use]
pub fn display_name(&self) -> &'static str {
match self {
Self::Off => "Off",
Self::Play => "Play",
Self::Write => "Write",
Self::Touch => "Touch",
Self::Latch => "Latch",
}
}
#[must_use]
pub fn abbreviation(&self) -> &'static str {
match self {
Self::Off => "OFF",
Self::Play => "PLY",
Self::Write => "WRT",
Self::Touch => "TCH",
Self::Latch => "LCH",
}
}
}
impl core::fmt::Display for AutomationState {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.display_name())
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::format;
#[test]
fn test_automation_state_properties() {
assert!(!AutomationState::Off.reads_automation());
assert!(!AutomationState::Off.can_record());
assert!(AutomationState::Play.reads_automation());
assert!(!AutomationState::Play.can_record());
assert!(!AutomationState::Write.reads_automation());
assert!(AutomationState::Write.can_record());
assert!(AutomationState::Touch.reads_automation());
assert!(AutomationState::Touch.can_record());
assert!(AutomationState::Touch.starts_on_touch());
assert!(AutomationState::Touch.stops_on_release());
assert!(AutomationState::Latch.reads_automation());
assert!(AutomationState::Latch.can_record());
assert!(AutomationState::Latch.starts_on_touch());
assert!(!AutomationState::Latch.stops_on_release());
}
#[test]
fn test_all_states() {
assert_eq!(AutomationState::all().len(), 5);
}
#[test]
fn test_display() {
assert_eq!(AutomationState::Touch.display_name(), "Touch");
assert_eq!(AutomationState::Touch.abbreviation(), "TCH");
assert_eq!(format!("{}", AutomationState::Touch), "Touch");
}
#[test]
fn test_default() {
assert_eq!(AutomationState::default(), AutomationState::Off);
}
}