zstd/
lib.rs

1//! Rust binding to the [zstd library][zstd].
2//!
3//! This crate provides:
4//!
5//! * An [encoder](stream/write/struct.Encoder.html) to compress data using zstd
6//!   and send the output to another write.
7//! * A [decoder](stream/read/struct.Decoder.html) to read input data from a `Read`
8//!   and decompress it.
9//! * Convenient functions for common tasks.
10//!
11//! # Example
12//!
13//! ```no_run
14//! use std::io;
15//!
16//! // Uncompress input and print the result.
17//! zstd::stream::copy_decode(io::stdin(), io::stdout()).unwrap();
18//! ```
19//!
20//! [zstd]: https://github.com/facebook/zstd
21#![deny(missing_docs)]
22#![cfg_attr(feature = "doc-cfg", feature(doc_cfg))]
23
24// Re-export the zstd-safe crate.
25pub use zstd_safe;
26
27pub mod bulk;
28pub mod dict;
29
30#[macro_use]
31pub mod stream;
32
33use std::io;
34
35/// Default compression level.
36pub use zstd_safe::CLEVEL_DEFAULT as DEFAULT_COMPRESSION_LEVEL;
37
38/// The accepted range of compression levels.
39pub fn compression_level_range(
40) -> std::ops::RangeInclusive<zstd_safe::CompressionLevel> {
41    zstd_safe::min_c_level()..=zstd_safe::max_c_level()
42}
43
44#[doc(no_inline)]
45pub use crate::stream::{decode_all, encode_all, Decoder, Encoder};
46
47/// Returns the error message as io::Error based on error_code.
48fn map_error_code(code: usize) -> io::Error {
49    let msg = zstd_safe::get_error_name(code);
50    io::Error::new(io::ErrorKind::Other, msg.to_string())
51}
52
53// Some helper functions to write full-cycle tests.
54
55#[cfg(test)]
56fn test_cycle<F, G>(data: &[u8], f: F, g: G)
57where
58    F: Fn(&[u8]) -> Vec<u8>,
59    G: Fn(&[u8]) -> Vec<u8>,
60{
61    let mid = f(data);
62    let end = g(&mid);
63    assert_eq!(data, &end[..]);
64}
65
66#[cfg(test)]
67fn test_cycle_unwrap<F, G>(data: &[u8], f: F, g: G)
68where
69    F: Fn(&[u8]) -> io::Result<Vec<u8>>,
70    G: Fn(&[u8]) -> io::Result<Vec<u8>>,
71{
72    test_cycle(data, |data| f(data).unwrap(), |data| g(data).unwrap())
73}
74
75#[test]
76fn default_compression_level_in_range() {
77    assert!(compression_level_range().contains(&DEFAULT_COMPRESSION_LEVEL));
78}