webrtc_dtls/
compression_methods.rs

1use std::io::{Read, Write};
2
3use byteorder::{ReadBytesExt, WriteBytesExt};
4
5use crate::error::Result;
6
7#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8pub enum CompressionMethodId {
9    Null = 0,
10    Unsupported,
11}
12
13impl 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, Eq)]
23pub struct CompressionMethods {
24    pub ids: Vec<CompressionMethodId>,
25}
26
27impl CompressionMethods {
28    pub fn size(&self) -> usize {
29        1 + self.ids.len()
30    }
31
32    pub fn marshal<W: Write>(&self, writer: &mut W) -> Result<()> {
33        writer.write_u8(self.ids.len() as u8)?;
34
35        for id in &self.ids {
36            writer.write_u8(*id as u8)?;
37        }
38
39        Ok(writer.flush()?)
40    }
41
42    pub fn unmarshal<R: Read>(reader: &mut R) -> Result<Self> {
43        let compression_methods_count = reader.read_u8()? as usize;
44        let mut ids = vec![];
45        for _ in 0..compression_methods_count {
46            let id = reader.read_u8()?.into();
47            if id != CompressionMethodId::Unsupported {
48                ids.push(id);
49            }
50        }
51
52        Ok(CompressionMethods { ids })
53    }
54}
55
56pub fn default_compression_methods() -> CompressionMethods {
57    CompressionMethods {
58        ids: vec![CompressionMethodId::Null],
59    }
60}