summaryrefslogtreecommitdiffstats
path: root/openpgp/src/serialize/stream/writer/writer_bzip2.rs
blob: b38d8f7ee0d7fc56cc4138c30c6aaa228316eafc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use bzip2::write::BzEncoder;
use std::fmt;
use std::io;

use crate::Result;
use crate::types::CompressionLevel;
use super::{Generic, Message, BoxStack, Stackable, Cookie};

/// BZing writer.
pub struct BZ<'a, C: 'a> {
    inner: Generic<BzEncoder<BoxStack<'a, C>>, C>,
}
assert_send_and_sync!(BZ<'_, C>, C);

impl<'a> BZ<'a, Cookie> {
    /// Makes a BZ compressing writer.
    pub fn new<L>(inner: Message<'a>, cookie: Cookie, level: L) -> Message<'a>
        where L: Into<Option<CompressionLevel>>
    {
        Message::from(Box::new(BZ {
            inner: Generic::new_unboxed(
                BzEncoder::new(inner.into(),
                               level.into().unwrap_or_default().into()),
                cookie),
        }))
    }
}

impl<'a, C: 'a> fmt::Debug for BZ<'a, C> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("writer::BZ")
            .field("inner", &self.inner)
            .finish()
    }
}

impl<'a, C: 'a> io::Write for BZ<'a, C> {
    fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
        self.inner.write(bytes)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()
    }
}

impl<'a, C: 'a> Stackable<'a, C> for BZ<'a, C> {
    fn into_inner(self: Box<Self>) -> Result<Option<BoxStack<'a, C>>> {
        let inner = self.inner.inner.finish()?;
        Ok(Some(inner))
    }
    fn pop(&mut self) -> Result<Option<BoxStack<'a, C>>> {
        unreachable!("Only implemented by Signer")
    }
    fn mount(&mut self, _new: BoxStack<'a, C>) {
        unreachable!("Only implemented by Signer")
    }
    fn inner_mut(&mut self) -> Option<&mut (dyn Stackable<'a, C> + Send + Sync)> {
        Some(self.inner.inner.get_mut())
    }
    fn inner_ref(&self) -> Option<&(dyn Stackable<'a, C> + Send + Sync)> {
        Some(self.inner.inner.get_ref())
    }
    fn cookie_set(&mut self, cookie: C) -> C {
        self.inner.cookie_set(cookie)
    }
    fn cookie_ref(&self) -> &C {
        self.inner.cookie_ref()
    }
    fn cookie_mut(&mut self) -> &mut C {
        self.inner.cookie_mut()
    }
    fn position(&self) -> u64 {
        self.inner.position
    }
}