use std::fmt;
use crate::packet;
use crate::Packet;
use crate::types::CompressionAlgorithm;
#[derive(Clone)]
pub struct CompressedData {
pub(crate) common: packet::Common,
algo: CompressionAlgorithm,
container: packet::Container,
}
impl PartialEq for CompressedData {
fn eq(&self, other: &CompressedData) -> bool {
self.algo == other.algo
&& self.container == other.container
}
}
impl Eq for CompressedData {}
impl std::hash::Hash for CompressedData {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::hash::Hash::hash(&self.algo, state);
std::hash::Hash::hash(&self.container, state);
}
}
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.container.children_ref())
.field("body (bytes)",
&self.container.body().len())
.field("body_digest", &self.container.body_digest())
.finish()
}
}
impl CompressedData {
pub fn new(algo: CompressionAlgorithm) -> Self {
CompressedData {
common: Default::default(),
algo: algo,
container: Default::default(),
}
}
pub fn algo(&self) -> CompressionAlgorithm {
self.algo
}
pub fn set_algo(&mut self, algo: CompressionAlgorithm) -> CompressionAlgorithm {
::std::mem::replace(&mut self.algo, algo)
}
#[cfg(test)]
pub fn push(mut self, packet: Packet) -> Self {
self.container.children_mut().push(packet);
self
}
#[cfg(test)]
pub fn insert(mut self, i: usize, packet: Packet) -> Self {
self.container.children_mut().insert(i, packet);
self
}
}
impl_container_forwards!(CompressedData);
impl From<CompressedData> for Packet {
fn from(s: CompressedData) -> Self {
Packet::CompressedData(s)
}
}