sequoia_openpgp/packet/
compressed_data.rs1use std::fmt;
2
3#[cfg(test)]
4use quickcheck::{Arbitrary, Gen};
5
6use crate::packet;
7use crate::Packet;
8use crate::types::CompressionAlgorithm;
9
10#[derive(Clone, PartialEq, Eq, Hash)]
23pub struct CompressedData {
24 pub(crate) common: packet::Common,
26 algo: CompressionAlgorithm,
28
29 container: packet::Container,
31}
32assert_send_and_sync!(CompressedData);
33
34impl fmt::Debug for CompressedData {
35 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 f.debug_struct("CompressedData")
37 .field("algo", &self.algo)
38 .field("container", &self.container)
39 .finish()
40 }
41}
42
43impl CompressedData {
44 pub fn new(algo: CompressionAlgorithm) -> Self {
46 CompressedData {
47 common: Default::default(),
48 algo,
49 container: Default::default(),
50 }
51 }
52
53 pub fn algo(&self) -> CompressionAlgorithm {
55 self.algo
56 }
57
58 pub fn set_algo(&mut self, algo: CompressionAlgorithm) -> CompressionAlgorithm {
60 ::std::mem::replace(&mut self.algo, algo)
61 }
62
63 #[cfg(test)]
65 pub fn push(mut self, packet: Packet) -> Self {
66 self.container.children_mut().unwrap().push(packet);
67 self
68 }
69
70 #[cfg(test)]
75 pub fn insert(mut self, i: usize, packet: Packet) -> Self {
76 self.container.children_mut().unwrap().insert(i, packet);
77 self
78 }
79}
80
81impl_processed_body_forwards!(CompressedData);
82
83impl From<CompressedData> for Packet {
84 fn from(s: CompressedData) -> Self {
85 Packet::CompressedData(s)
86 }
87}
88
89#[cfg(test)]
90impl Arbitrary for CompressedData {
91 fn arbitrary(g: &mut Gen) -> Self {
92 use crate::serialize::SerializeInto;
93 use crate::arbitrary_helper::gen_arbitrary_from_range;
94
95 loop {
96 let a =
97 CompressionAlgorithm::from(gen_arbitrary_from_range(0..4, g));
98 if a.is_supported() {
99 let mut c = CompressedData::new(a);
100 c.set_body(packet::Body::Processed(
104 Packet::arbitrary(g).to_vec().unwrap()
105 ));
106 return c;
107 }
108 }
109 }
110}