1 use util::Error; 2 3 use std::io::{Read, Write}; 4 5 use byteorder::{ReadBytesExt, WriteBytesExt}; 6 7 #[derive(Copy, Clone, Debug, PartialEq)] 8 pub enum CompressionMethodId { 9 Null = 0, 10 Unsupported, 11 } 12 13 impl From<u8> for CompressionMethodId { 14 fn from(val: u8) -> Self { 15 match val { 16 0 => CompressionMethodId::Null, 17 _ => CompressionMethodId::Unsupported, 18 } 19 } 20 } 21 22 #[derive(Clone, Debug, PartialEq)] 23 pub struct CompressionMethods { 24 pub ids: Vec<CompressionMethodId>, 25 } 26 27 impl CompressionMethods { 28 pub fn marshal<W: Write>(&self, writer: &mut W) -> Result<(), Error> { 29 writer.write_u8(self.ids.len() as u8)?; 30 31 for id in &self.ids { 32 writer.write_u8(*id as u8)?; 33 } 34 35 Ok(()) 36 } 37 38 pub fn unmarshal<R: Read>(reader: &mut R) -> Result<Self, Error> { 39 let compression_methods_count = reader.read_u8()? as usize; 40 let mut ids = vec![]; 41 for _ in 0..compression_methods_count { 42 let id = reader.read_u8()?.into(); 43 if id != CompressionMethodId::Unsupported { 44 ids.push(id); 45 } 46 } 47 48 Ok(CompressionMethods { ids }) 49 } 50 } 51