Lines
100 %
Functions
Branches
use anyhow::Result;
use flate2::{Compression, CrcReader, CrcWriter};
use crate::RwBuilder;
/// Type returned by the `deflate`, `gz` and `zlib` functions on the `RwBuilder`
/// trait. It is itself an `RwBuilder` so can be chained further.
#[derive(Debug)]
pub struct CompressionBuilder<B, C>
where
B: RwBuilder,
C: CoderBuilder<B::Reader, B::Writer>,
{
/// The inner builder it wraps
builder: B,
/// The compression used for the encoder
compression: Compression,
/// The builder for the encoder and decoder
coder: C,
}
impl<B, C> RwBuilder for CompressionBuilder<B, C>
B::Reader: std::io::Read,
B::Writer: std::io::Write,
C::Decoder: std::io::Read,
C::Encoder: std::io::Write,
type Reader = C::Decoder;
type Writer = C::Encoder;
fn reader(&self) -> Result<Self::Reader> {
let reader = self.builder.reader()?;
Ok(self.coder.decoder(reader))
fn writer(&self) -> Result<Self::Writer> {
let writer = self.builder.writer()?;
Ok(self.coder.encoder(writer, self.compression))
/// Implementors like `Deflate`, `Gz` and `Zlib` create the associated encoders
/// and decoders.
pub trait CoderBuilder<R, W> {
/// The type of encoder created
type Encoder;
/// Create an encoder on top of a writer
fn encoder(&self, writer: W, compression: Compression) -> Self::Encoder;
/// The type of decoder created
type Decoder;
/// Create a decoder on top of a reader
fn decoder(&self, reader: R) -> Self::Decoder;
/// The Zlib encoder and decoder builder
#[derive(Default, Debug, Copy, Clone)]
pub struct Zlib;
impl<R, W> CoderBuilder<R, W> for Zlib
R: std::io::Read,
W: std::io::Write,
type Decoder = flate2::read::ZlibDecoder<R>;
type Encoder = flate2::write::ZlibEncoder<W>;
fn encoder(&self, writer: W, compression: Compression) -> Self::Encoder {
flate2::write::ZlibEncoder::new(writer, compression)
fn decoder(&self, reader: R) -> Self::Decoder {
flate2::read::ZlibDecoder::new(reader)
/// Convenience trait for creating a new encoder/decoder builder
pub trait Constructor<B>
Self: Sized + CoderBuilder<B::Reader, B::Writer> + Default,
/// Create the encoder/decoder builder
fn new(builder: B, compression: Compression) -> CompressionBuilder<B, Self> {
CompressionBuilder { builder, compression, coder: Self::default() }
impl<B> Constructor<B> for Zlib where B: RwBuilder {}
/// The Gz encoder and decoder builder
pub struct Gz;
impl<R, W> CoderBuilder<R, W> for Gz
type Decoder = flate2::read::GzDecoder<R>;
type Encoder = flate2::write::GzEncoder<W>;
flate2::write::GzEncoder::new(writer, compression)
flate2::read::GzDecoder::new(reader)
impl<B> Constructor<B> for Gz where B: RwBuilder {}
/// The Deflate encoder and decoder builder
pub struct Deflate;
impl<R, W> CoderBuilder<R, W> for Deflate
type Decoder = flate2::read::DeflateDecoder<R>;
type Encoder = flate2::write::DeflateEncoder<W>;
flate2::write::DeflateEncoder::new(writer, compression)
flate2::read::DeflateDecoder::new(reader)
impl<B> Constructor<B> for Deflate where B: RwBuilder {}
/// Type returned by the `crc` function on the `RwBuilder` trait.
/// It is itself an `RwBuilder` so can be chained further, although this is an
/// uncommon scenario
pub struct CrcBuilder<B>
impl<B> CrcBuilder<B>
/// Factory function to wrap an inner builder
#[must_use]
pub const fn new(builder: B) -> Self {
Self { builder }
impl<B> RwBuilder for CrcBuilder<B>
type Reader = CrcReader<B::Reader>;
type Writer = CrcWriter<B::Writer>;
Ok(CrcReader::new(self.builder.reader()?))
Ok(CrcWriter::new(self.builder.writer()?))