use toxcore::binary_io::*;
use nom::{be_u8, rest};
#[derive(Debug, PartialEq, Clone)]
pub struct Data {
pub connection_id: u8,
pub data: Vec<u8>
}
impl FromBytes for Data {
named!(from_bytes<Data>, do_parse!(
connection_id: be_u8 >>
verify!(value!(connection_id), |id| id >= 0x10 && id < 0xF0) >>
data: rest >>
(Data { connection_id, data: data.to_vec() })
));
}
impl ToBytes for Data {
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
do_gen!(buf,
gen_be_u8!(self.connection_id) >>
gen_slice!(self.data)
)
}
}
#[cfg(test)]
mod test {
use super::*;
encode_decode_test!(
data_encode_decode,
Data {
connection_id: 17,
data: vec![42; 123]
}
);
}