1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use crate::{
    codec::{Codec, StdError, WithOffset, WithSize},
    spec::v1_2::action::OpCode,
};

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Chunk {
    Continue = 0,
    Start = 1,
    End = 2,
    StartEnd = 3,
}
impl std::fmt::Display for Chunk {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match &self {
            Chunk::Continue => write!(f, "[C]"),
            Chunk::Start => write!(f, "[S]"),
            Chunk::End => write!(f, "[E]"),
            Chunk::StartEnd => write!(f, "[R]"),
        }
    }
}
impl Chunk {
    fn from(n: u8) -> Self {
        match n {
            0 => Chunk::Continue,
            1 => Chunk::Start,
            2 => Chunk::End,
            3 => Chunk::StartEnd,
            x => panic!("Impossible chunk step {}", x),
        }
    }
}
impl Codec for Chunk {
    type Error = StdError;
    fn encoded_size(&self) -> usize {
        1
    }
    unsafe fn encode_in(&self, out: &mut [u8]) -> usize {
        out[0] = OpCode::Chunk as u8 + ((*self as u8) << 6);
        1
    }
    fn decode(out: &[u8]) -> Result<WithSize<Self>, WithOffset<Self::Error>> {
        if out.is_empty() {
            return Err(WithOffset::new_head(Self::Error::MissingBytes(1)));
        }
        Ok(WithSize {
            value: Self::from(out[0] >> 6),
            size: 1,
        })
    }
}