Skip to main content

include_zstd/
lib.rs

1extern crate self as include_zstd;
2
3pub use include_zstd_derive::{bytes, file_bytes, file_str, r#str};
4
5#[doc(hidden)]
6pub mod __private {
7    pub fn decode_utf8(bytes: &'static [u8]) -> &'static str {
8        std::str::from_utf8(bytes).unwrap_or_else(|err| {
9            panic!("include_zstd::str!/file_str! decoded data is not UTF-8: {err}")
10        })
11    }
12
13    pub fn decompress_bytes(compressed: &[u8]) -> Box<[u8]> {
14        zstd::stream::decode_all(compressed)
15            .unwrap_or_else(|err| panic!("include_zstd decode failed: {err}"))
16            .into_boxed_slice()
17    }
18}
19
20#[cfg(test)]
21mod tests {
22    fn include_str_fixture() -> &'static str {
23        crate::str!("hello include-zstd")
24    }
25
26    fn include_bytes_fixture() -> &'static [u8] {
27        crate::bytes!(b"\x00\x01\x02\x03")
28    }
29
30    fn include_file_str_fixture() -> &'static str {
31        crate::file_str!("../Cargo.toml")
32    }
33
34    fn include_file_fixture() -> &'static [u8] {
35        crate::file_bytes!("../Cargo.toml")
36    }
37
38    #[test]
39    fn str_matches_original_text() {
40        let expected: &'static str = "hello include-zstd";
41        let actual: &'static str = include_str_fixture();
42
43        assert_eq!(actual, expected);
44    }
45
46    #[test]
47    fn binary_matches_original_bytes() {
48        let expected: &'static [u8] = b"\x00\x01\x02\x03";
49        let actual: &'static [u8] = include_bytes_fixture();
50
51        assert_eq!(actual, expected);
52    }
53
54    #[test]
55    fn file_str_matches_include_str() {
56        let expected: &'static str = include_str!("../Cargo.toml");
57        let actual: &'static str = include_file_str_fixture();
58
59        assert_eq!(actual, expected);
60    }
61
62    #[test]
63    fn file_matches_include_bytes() {
64        let expected: &'static [u8] = include_bytes!("../Cargo.toml");
65        let actual: &'static [u8] = include_file_fixture();
66
67        assert_eq!(actual, expected);
68    }
69
70    #[test]
71    fn macros_use_once_lock_for_each_callsite() {
72        let first = include_str_fixture().as_ptr();
73        let second = include_str_fixture().as_ptr();
74        assert_eq!(first, second);
75
76        let first = include_bytes_fixture().as_ptr();
77        let second = include_bytes_fixture().as_ptr();
78        assert_eq!(first, second);
79
80        let first = include_file_str_fixture().as_ptr();
81        let second = include_file_str_fixture().as_ptr();
82        assert_eq!(first, second);
83
84        let first = include_file_fixture().as_ptr();
85        let second = include_file_fixture().as_ptr();
86        assert_eq!(first, second);
87    }
88}