zstd/stream/
functions.rs

1use std::io;
2
3use super::{Decoder, Encoder};
4
5/// Decompress from the given source as if using a `Decoder`.
6///
7/// The input data must be in the zstd frame format.
8pub fn decode_all<R: io::Read>(source: R) -> io::Result<Vec<u8>> {
9    let mut result = Vec::new();
10    copy_decode(source, &mut result)?;
11    Ok(result)
12}
13
14/// Decompress from the given source as if using a `Decoder`.
15///
16/// Decompressed data will be appended to `destination`.
17pub fn copy_decode<R, W>(source: R, mut destination: W) -> io::Result<()>
18where
19    R: io::Read,
20    W: io::Write,
21{
22    let mut decoder = Decoder::new(source)?;
23    io::copy(&mut decoder, &mut destination)?;
24    Ok(())
25}
26
27/// Compress all data from the given source as if using an `Encoder`.
28///
29/// Result will be in the zstd frame format.
30///
31/// A level of `0` uses zstd's default (currently `3`).
32pub fn encode_all<R: io::Read>(source: R, level: i32) -> io::Result<Vec<u8>> {
33    let mut result = Vec::<u8>::new();
34    copy_encode(source, &mut result, level)?;
35    Ok(result)
36}
37
38/// Compress all data from the given source as if using an `Encoder`.
39///
40/// Compressed data will be appended to `destination`.
41///
42/// A level of `0` uses zstd's default (currently `3`).
43pub fn copy_encode<R, W>(
44    mut source: R,
45    destination: W,
46    level: i32,
47) -> io::Result<()>
48where
49    R: io::Read,
50    W: io::Write,
51{
52    let mut encoder = Encoder::new(destination, level)?;
53    io::copy(&mut source, &mut encoder)?;
54    encoder.finish()?;
55    Ok(())
56}
57
58#[cfg(tests)]
59mod tests {}