use super::{Display, Frame, ParseError};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(PartialEq, Debug, Display, Clone)]
#[repr(align(8))]
pub enum Ignition {
Off,
Kill,
Acc,
Run,
StartReceived,
Cranking,
}
impl TryFrom<Frame> for Ignition {
type Error = ParseError;
fn try_from(frame: Frame) -> Result<Self, Self::Error> {
const ID: u32 = 0x122;
const LEN: usize = 4;
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 u32::from_be_bytes(data) {
0x00000000 => Ok(Ignition::Off), 0x00010000 => Ok(Ignition::Off), 0x03010000 => Ok(Ignition::Kill), 0x03020000 => Ok(Ignition::Kill), 0x05020000 => Ok(Ignition::Acc), 0x15020000 => Ok(Ignition::Acc), 0x44010000 => Ok(Ignition::Run), 0x44020000 => Ok(Ignition::Off), 0x45010000 => Ok(Ignition::Off), 0x5d010000 => Ok(Ignition::Off), _ => Err(ParseError::Data {
frame,
detail: format!(
"Unrecognized `Ignition` value: {:X}",
u32::from_be_bytes(data)
),
}),
}
}
}