include_bytes_zstd/lib.rs
1//! Includes a file with zstd compression.
2//!
3//! This macro can be used like [`std::include_bytes`], but the byte array is compressed by the
4//! [zstd crate](https://docs.rs/zstd/). The included data will be decompressed by the
5//! [ruzstd](https://docs.rs/ruzstd) crate in runtime and returned as a [`Vec<u8>`].
6//!
7//! This macro performs the decompression each time it is called.
8//!
9//! # Examples
10//!
11//! `input.txt`:
12//! ```plain
13//! This is a test.
14//! ```
15//!
16//! Rust code:
17//! ```
18//! let data = include_bytes_zstd::include_bytes_zstd!("test-resources/input.txt", 19);
19//! assert_eq!(b"This is a test.\n", data.as_slice());
20//! ```
21
22use std::io::Read;
23
24#[doc(hidden)]
25pub use include_bytes_zstd_macro;
26
27/// Includes a file with zstd compression.
28///
29/// # Arguments
30///
31/// * `filename` - File name relative to the project root.
32/// * `level`: Compression level (1-21).
33#[macro_export]
34macro_rules! include_bytes_zstd {
35 ($filename:literal, $level:literal) => {{
36 const _: &'static [u8] =
37 include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $filename));
38 $crate::include_bytes_zstd_macro::include_bytes_zstd!($filename, $level)
39 }};
40}
41
42/// Decodes zstd compressed data.
43#[doc(hidden)]
44pub fn decode(data: &[u8]) -> Vec<u8> {
45 let mut decoder = ruzstd::StreamingDecoder::new(data).unwrap();
46 let mut buff = vec![];
47 decoder.read_to_end(&mut buff).unwrap();
48 buff
49}