From 69da09ecc8e61a957791d9b03266d371fddb9e4c Mon Sep 17 00:00:00 2001 From: Trivernis Date: Thu, 12 Mar 2020 11:31:01 +0100 Subject: [PATCH] Add serialization of HashEntries --- src/lib/rainbowutils.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lib/rainbowutils.rs b/src/lib/rainbowutils.rs index d3df94e..3c64eb3 100644 --- a/src/lib/rainbowutils.rs +++ b/src/lib/rainbowutils.rs @@ -35,7 +35,7 @@ pub struct MetaChunk { #[derive(Debug, Clone)] pub struct HashLookupTable { - entries: HashMap, + pub entries: HashMap, } #[derive(Debug, Clone)] @@ -126,6 +126,7 @@ impl BinaryDictionaryFile { } impl GenericChunk { + /// Returns the data entries of the chunk pub fn data_entries( &mut self, lookup_table: &HashLookupTable, @@ -275,3 +276,22 @@ impl TryFrom for HashLookupTable { }) } } + +impl HashEntry { + pub fn serialize(&mut self) -> Vec { + let mut serialized: Vec = Vec::new(); + let mut id_raw = [0u8; 4]; + BigEndian::write_u32(&mut id_raw, self.id); + serialized.append(&mut id_raw.to_vec()); + let mut output_length_raw = [0u8; 4]; + BigEndian::write_u32(&mut output_length_raw, self.output_length); + serialized.append(&mut output_length_raw.to_vec()); + let mut name_raw = self.name.clone().into_bytes(); + let mut name_length_raw = [0u8; 4]; + BigEndian::write_u32(&mut name_length_raw, name_raw.len() as u32); + serialized.append(&mut name_length_raw.to_vec()); + serialized.append(&mut name_raw); + + serialized + } +}