|
29 | 29 | /// The low-level macros used by this crate. |
30 | 30 | pub use include_flate_codegen as codegen; |
31 | 31 | use include_flate_compress::apply_decompression; |
| 32 | +use std::string::FromUtf8Error; |
32 | 33 |
|
33 | | -#[doc(hidden)] |
34 | 34 | pub use include_flate_compress::CompressionMethod; |
35 | 35 |
|
36 | 36 | /// This macro is like [`include_bytes!`][1] or [`include_str!`][2], but compresses at compile time |
@@ -117,6 +117,51 @@ macro_rules! flate { |
117 | 117 | $crate::decode_string($crate::codegen::deflate_utf8_file!($path $($algo)?), Some($crate::CompressionMethodTy(algo))) |
118 | 118 | }); |
119 | 119 | }; |
| 120 | + ($(#[$meta:meta])* |
| 121 | + $(pub $(($($vis:tt)+))?)? static $name:ident: IFlate from $path:literal $(with $algo:ident)?) => { |
| 122 | + // HACK: workaround to make cargo auto rebuild on modification of source file |
| 123 | + const _: &'static [u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path)); |
| 124 | + |
| 125 | + $(#[$meta])* |
| 126 | + $(pub $(($($vis)+))?)? static $name: ::std::sync::LazyLock<$crate::IFlate> = ::std::sync::LazyLock::new(|| { |
| 127 | + let algo = match stringify!($($algo)?){ |
| 128 | + "deflate" => $crate::CompressionMethod::Deflate, |
| 129 | + "zstd" => $crate::CompressionMethod::Zstd, |
| 130 | + _ => $crate::CompressionMethod::default(), |
| 131 | + }; |
| 132 | + let compressed = $crate::codegen::deflate_file!($path $($algo)?); |
| 133 | + $crate::IFlate::new(compressed, algo) |
| 134 | + }); |
| 135 | + }; |
| 136 | +} |
| 137 | + |
| 138 | +#[derive(Debug)] |
| 139 | +pub struct IFlate { |
| 140 | + compressed: &'static [u8], |
| 141 | + algo: CompressionMethod, |
| 142 | +} |
| 143 | + |
| 144 | +impl IFlate { |
| 145 | + #[doc(hidden)] |
| 146 | + pub fn new(compressed: &'static [u8], algo: CompressionMethod) -> Self { |
| 147 | + Self { compressed, algo } |
| 148 | + } |
| 149 | + |
| 150 | + pub fn compressed(&self) -> &[u8] { |
| 151 | + &self.compressed |
| 152 | + } |
| 153 | + |
| 154 | + pub fn decoded(&self) -> Vec<u8> { |
| 155 | + decode(&self.compressed, Some(CompressionMethodTy(self.algo))) |
| 156 | + } |
| 157 | + |
| 158 | + pub fn decode_string(&self) -> Result<String, FromUtf8Error> { |
| 159 | + String::from_utf8(self.decoded()) |
| 160 | + } |
| 161 | + |
| 162 | + pub fn algo(&self) -> CompressionMethod { |
| 163 | + self.algo |
| 164 | + } |
120 | 165 | } |
121 | 166 |
|
122 | 167 | #[derive(Debug)] |
|
0 commit comments