Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ keywords = ["compression", "deflate", "macro", "include", "assets"]
include = ["/src", "/LICENSE", "/README.md"]

[dependencies]
include-flate-codegen = { version = "0.3.1", path = "codegen" }
include-flate-compress = { version = "0.3.1", path = "compress" }
libflate = { workspace = true }
zstd = { workspace = true }
include-flate-codegen = { version = "0.3.1", path = "codegen", default-features = false }
include-flate-compress = { version = "0.3.1", path = "compress", default-features = false }

[features]
default = ["deflate", "zstd"]
deflate = ["include-flate-compress/deflate"]
zstd = ["include-flate-compress/zstd"]
deflate = ["include-flate-codegen/deflate", "include-flate-compress/deflate"]
zstd = ["include-flate-codegen/zstd", "include-flate-compress/zstd"]
no-compression-warnings = ["include-flate-codegen/no-compression-warnings"]
8 changes: 4 additions & 4 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ description = "Macro codegen for the include-flate crate"
proc-macro = true

[dependencies]
libflate = { workspace = true }
proc-macro2 = "1.0.95"
quote = "1.0.40"
syn = { version = "2.0.104", features = ["full"] }
zstd = { workspace = true }
include-flate-compress = { version = "0.3.1", path = "../compress" }
include-flate-compress = { version = "0.3.1", path = "../compress", default-features = false }
proc-macro-error = "1.0.4"

[features]
default = []
default = ["deflate", "zstd"]
deflate = ["include-flate-compress/deflate"]
zstd = ["include-flate-compress/zstd"]
no-compression-warnings = []
27 changes: 19 additions & 8 deletions codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,24 @@ impl syn::parse::Parse for FlateArgs {
} else {
let lookahead = input.lookahead1();
if lookahead.peek(kw::deflate) {
input.parse::<kw::deflate>()?;
Some(CompressionMethodTy(CompressionMethod::Deflate))
#[cfg(feature = "deflate")]
{
input.parse::<kw::deflate>()?;
Some(CompressionMethodTy(CompressionMethod::Deflate))
}
#[cfg(not(feature = "deflate"))]
return Err(Error::new(
input.span(),
"Please enable the `deflate` feature",
));
} else if lookahead.peek(kw::zstd) {
input.parse::<kw::zstd>()?;
Some(CompressionMethodTy(CompressionMethod::Zstd))
#[cfg(feature = "zstd")]
{
input.parse::<kw::zstd>()?;
Some(CompressionMethodTy(CompressionMethod::Zstd))
}
#[cfg(not(feature = "zstd"))]
return Err(Error::new(input.span(), "Please enable the `zstd` feature"));
} else {
return Err(lookahead.error());
}
Expand All @@ -106,7 +119,7 @@ mod kw {
syn::custom_keyword!(zstd);
}

#[derive(Debug)]
#[derive(Debug, Default)]
struct CompressionMethodTy(CompressionMethod);

fn compression_ratio(original_size: u64, compressed_size: u64) -> f64 {
Expand All @@ -122,9 +135,7 @@ fn inner(ts: TokenStream, utf8: bool) -> syn::Result<impl Into<TokenStream>> {

let args: FlateArgs = syn::parse2::<FlateArgs>(ts.to_owned().into())?;
let path = PathBuf::from_str(&args.path.value()).map_err(emap)?;
let algo = args
.algorithm
.unwrap_or(CompressionMethodTy(CompressionMethod::Deflate));
let algo = args.algorithm.unwrap_or_default();

if path.is_absolute() {
Err(emap("absolute paths are not supported"))?;
Expand Down
4 changes: 3 additions & 1 deletion compress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
#[cfg(not(any(feature = "zstd", feature = "deflate")))]
compile_error!("You must enable either the `deflate` or `zstd` feature.");

#[cfg(feature = "zstd")]
use std::io::BufReader;
use std::{
fmt,
io::{self, BufRead, BufReader, Read, Seek, Write},
io::{self, BufRead, Read, Seek, Write},
};

#[cfg(feature = "deflate")]
Expand Down
54 changes: 27 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ macro_rules! flate {

$(#[$meta])*
$(pub $(($($vis)+))?)? static $name: ::std::sync::LazyLock<::std::vec::Vec<u8>> = ::std::sync::LazyLock::new(|| {
$crate::decode($crate::codegen::deflate_file!($path), None)
let algo = $crate::__parse_algo!($($algo)?);
$crate::decode($crate::codegen::deflate_file!($path $($algo)?), Some(algo))
});
};
($(#[$meta:meta])*
Comment on lines 99 to 106

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must have been an oversight in #25 no? Anyways, proper support has been added.

Expand All @@ -109,12 +110,8 @@ macro_rules! flate {

$(#[$meta])*
$(pub $(($($vis)+))?)? static $name: ::std::sync::LazyLock<::std::string::String> = ::std::sync::LazyLock::new(|| {
let algo = match stringify!($($algo)?){
"deflate" => $crate::CompressionMethod::Deflate,
"zstd" => $crate::CompressionMethod::Zstd,
_ => $crate::CompressionMethod::default(),
};
$crate::decode_string($crate::codegen::deflate_utf8_file!($path $($algo)?), Some($crate::CompressionMethodTy(algo)))
let algo = $crate::__parse_algo!($($algo)?);
$crate::decode_string($crate::codegen::deflate_utf8_file!($path $($algo)?), Some(algo))
});
};
($(#[$meta:meta])*
Expand All @@ -124,17 +121,31 @@ macro_rules! flate {

$(#[$meta])*
$(pub $(($($vis)+))?)? static $name: ::std::sync::LazyLock<$crate::IFlate> = ::std::sync::LazyLock::new(|| {
let algo = match stringify!($($algo)?){
"deflate" => $crate::CompressionMethod::Deflate,
"zstd" => $crate::CompressionMethod::Zstd,
_ => $crate::CompressionMethod::default(),
};
let algo = $crate::__parse_algo!($($algo)?);
let compressed = $crate::codegen::deflate_file!($path $($algo)?);
$crate::IFlate::new(compressed, algo)
});
};
}

/// Helper macro to parse the user-specified algorithm.
#[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],
Expand All @@ -152,7 +163,7 @@ impl IFlate {
}

pub fn decoded(&self) -> Vec<u8> {
decode(&self.compressed, Some(CompressionMethodTy(self.algo)))
decode(&self.compressed, Some(self.algo))
}

pub fn decode_string(&self) -> Result<String, FromUtf8Error> {
Expand All @@ -164,23 +175,12 @@ impl IFlate {
}
}

#[derive(Debug)]
pub struct CompressionMethodTy(pub CompressionMethod);

impl Into<CompressionMethod> for CompressionMethodTy {
fn into(self) -> CompressionMethod {
self.0
}
}

#[doc(hidden)]
#[allow(private_interfaces)]
pub fn decode(bytes: &[u8], algo: Option<CompressionMethodTy>) -> Vec<u8> {
pub fn decode(bytes: &[u8], algo: Option<CompressionMethod>) -> Vec<u8> {
use std::io::Cursor;

let algo: CompressionMethod = algo
.unwrap_or(CompressionMethodTy(CompressionMethod::Deflate))
.into();
let algo: CompressionMethod = algo.unwrap_or_default().into();
let mut source = Cursor::new(bytes);
let mut ret = Vec::new();

Expand All @@ -194,7 +194,7 @@ pub fn decode(bytes: &[u8], algo: Option<CompressionMethodTy>) -> Vec<u8> {

#[doc(hidden)]
#[allow(private_interfaces)]
pub fn decode_string(bytes: &[u8], algo: Option<CompressionMethodTy>) -> String {
pub fn decode_string(bytes: &[u8], algo: Option<CompressionMethod>) -> String {
// We should have checked for utf8 correctness in encode_utf8_file!
String::from_utf8(decode(bytes, algo))
.expect("flate_str has malformed UTF-8 despite checked at compile time")
Expand Down