use std::fmt;
use std::ops::{Deref, DerefMut};
use packet::{self, Common};
use Packet;
use Container;
use constants::CompressionAlgorithm;
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct CompressedData {
pub(crate) common: packet::Common,
algo: CompressionAlgorithm,
}
impl fmt::Debug for CompressedData {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("CompressedData")
.field("algo", &self.algo)
.field("children",
&self.common.children.as_ref()
.map(|c| &c.packets).unwrap_or(&Vec::new()))
.field("body (bytes)",
&self.common.body.as_ref().unwrap_or(&b"".to_vec()).len())
.finish()
}
}
impl CompressedData {
pub fn new(algo: CompressionAlgorithm) -> Self {
CompressedData {
common: Default::default(),
algo: algo,
}
}
pub fn algorithm(&self) -> CompressionAlgorithm {
self.algo
}
pub fn set_algorithm(&mut self, algo: CompressionAlgorithm) -> CompressionAlgorithm {
::std::mem::replace(&mut self.algo, algo)
}
pub fn push(mut self, packet: Packet) -> Self {
if self.common.children.is_none() {
self.common.children = Some(Container::new());
}
self.common.children.as_mut().unwrap().push(packet);
self
}
pub fn insert(mut self, i: usize, packet: Packet) -> Self {
if self.common.children.is_none() {
self.common.children = Some(Container::new());
}
self.common.children.as_mut().unwrap().insert(i, packet);
self
}
}
impl From<CompressedData> for Packet {
fn from(s: CompressedData) -> Self {
Packet::CompressedData(s)
}
}
impl<'a> Deref for CompressedData {
type Target = Common;
fn deref(&self) -> &Self::Target {
&self.common
}
}
impl<'a> DerefMut for CompressedData {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.common
}
}