pub fn compress(
    compression: CompressionOptions,
    input_buf: &[u8],
    output_buf: &mut Vec<u8>,
) -> Result<()> {
    match compression {
        #[cfg(feature = "brotli")]
        CompressionOptions::Brotli(level) => {
            use std::io::Write;
            const BROTLI_DEFAULT_BUFFER_SIZE: usize = 4096;
            const BROTLI_DEFAULT_LG_WINDOW_SIZE: u32 = 22; // recommended between 20-22

            let q = level.unwrap_or_default();
            let mut encoder = brotli::CompressorWriter::new(
                output_buf,
                BROTLI_DEFAULT_BUFFER_SIZE,
                q.compression_level(),
                BROTLI_DEFAULT_LG_WINDOW_SIZE,
            );
            encoder.write_all(input_buf)?;
            encoder.flush().map_err(|e| e.into())
        }
        #[cfg(not(feature = "brotli"))]
        CompressionOptions::Brotli(_) => Err(Error::FeatureNotActive(
            crate::error::Feature::Brotli,
            "compress to brotli".to_string(),
        )),
        #[cfg(feature = "gzip")]
        CompressionOptions::Gzip(level) => {
            use std::io::Write;
            let level = level.unwrap_or_default();
            let mut encoder = flate2::write::GzEncoder::new(output_buf, level.into());
            encoder.write_all(input_buf)?;
            encoder.try_finish().map_err(|e| e.into())
        }
        #[cfg(not(feature = "gzip"))]
        CompressionOptions::Gzip(_) => Err(Error::FeatureNotActive(
            crate::error::Feature::Gzip,
            "compress to gzip".to_string(),
        )),
        #[cfg(feature = "snappy")]
        CompressionOptions::Snappy => inner_compress(
            input_buf,
            output_buf,
            |len| Ok(snap::raw::max_compress_len(len)),
            |input, output| Ok(snap::raw::Encoder::new().compress(input, output)?),
        ),
        #[cfg(not(feature = "snappy"))]
        CompressionOptions::Snappy => Err(Error::FeatureNotActive(
            crate::error::Feature::Snappy,
            "compress to snappy".to_string(),
        )),
        #[cfg(all(feature = "lz4_flex", not(feature = "lz4")))]
        CompressionOptions::Lz4Raw => inner_compress(
            input_buf,
            output_buf,
            |len| Ok(lz4_flex::block::get_maximum_output_size(len)),
            |input, output| {
                let compressed_size = lz4_flex::block::compress_into(input, output)?;
                Ok(compressed_size)
            },
        ),
        #[cfg(feature = "lz4")]
        CompressionOptions::Lz4Raw => inner_compress(
            input_buf,
            output_buf,
            |len| Ok(lz4::block::compress_bound(len)?),
            |input, output| {
                let compressed_size = lz4::block::compress_to_buffer(input, None, false, output)?;
                Ok(compressed_size)
            },
        ),
        #[cfg(all(not(feature = "lz4"), not(feature = "lz4_flex")))]
        CompressionOptions::Lz4Raw => Err(Error::FeatureNotActive(
            crate::error::Feature::Lz4,
            "compress to lz4".to_string(),
        )),
        #[cfg(feature = "zstd")]
        CompressionOptions::Zstd(level) => {
            use std::io::Write;
            let level = level.map(|v| v.compression_level()).unwrap_or_default();

            let mut encoder = zstd::Encoder::new(output_buf, level)?;
            encoder.write_all(input_buf)?;
            match encoder.finish() {
                Ok(_) => Ok(()),
                Err(e) => Err(e.into()),
            }
        }
        #[cfg(not(feature = "zstd"))]
        CompressionOptions::Zstd(_) => Err(Error::FeatureNotActive(
            crate::error::Feature::Zstd,
            "compress to zstd".to_string(),
        )),
        CompressionOptions::Uncompressed => {
            Err(general_err!("Compressing without compression is not valid"))
        }
        _ => Err(general_err!(
            "Compression {:?} is not supported",
            compression
        )),
    }
}
/// Compresses data stored in slice `input_buf` and writes the compressed result
/// to `output_buf`.
/// Note that you'll need to call `clear()` before reusing the same `output_buf`
/// across different `compress` calls.
Compresses data stored in slice `input_buf` and writes the compressed result
to `output_buf`.
Compresses data stored in slice ` input_buf ` and writes the compressed result to ` output_buf ` .
pub fn compress(
    compression: CompressionOptions,
    input_buf: &[u8],
    output_buf: &mut Vec<u8>,
) -> Result<()> {
    match compression {
        #[cfg(feature = "brotli")]
        CompressionOptions::Brotli(level) => {
            use std::io::Write;
            const BROTLI_DEFAULT_BUFFER_SIZE: usize = 4096;
            const BROTLI_DEFAULT_LG_WINDOW_SIZE: u32 = 22; // recommended between 20-22
            let q = level.unwrap_or_default();
            let mut encoder = brotli::CompressorWriter::new(
                output_buf,
                BROTLI_DEFAULT_BUFFER_SIZE,
                q.compression_level(),
                BROTLI_DEFAULT_LG_WINDOW_SIZE,
            );
            encoder.write_all(input_buf)?;
            encoder.flush().map_err(|e| e.into())
        }
        #[cfg(not(feature = "brotli"))]
        CompressionOptions::Brotli(_) => Err(Error::FeatureNotActive(
            crate::error::Feature::Brotli,
            "compress to brotli".to_string(),
        )),
        #[cfg(feature = "gzip")]
        CompressionOptions::Gzip(level) => {
            use std::io::Write;
            let level = level.unwrap_or_default();
            let mut encoder = flate2::write::GzEncoder::new(output_buf, level.into());
            encoder.write_all(input_buf)?;
            encoder.try_finish().map_err(|e| e.into())
        }
        #[cfg(not(feature = "gzip"))]
        CompressionOptions::Gzip(_) => Err(Error::FeatureNotActive(
            crate::error::Feature::Gzip,
            "compress to gzip".to_string(),
        )),
        #[cfg(feature = "snappy")]
        CompressionOptions::Snappy => inner_compress(
            input_buf,
            output_buf,
            |len| Ok(snap::raw::max_compress_len(len)),
            |input, output| Ok(snap::raw::Encoder::new().compress(input, output)?),
        ),
        #[cfg(not(feature = "snappy"))]
        CompressionOptions::Snappy => Err(Error::FeatureNotActive(
            crate::error::Feature::Snappy,
            "compress to snappy".to_string(),
        )),
        #[cfg(all(feature = "lz4_flex", not(feature = "lz4")))]
        CompressionOptions::Lz4Raw => inner_compress(
            input_buf,
            output_buf,
            |len| Ok(lz4_flex::block::get_maximum_output_size(len)),
            |input, output| {
                let compressed_size = lz4_flex::block::compress_into(input, output)?;
                Ok(compressed_size)
            },
        ),
        #[cfg(feature = "lz4")]
        CompressionOptions::Lz4Raw => inner_compress(
            input_buf,
            output_buf,
            |len| Ok(lz4::block::compress_bound(len)?),
            |input, output| {
                let compressed_size = lz4::block::compress_to_buffer(input, None, false, output)?;
                Ok(compressed_size)
            },
        ),
        #[cfg(all(not(feature = "lz4"), not(feature = "lz4_flex")))]
        CompressionOptions::Lz4Raw => Err(Error::FeatureNotActive(
            crate::error::Feature::Lz4,
            "compress to lz4".to_string(),
        )),
        #[cfg(feature = "zstd")]
        CompressionOptions::Zstd(level) => {
            use std::io::Write;
            let level = level.map(|v| v.compression_level()).unwrap_or_default();
            let mut encoder = zstd::Encoder::new(output_buf, level)?;
            encoder.write_all(input_buf)?;
            match encoder.finish() {
                Ok(_) => Ok(()),
                Err(e) => Err(e.into()),
            }
        }
        #[cfg(not(feature = "zstd"))]
        CompressionOptions::Zstd(_) => Err(Error::FeatureNotActive(
            crate::error::Feature::Zstd,
            "compress to zstd".to_string(),
        )),
        CompressionOptions::Uncompressed => {
            Err(general_err!("Compressing without compression is not valid"))
        }
        _ => Err(general_err!(
            "Compression {:?} is not supported",
            compression
        )),
    }
}
pub fn compress ( compression : CompressionOptions , input_buf : & [ u8 ] , output_buf : & mut Vec < u8 > , ) -> Result < ( ) > { match compression { # [ cfg ( feature " " ) ] CompressionOptions :: Brotli ( level ) => { use std :: io :: Write ; const BROTLI_DEFAULT_BUFFER_SIZE : usize = 4096 ; const BROTLI_DEFAULT_LG_WINDOW_SIZE : u32 = 22 ; // recommended between 20-22 let q = level . unwrap_or_default ( ) ; let mut encoder = brotli :: CompressorWriter :: new ( output_buf , BROTLI_DEFAULT_BUFFER_SIZE , q . compression_level ( ) , BROTLI_DEFAULT_LG_WINDOW_SIZE , ) ; encoder . write_all ( input_buf ) ? ; encoder . flush ( ) . map_err ( | e | e . into ( ) ) } # [ cfg ( not ( feature " " ) ) ] CompressionOptions :: Brotli ( _ ) => Err ( Error :: FeatureNotActive ( crate :: error :: Feature :: Brotli , " " . to_string ( ) , ) ) , # [ cfg ( feature " " ) ] CompressionOptions :: Gzip ( level ) => { use std :: io :: Write ; let level = level . unwrap_or_default ( ) ; let mut encoder = flate2 :: write :: GzEncoder :: new ( output_buf , level . into ( ) ) ; encoder . write_all ( input_buf ) ? ; encoder . try_finish ( ) . map_err ( | e | e . into ( ) ) } # [ cfg ( not ( feature " " ) ) ] CompressionOptions :: Gzip ( _ ) => Err ( Error :: FeatureNotActive ( crate :: error :: Feature :: Gzip , " " . to_string ( ) , ) ) , # [ cfg ( feature " " ) ] CompressionOptions :: Snappy => inner_compress ( input_buf , output_buf , | len | Ok ( snap :: raw :: max_compress_len ( len ) ) , | input , output | Ok ( snap :: raw :: Encoder :: new ( ) . compress ( input , output ) ? ) , ) , # [ cfg ( not ( feature " " ) ) ] CompressionOptions :: Snappy => Err ( Error :: FeatureNotActive ( crate :: error :: Feature :: Snappy , " " . to_string ( ) , ) ) , # [ cfg ( all ( feature " " not ( feature " " ) ) ) ] CompressionOptions :: Lz4Raw => inner_compress ( input_buf , output_buf , | len | Ok ( lz4_flex :: block :: get_maximum_output_size ( len ) ) , | input , output | { let compressed_size = lz4_flex :: block :: compress_into ( input , output ) ? ; Ok ( compressed_size ) } , ) , # [ cfg ( feature " " ) ] CompressionOptions :: Lz4Raw => inner_compress ( input_buf , output_buf , | len | Ok ( lz4 :: block :: compress_bound ( len ) ? ) , | input , output | { let compressed_size = lz4 :: block :: compress_to_buffer ( input , None , false , output ) ? ; Ok ( compressed_size ) } , ) , # [ cfg ( all ( not ( feature " " ) not ( feature " " ) ) ) ] CompressionOptions :: Lz4Raw => Err ( Error :: FeatureNotActive ( crate :: error :: Feature :: Lz4 , " " . to_string ( ) , ) ) , # [ cfg ( feature " " ) ] CompressionOptions :: Zstd ( level ) => { use std :: io :: Write ; let level = level . map ( | v | v . compression_level ( ) ) . unwrap_or_default ( ) ; let mut encoder = zstd :: Encoder :: new ( output_buf , level ) ? ; encoder . write_all ( input_buf ) ? ; match encoder . finish ( ) { Ok ( _ ) => Ok ( ( ) ) , Err ( e ) => Err ( e . into ( ) ) , } } # [ cfg ( not ( feature " " ) ) ] CompressionOptions :: Zstd ( _ ) => Err ( Error :: FeatureNotActive ( crate :: error :: Feature :: Zstd , " " . to_string ( ) , ) ) , CompressionOptions :: Uncompressed => { Err ( general_err ! ( " " ) ) } _ => Err ( general_err ! ( " " compression ) ) , } }
Compresses data stored in slice `input_buf` and writes the compressed result
to `output_buf`.
Compresses data stored in slice ` input_buf ` and writes the compressed result to ` output_buf ` .
compression
CompressionOptions
input_buf
&[u8]
output_buf
&mut Vec<u8>