ham_cats/whisker/
comment.rs

1use arrayvec::ArrayVec;
2
3#[derive(Debug)]
4pub struct Comment(ArrayVec<u8, 255>);
5
6impl Comment {
7    pub fn new(data: &[u8]) -> Option<Self> {
8        let mut av = ArrayVec::new();
9        av.try_extend_from_slice(data).ok()?;
10
11        Some(Self(av))
12    }
13
14    pub fn internal_data(&self) -> &ArrayVec<u8, 255> {
15        &self.0
16    }
17
18    pub fn encode<'a>(&self, buf: &'a mut [u8]) -> Option<&'a [u8]> {
19        let len = self.0.len();
20
21        // length must be <= 255, so this is safe
22        *buf.get_mut(0)? = len.try_into().unwrap();
23        buf.get_mut(1..(len + 1))?.copy_from_slice(&self.0);
24
25        Some(&buf[0..(len + 1)])
26    }
27
28    pub fn decode(data: &[u8]) -> Option<Self> {
29        let len: usize = (*data.first()?).into();
30        let content = data.get(1..(len + 1))?;
31
32        Self::new(content)
33    }
34}