use super::*;
use nom::number::complete::le_u8;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FileControl {
transfer_direction: TransferDirection,
file_id: u8,
control_type: ControlType,
}
impl FromBytes for FileControl {
named!(from_bytes<FileControl>, do_parse!(
tag!("\x51") >>
transfer_direction: call!(TransferDirection::from_bytes) >>
file_id: le_u8 >>
control_type: call!(ControlType::from_bytes) >>
(FileControl {
transfer_direction,
file_id,
control_type,
})
));
}
impl ToBytes for FileControl {
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
do_gen!(buf,
gen_be_u8!(0x51) >>
gen_be_u8!(self.transfer_direction as u8) >>
gen_be_u8!(self.file_id) >>
gen_call!(|buf, control_type| ControlType::to_bytes(control_type, buf), &self.control_type)
)}
}
impl FileControl {
pub fn new(transfer_direction: TransferDirection, file_id: u8, control_type: ControlType) -> Self {
FileControl {
transfer_direction,
file_id,
control_type,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
encode_decode_test!(
tox_crypto::crypto_init().unwrap(),
file_control_encode_decode,
FileControl::new(TransferDirection::Send, 1, ControlType::Seek(100))
);
}