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
53
54
55
mod r#continue;
mod flags;
mod scope;
mod start;
pub use self::{flags::*, r#continue::*, scope::*, start::*};
use std::io::Read;
#[derive(Debug, thiserror::Error)]
pub enum StreamError {
#[error("{0}")]
LengthOverflow(String),
#[error("can't find free outgoing packet id")]
PacketIdLimit,
#[error(transparent)]
Flags(#[from] FlagsError),
#[error(transparent)]
StartPacket(#[from] StartPacketError),
#[error(transparent)]
ContinuePacket(#[from] ContinuePacketError),
}
type Result<T> = std::result::Result<T, StreamError>;
#[derive(Debug, PartialEq, Eq)]
pub enum Packet {
Start(StartPacket),
Continue(ContinuePacket),
}
impl Packet {
pub const DATA_LENGTH: usize = 64;
pub fn encode(&self) -> Result<Vec<u8>> {
match self {
Self::Start(packet) => Ok(packet.encode()?),
Self::Continue(packet) => Ok(packet.encode()?),
}
}
pub fn decode(data_stream: &mut impl Read) -> Result<Self> {
let flags = Flags::decode(data_stream)?;
let packet = if flags.is_packet_start {
Packet::Start(StartPacket::decode(flags.stream_id, data_stream)?)
} else {
Packet::Continue(ContinuePacket::decode(flags.stream_id, data_stream)?)
};
Ok(packet)
}
}