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
56
57
58
59
60
61
62
63
64
65
use cyfs_base::*;
#[derive(Debug, Clone)]
pub enum PieceSessionType {
Unknown,
Stream(u32),
RaptorA(u32),
RaptorB(u32),
}
impl RawEncode for PieceSessionType {
fn raw_measure(&self, _: &Option<RawEncodePurpose>) -> BuckyResult<usize> {
match self {
Self::Unknown => Ok(u8::raw_bytes().unwrap()),
_ => Ok(u8::raw_bytes().unwrap() + u32::raw_bytes().unwrap())
}
}
fn raw_encode<'a>(
&self,
buf: &'a mut [u8],
purpose: &Option<RawEncodePurpose>,
) -> BuckyResult<&'a mut [u8]> {
match self {
Self::Unknown => 0u8.raw_encode(buf, purpose),
Self::Stream(start) => {
let buf = 1u8.raw_encode(buf, purpose)?;
start.raw_encode(buf, purpose)
},
Self::RaptorA(start) => {
let buf = 2u8.raw_encode(buf, purpose)?;
start.raw_encode(buf, purpose)
},
Self::RaptorB(start) => {
let buf = 3u8.raw_encode(buf, purpose)?;
start.raw_encode(buf, purpose)
}
}
}
}
impl<'de> RawDecode<'de> for PieceSessionType {
fn raw_decode(buf: &'de [u8]) -> BuckyResult<(Self, &'de [u8])> {
let (type_code, buf) = u8::raw_decode(buf)?;
match type_code {
0u8 => Ok((Self::Unknown, buf)),
1u8 => {
let (start, buf) = u32::raw_decode(buf)?;
Ok((Self::Stream(start), buf))
},
2u8 => {
let (start, buf) = u32::raw_decode(buf)?;
Ok((Self::RaptorA(start), buf))
},
3u8 => {
let (start, buf) = u32::raw_decode(buf)?;
Ok((Self::RaptorB(start), buf))
},
_ => Err(BuckyError::new(BuckyErrorCode::InvalidData, "invalid type code"))
}
}
}