use super::{Display, ParseError};
use crate::frame::Frame;
#[derive(PartialEq, Debug, Display, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(align(8))]
pub enum Camera {
Off = 0x00,
Initializing = 0x02,
Reverse = 0x07,
Cargo = 0x09,
}
impl TryFrom<Frame> for Camera {
type Error = ParseError;
fn try_from(frame: Frame) -> Result<Self, Self::Error> {
const ID: u32 = 0x302;
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[0] {
0x00 => Ok(Camera::Off),
0x02 => Ok(Camera::Reverse),
0x07 => Ok(Camera::Cargo),
0x09 => Ok(Camera::Initializing),
_ => Err(ParseError::Data {
frame: frame,
detail: format!(
"Unrecognize {} byte at index 0: {}",
stringify!(Camera),
data[0]
),
}),
}
}
}