Add meta section len() and to_bytes()
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -205,11 +205,21 @@ pub struct MetaSection {
|
||||
|
||||
impl MetaSection {
|
||||
pub fn len(&self) -> usize {
|
||||
todo!() // 8 + ...
|
||||
8 + self.entries.iter()
|
||||
.map(|(k, _)| 8 + k.as_bytes().len() + 8)
|
||||
.sum::<usize>()
|
||||
}
|
||||
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
todo!()
|
||||
let mut cursor = Cursor::new(Vec::new());
|
||||
cursor.write_u64::<LE>(self.entries.len() as u64).unwrap();
|
||||
for (k, v) in self.entries.iter() {
|
||||
let key_len = k.as_bytes().len();
|
||||
cursor.write_u64::<LE>(key_len as u64).unwrap();
|
||||
cursor.write(k.as_bytes()).unwrap();
|
||||
cursor.write_u64::<LE>(*v).unwrap();
|
||||
}
|
||||
cursor.into_inner()
|
||||
}
|
||||
|
||||
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
|
||||
@@ -246,6 +256,9 @@ mod test {
|
||||
len: 16,
|
||||
contents: vec!(0u8; 16),
|
||||
}),
|
||||
Section::Meta(MetaSection {
|
||||
entries: Default::default(),
|
||||
})
|
||||
],
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user