hebo_codec/
binary_data.rs1use byteorder::{BigEndian, WriteBytesExt};
6use std::io::Write;
7
8use crate::{utils, ByteArray, DecodeError, DecodePacket, EncodeError, EncodePacket};
9
10#[derive(Clone, Debug, Default, PartialEq, Eq)]
24pub struct BinaryData(Vec<u8>);
25
26impl BinaryData {
27 #[must_use]
29 pub const fn new() -> Self {
30 Self(Vec::new())
31 }
32
33 pub fn from_slice(data: &[u8]) -> Result<Self, EncodeError> {
39 utils::validate_two_bytes_data(data)?;
40 Ok(Self(data.to_vec()))
41 }
42
43 #[must_use]
45 pub fn bytes(&self) -> usize {
46 2 + self.0.len()
47 }
48
49 pub fn clear(&mut self) {
51 self.0.clear();
52 }
53}
54
55impl AsRef<[u8]> for BinaryData {
56 fn as_ref(&self) -> &[u8] {
57 &self.0
58 }
59}
60
61impl AsMut<Vec<u8>> for BinaryData {
62 fn as_mut(&mut self) -> &mut Vec<u8> {
63 &mut self.0
64 }
65}
66
67impl DecodePacket for BinaryData {
68 fn decode(ba: &mut ByteArray) -> Result<Self, DecodeError> {
69 let len = ba.read_u16()?;
70 let data = ba.read_bytes(len as usize)?;
71 Ok(Self(data.to_vec()))
72 }
73}
74
75impl EncodePacket for BinaryData {
76 fn encode(&self, buf: &mut Vec<u8>) -> Result<usize, EncodeError> {
77 #[allow(clippy::cast_possible_truncation)]
78 let len = self.0.len() as u16;
79 buf.write_u16::<BigEndian>(len)?;
80 buf.write_all(&self.0)?;
81 Ok(self.bytes())
82 }
83}