use super::{Display, ParseError};
use crate::frame::Frame;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(PartialEq, Debug, Display, Clone)]
pub enum Wake {
HoodOpen,
HoodClose,
Unplug,
Plug,
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(PartialEq, Debug, Display, Clone)]
#[repr(align(8))]
pub enum Bus {
Wake(Wake),
}
impl TryFrom<Frame> for Bus {
type Error = ParseError;
fn try_from(frame: Frame) -> Result<Self, Self::Error> {
const ID: u32 = 0x401;
const LEN: usize = 8;
if frame.id() != ID {
return Err(ParseError::Id { frame });
}
let data: [u8; LEN] = match frame.data().try_into() {
Ok(data) => data,
Err(_) => {
return Err(ParseError::Len {
frame,
expected: LEN,
})
}
};
match data[4..6] {
[0x01, 0x03] => Ok(Bus::Wake(Wake::Plug)),
[0x01, 0x04] => Ok(Bus::Wake(Wake::Unplug)),
[0x0c, 0x06] => Ok(Bus::Wake(Wake::HoodOpen)),
[0x0c, 0x07] => Ok(Bus::Wake(Wake::HoodClose)),
_ => Err(ParseError::Data {
frame: frame.clone(),
detail: format!(
"Unrecognized {} data in frame: {}",
stringify!(Bus),
&frame
),
}),
}
}
}