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
use super::{Codec, Error, Version};
use integer_encoding::{VarIntReader, VarIntWriter};
use multihash::Code;
use std::io::Cursor;
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Prefix {
pub version: Version,
pub codec: Codec,
pub mh_type: Code,
pub mh_len: usize,
}
impl Prefix {
pub fn new_from_bytes(data: &[u8]) -> Result<Prefix, Error> {
let mut cur = Cursor::new(data);
let raw_version = cur.read_varint()?;
let raw_codec = cur.read_varint()?;
let raw_mh_type: u64 = cur.read_varint()?;
let mh_len: usize = cur.read_varint()?;
let version = Version::from(raw_version)?;
let codec = Codec::from(raw_codec)?;
let mh_type = Code::from_u64(raw_mh_type);
Ok(Prefix {
version,
codec,
mh_type,
mh_len,
})
}
pub fn to_bytes(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(4);
res.write_varint(u64::from(self.version)).unwrap();
res.write_varint(u64::from(self.codec)).unwrap();
res.write_varint(self.mh_type.to_u64()).unwrap();
res.write_varint(self.mh_len).unwrap();
res
}
}