Add meta section len() and to_bytes()

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-02-25 17:56:16 -05:00
parent 86d46b2a50
commit d11dbbcdf5

View File

@@ -205,11 +205,21 @@ pub struct MetaSection {
impl MetaSection { impl MetaSection {
pub fn len(&self) -> usize { 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> { 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> { pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
@@ -246,6 +256,9 @@ mod test {
len: 16, len: 16,
contents: vec!(0u8; 16), contents: vec!(0u8; 16),
}), }),
Section::Meta(MetaSection {
entries: Default::default(),
})
], ],
}; };