use crate::command::Command;
use crate::types::{PowerIndex, PowerState};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PowerCommand {
Get {
index: PowerIndex,
},
Set {
index: PowerIndex,
state: PowerState,
},
Toggle {
index: PowerIndex,
},
}
impl PowerCommand {
#[must_use]
pub const fn on(index: PowerIndex) -> Self {
Self::Set {
index,
state: PowerState::On,
}
}
#[must_use]
pub const fn off(index: PowerIndex) -> Self {
Self::Set {
index,
state: PowerState::Off,
}
}
#[must_use]
pub const fn toggle(index: PowerIndex) -> Self {
Self::Toggle { index }
}
#[must_use]
pub const fn query(index: PowerIndex) -> Self {
Self::Get { index }
}
}
impl Command for PowerCommand {
fn name(&self) -> String {
let index = match self {
Self::Get { index } | Self::Set { index, .. } | Self::Toggle { index } => index,
};
format!("Power{}", index.command_suffix())
}
fn payload(&self) -> Option<String> {
match self {
Self::Get { .. } => None,
Self::Set { state, .. } => Some(state.as_str().to_string()),
Self::Toggle { .. } => Some(PowerState::Toggle.as_str().to_string()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FadeCommand {
Get,
Enable,
Disable,
}
impl Command for FadeCommand {
fn name(&self) -> String {
"Fade".to_string()
}
fn payload(&self) -> Option<String> {
match self {
Self::Get => None,
Self::Enable => Some("1".to_string()),
Self::Disable => Some("0".to_string()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StartupFadeCommand {
Get,
Enable,
Disable,
}
impl Command for StartupFadeCommand {
fn name(&self) -> String {
"SetOption91".to_string()
}
fn payload(&self) -> Option<String> {
match self {
Self::Get => None,
Self::Enable => Some("1".to_string()),
Self::Disable => Some("0".to_string()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn power_command_on() {
let cmd = PowerCommand::on(PowerIndex::one());
assert_eq!(cmd.name(), "Power1");
assert_eq!(cmd.payload(), Some("ON".to_string()));
}
#[test]
fn power_command_off() {
let cmd = PowerCommand::off(PowerIndex::new(2).unwrap());
assert_eq!(cmd.name(), "Power2");
assert_eq!(cmd.payload(), Some("OFF".to_string()));
}
#[test]
fn power_command_toggle() {
let cmd = PowerCommand::toggle(PowerIndex::all());
assert_eq!(cmd.name(), "Power");
assert_eq!(cmd.payload(), Some("TOGGLE".to_string()));
}
#[test]
fn power_command_query() {
let cmd = PowerCommand::query(PowerIndex::one());
assert_eq!(cmd.name(), "Power1");
assert_eq!(cmd.payload(), None);
}
#[test]
fn fade_command() {
assert_eq!(FadeCommand::Get.payload(), None);
assert_eq!(FadeCommand::Enable.payload(), Some("1".to_string()));
assert_eq!(FadeCommand::Disable.payload(), Some("0".to_string()));
}
#[test]
fn startup_fade_command() {
assert_eq!(StartupFadeCommand::Get.name(), "SetOption91");
assert_eq!(StartupFadeCommand::Enable.payload(), Some("1".to_string()));
}
}