pub use include_flate_codegen as codegen;
use include_flate_compress::apply_decompression;
use std::string::FromUtf8Error;
pub use include_flate_compress::CompressionMethod;
#[macro_export]
macro_rules! flate {
($(#[$meta:meta])*
$(pub $(($($vis:tt)+))?)? static $name:ident: [u8] from $path:literal $(with $algo:ident)?) => {
const _: &'static [u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path));
$(#[$meta])*
$(pub $(($($vis)+))?)? static $name: ::std::sync::LazyLock<::std::vec::Vec<u8>> = ::std::sync::LazyLock::new(|| {
let algo = $crate::__parse_algo!($($algo)?);
$crate::decode($crate::codegen::deflate_file!($path $($algo)?), Some(algo))
});
};
($(#[$meta:meta])*
$(pub $(($($vis:tt)+))?)? static $name:ident: str from $path:literal $(with $algo:ident)?) => {
const _: &'static str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path));
$(#[$meta])*
$(pub $(($($vis)+))?)? static $name: ::std::sync::LazyLock<::std::string::String> = ::std::sync::LazyLock::new(|| {
let algo = $crate::__parse_algo!($($algo)?);
$crate::decode_string($crate::codegen::deflate_utf8_file!($path $($algo)?), Some(algo))
});
};
($(#[$meta:meta])*
$(pub $(($($vis:tt)+))?)? static $name:ident: IFlate from $path:literal $(with $algo:ident)?) => {
const _: &'static [u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path));
$(#[$meta])*
$(pub $(($($vis)+))?)? static $name: ::std::sync::LazyLock<$crate::IFlate> = ::std::sync::LazyLock::new(|| {
let algo = $crate::__parse_algo!($($algo)?);
let compressed = $crate::codegen::deflate_file!($path $($algo)?);
$crate::IFlate::new(compressed, algo)
});
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __parse_algo {
() => {
$crate::CompressionMethod::default()
};
(deflate) => {
$crate::CompressionMethod::Deflate
};
(zstd) => {
$crate::CompressionMethod::Zstd
};
($other:ident) => {
compile_error!("Unknown compression algorithm: {}", stringify!($other))
};
}
#[derive(Debug)]
pub struct IFlate {
compressed: &'static [u8],
algo: CompressionMethod,
}
impl IFlate {
#[doc(hidden)]
pub fn new(compressed: &'static [u8], algo: CompressionMethod) -> Self {
Self { compressed, algo }
}
pub fn compressed(&self) -> &[u8] {
self.compressed
}
pub fn decoded(&self) -> Vec<u8> {
decode(self.compressed, Some(self.algo))
}
pub fn decode_string(&self) -> Result<String, FromUtf8Error> {
String::from_utf8(self.decoded())
}
pub fn algo(&self) -> CompressionMethod {
self.algo
}
}
#[doc(hidden)]
#[allow(private_interfaces)]
pub fn decode(bytes: &[u8], algo: Option<CompressionMethod>) -> Vec<u8> {
use std::io::Cursor;
let algo = algo.unwrap_or_default();
let mut source = Cursor::new(bytes);
let mut ret = Vec::new();
match apply_decompression(&mut source, &mut ret, algo) {
Ok(_) => {}
Err(err) => panic!("Compiled `{:?}` buffer was corrupted: {:?}", algo, err),
}
ret
}
#[doc(hidden)]
#[allow(private_interfaces)]
pub fn decode_string(bytes: &[u8], algo: Option<CompressionMethod>) -> String {
String::from_utf8(decode(bytes, algo))
.expect("flate_str has malformed UTF-8 despite checked at compile time")
}