use sealed::sealed;
use serde::Serialize;
use crate::pishock::serial::payload::{__seal_serial_command, SerialCommand};
macro_rules! test_command {
($name:ident, $cmd:expr, $expected:expr) => {
#[cfg(test)]
#[test]
fn $name() -> Result<(), std::io::Error> {
let mut payload = Vec::new();
crate::pishock::serial::payload::serialize(&mut payload, $cmd)?;
assert_eq!(std::str::from_utf8(&payload).expect("bad utf8 string"), concat!($expected, "\n"));
return Ok(());
}
};
}
#[derive(Debug, Serialize)]
pub struct Connect<'a> {
pub ssid: &'a str,
pub password: &'a str,
}
#[sealed]
impl SerialCommand for Connect<'_> {
const ID: &'static str = "connect";
}
test_command! {
serial_cmd_connect_serialize,
Connect {
ssid: "test_ssid",
password: "test_password",
},
r#"{"cmd":"connect","value":{"ssid":"test_ssid","password":"test_password"}}"#
}
#[derive(Debug, Serialize)]
pub struct AddNetwork<'a> {
pub ssid: &'a str,
pub password: &'a str,
}
#[sealed]
impl SerialCommand for AddNetwork<'_> {
const ID: &'static str = "addnetwork";
}
test_command! {
serial_cmd_addnetwork_serialize,
AddNetwork {
ssid: "test_ssid",
password: "test_password",
},
r#"{"cmd":"addnetwork","value":{"ssid":"test_ssid","password":"test_password"}}"#
}
#[derive(Debug, Serialize)]
#[serde(transparent)]
pub struct RemoveNetwork<'a> {
pub ssid: &'a str,
}
#[sealed]
impl SerialCommand for RemoveNetwork<'_> {
const ID: &'static str = "removenetwork";
}
test_command! {
serial_cmd_removenetwork_serialize,
RemoveNetwork {
ssid: "test_ssid",
},
r#"{"cmd":"removenetwork","value":"test_ssid"}"#
}
#[derive(Debug, Serialize)]
pub struct Restart;
#[sealed]
impl SerialCommand for Restart {
const ID: &'static str = "restart";
}
test_command! {
serial_cmd_restart_serialize,
Restart,
r#"{"cmd":"restart"}"#
}
#[derive(Debug, Serialize)]
pub struct Info;
#[sealed]
impl SerialCommand for Info {
const ID: &'static str = "info";
}
test_command! {
serial_cmd_info_serialize,
Info,
r#"{"cmd":"info"}"#
}
#[derive(Debug, Serialize)]
pub struct Operate {
#[serde(rename = "id")]
pub shocker_id: u32,
#[serde(rename = "op")]
pub operation: ShockerOperation,
pub intensity: u8,
pub duration: u32,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ShockerOperation {
Shock,
Vibrate,
Beep,
End,
}
#[sealed]
impl SerialCommand for Operate {
const ID: &'static str = "operate";
}
test_command! {
serial_cmd_operate_serialize,
Operate {
shocker_id: 1,
operation: ShockerOperation::Shock,
duration: 100,
intensity: 10,
},
r#"{"cmd":"operate","value":{"id":1,"op":"shock","intensity":10,"duration":100}}"#
}