copernica_packets/
data.rs1use {
2 copernica_common::{constants::*, u8_to_u16 },
3 std::fmt,
4 anyhow::{anyhow, Result},
5 };
7#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
8pub struct Data([u8; FRAGMENT_SIZE]);
9impl Data {
10 pub fn new(data_in: &[u8]) -> Result<Self> {
11 if data_in.len() != FRAGMENT_SIZE {
12 return Err(anyhow!("Ensure data.len() passed into Data::new() is equal to {}", FRAGMENT_SIZE))
13 }
14 let mut data: [u8; FRAGMENT_SIZE] = [0u8; FRAGMENT_SIZE];
15 data.clone_from_slice(&data_in[..]);
16 Ok(Data(data))
17 }
18 pub fn as_bytes(&self) -> &[u8] {
19 &self.0[..]
20 }
21 pub fn data(&self) -> Result<Vec<u8>> {
22 let length = u8_to_u16([self.0[DATA_SIZE_START], self.0[DATA_SIZE_END]]);
23 let (data, _) = self.0.split_at(length as usize);
24 Ok(data.to_vec())
25 }
26}
27impl fmt::Display for Data {
28 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29 write!(f, "{:?}", self.0)
30 }
31}