#![cfg_attr(docsrs, doc(cfg(feature = "compression")))]
#![cfg(feature = "compression")]
pub mod bzip;
pub mod flate;
pub mod xz;
use singlefile::FileFormat;
use std::io::{Read, Write};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Compressed<C, F> {
pub format: F,
pub compression: C,
pub level: u32
}
impl<C, F> Compressed<C, F> {
#[inline]
pub const fn with_level(format: F, compression: C, level: u32) -> Self {
Compressed { format, compression, level }
}
}
impl<C, F> Compressed<C, F> where C: CompressionFormatLevels {
#[inline]
pub const fn new(format: F, compression: C) -> Self {
Compressed::with_level(format, compression, C::COMPRESSION_LEVEL_DEFAULT)
}
#[inline]
pub const fn new_fast_compression(format: F, compression: C) -> Self {
Compressed::with_level(format, compression, C::COMPRESSION_LEVEL_FAST)
}
#[inline]
pub const fn new_best_compression(format: F, compression: C) -> Self {
Compressed::with_level(format, compression, C::COMPRESSION_LEVEL_BEST)
}
}
impl<C, F> Default for Compressed<C, F>
where C: Default + CompressionFormatLevels, F: Default {
#[inline]
fn default() -> Self {
Compressed::new(F::default(), C::default())
}
}
impl<T, C, F> FileFormat<T> for Compressed<C, F>
where C: CompressionFormat, F: FileFormat<T> {
type FormatError = F::FormatError;
fn from_reader<R: Read>(&self, reader: R) -> Result<T, Self::FormatError> {
self.format.from_reader(self.compression.decode_reader(reader))
}
fn to_writer<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError> {
self.format.to_writer(self.compression.encode_writer(writer, self.level), value)
}
}
pub trait CompressionFormat {
type Encoder<W: Write>: Write;
type Decoder<R: Read>: Read;
fn encode_writer<W: Write>(&self, writer: W, level: u32) -> Self::Encoder<W>;
fn decode_reader<R: Read>(&self, reader: R) -> Self::Decoder<R>;
}
pub trait CompressionFormatLevels: CompressionFormat {
const COMPRESSION_LEVEL_NONE: u32;
const COMPRESSION_LEVEL_FAST: u32;
const COMPRESSION_LEVEL_BEST: u32;
const COMPRESSION_LEVEL_DEFAULT: u32;
}