uhppote_rs/messages/utils/
request.rs

1use std::fmt::Debug;
2
3use anyhow::Result;
4pub trait Request {
5    fn to_bytes(&self) -> [u8; 64];
6    fn to_bytes_impl(&self) -> [u8; 64]
7    where
8        Self: std::marker::Sized,
9        Self: bincode::Encode,
10    {
11        let options = bincode::config::standard()
12            .with_fixed_int_encoding()
13            .with_little_endian();
14
15        let mut result = [0u8; 64];
16        bincode::encode_into_slice(self, &mut result, options).unwrap();
17
18        result
19    }
20    fn get_id(&self) -> u32;
21}
22
23pub trait Response {
24    fn from_bytes(bytes: &[u8; 64]) -> Result<Self>
25    where
26        Self: std::marker::Sized,
27        Self: Debug;
28    fn from_bytes_impl(bytes: &[u8; 64]) -> Result<Self>
29    where
30        Self: std::marker::Sized,
31        Self: bincode::Decode,
32        Self: Debug,
33    {
34        let options = bincode::config::standard()
35            .with_fixed_int_encoding()
36            .with_little_endian();
37
38        match bincode::decode_from_slice(bytes, options) {
39            Ok((res, _)) => Ok(res),
40            Err(e) => Err(anyhow::Error::from(e)),
41        }
42    }
43}