Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ keywords = ["compression", "deflate", "macro", "include", "assets"]
[dependencies]
include-flate-codegen-exports = { version = "0.1.4", path = "codegen-exports" }
lazy_static = "1.3"
libflate = "1.0.0"
libflate = { version = "2.0.0", default-features = false }
core2 = { version = "0.4", default-features = false, features = ["alloc"] }

[badges]
travis-ci = {repository = "SOF3/include-flate"}

[features]
default = ["std"]
stable = ["include-flate-codegen-exports/stable"]
std = ["include-flate-codegen-exports/std", "libflate/std", "core2/std"]
1 change: 1 addition & 0 deletions codegen-exports/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ proc-macro-hack = "0.5.9"

[features]
stable = ["include-flate-codegen/stable"]
std = []
16 changes: 16 additions & 0 deletions codegen-exports/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#![cfg_attr(not(any(test, feature = "std")), no_std)]

extern crate alloc;

#[cfg_attr(feature = "stable", proc_macro_hack::proc_macro_hack)]
pub use include_flate_codegen::deflate_file;

#[cfg_attr(feature = "stable", proc_macro_hack::proc_macro_hack)]
pub use include_flate_codegen::deflate_utf8_file;

#[cfg(feature = "std")]
pub type String = ::std::string::String;

#[cfg(not(feature = "std"))]
pub type String = ::alloc::string::String;

#[cfg(feature = "std")]
pub type Vec<T> = ::std::vec::Vec<T>;

#[cfg(not(feature = "std"))]
pub type Vec<T> = ::alloc::vec::Vec<T>;
2 changes: 1 addition & 1 deletion codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description = "Macro codegen for the include-flate crate"
proc-macro = true

[dependencies]
libflate = "1.0.0"
libflate = "2.0.0"
proc-macro-hack = { version = "0.5.9", optional = true }
proc-macro2 = "1.0.9"
quote = "1.0.2"
Expand Down
38 changes: 30 additions & 8 deletions codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use std::str::from_utf8;
use libflate::deflate::Encoder;
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{Error, LitByteStr, LitStr, Result};
use quote::{quote, ToTokens};
use syn::{Error, ExprTuple, LitByteStr, LitStr, Result};

/// `deflate_file!("file")` is equivalent to `include_bytes!("file.gz")`.
///
Expand Down Expand Up @@ -67,20 +67,42 @@ pub fn deflate_utf8_file(ts: TokenStream) -> TokenStream {
}

fn inner(ts: TokenStream, utf8: bool) -> Result<impl Into<TokenStream>> {
if let Ok(t) = syn::parse::<ExprTuple>(ts.clone()) {
Copy link
Owner

Choose a reason for hiding this comment

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

what about actually using a custom Parse implementation

Copy link
Owner

Choose a reason for hiding this comment

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

Why tuple? The invocation will then become deflate_file!(("file.gz", "base")) with double parentheses.

if t.elems.len() != 2 {
panic!("expected a tuple of size 2 only");
}
let (lit, base) = (
t.elems.first().unwrap().into_token_stream(),
t.elems.last().unwrap().into_token_stream(),
);
let (lit, base) = (
syn::parse::<LitStr>(lit.into())?,
Copy link
Owner

Choose a reason for hiding this comment

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

why parse again instead of just checking if let Expr::Let(litlit) = lit and let Lit::Str(litstr) = &litlit.lit?

syn::parse::<LitStr>(base.into())?,
);
let key = quote!(#base).to_string();
Copy link
Owner

Choose a reason for hiding this comment

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

Why not just get base.value()?

compress_file_as_tokenstream(PathBuf::from(lit.value()), &key[1..key.len() - 1], utf8)
} else if let Ok(lit) = syn::parse::<LitStr>(ts) {
compress_file_as_tokenstream(PathBuf::from(lit.value()), "CARGO_MANIFEST_DIR", utf8)
} else {
panic!("invalid pattern")
}
}

fn compress_file_as_tokenstream(
path: PathBuf,
key: &str,
utf8: bool,
) -> Result<impl Into<TokenStream>> {
fn emap<E: std::fmt::Display>(error: E) -> Error {
Error::new(Span::call_site(), error)
}

let dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());

let lit = syn::parse::<LitStr>(ts)?;
let path = PathBuf::from(lit.value());

if path.is_absolute() {
Err(emap("absolute paths are not supported"))?;
}

let target = dir.join(path);
let dir = PathBuf::from(std::env::var(key).unwrap());
let target: PathBuf = dir.join(path);

let mut file = File::open(target).map_err(emap)?;

Expand Down
38 changes: 31 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
//! which might be undesirable if the data are too large.
//! An actual installer is still required if the binary involves too many resources that do not need to be kept in RAM all time.

#![cfg_attr(not(any(test, feature = "std")), no_std)]

extern crate alloc;

use libflate::deflate;

/// The low-level macros used by this crate.
Expand Down Expand Up @@ -92,14 +96,34 @@ pub use lazy_static::lazy_static;
/// [6]: https://github.com/SOF3/include-flate/tree/master/tests
#[macro_export]
macro_rules! flate {
($(#[$meta:meta])*
$(pub $(($($vis:tt)+))?)? static $name:ident: [u8] from $path:literal on $base:literal) => {
// HACK: workaround to make cargo auto rebuild on modification of source file
const _: &'static [u8] = include_bytes!(concat!(env!($base), "/", $path));
$crate::lazy_static! {
$(#[$meta])*
$(pub $(($($vis)+))?)? static ref $name: $crate::codegen::Vec<u8> = $crate::decode($crate::codegen::deflate_file!(($path, $base)));
}
};

($(#[$meta:meta])*
$(pub $(($($vis:tt)+))?)? static $name:ident: [u8] from $path:literal) => {
// HACK: workaround to make cargo auto rebuild on modification of source file
const _: &'static [u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path));

$crate::lazy_static! {
$(#[$meta])*
$(pub $(($($vis)+))?)? static ref $name: ::std::vec::Vec<u8> = $crate::decode($crate::codegen::deflate_file!($path));
$(pub $(($($vis)+))?)? static ref $name: $crate::codegen::Vec<u8> = $crate::decode($crate::codegen::deflate_file!($path));
}
};
($(#[$meta:meta])*
$(pub $(($($vis:tt)+))?)? static $name:ident: str from $path:literal on $base:literal) => {
// HACK: workaround to make cargo auto rebuild on modification of source file
const _: &'static str = include_str!(concat!(env!($base), "/", $path));

$crate::lazy_static! {
$(#[$meta])*
$(pub $(($($vis)+))?)? static ref $name: $crate::codegen::String = $crate::decode_string($crate::codegen::deflate_utf8_file!(($path, $base)));
}
};
($(#[$meta:meta])*
Expand All @@ -109,25 +133,25 @@ macro_rules! flate {

$crate::lazy_static! {
$(#[$meta])*
$(pub $(($($vis)+))?)? static ref $name: ::std::string::String = $crate::decode_string($crate::codegen::deflate_utf8_file!($path));
$(pub $(($($vis)+))?)? static ref $name: $crate::codegen::String = $crate::decode_string($crate::codegen::deflate_utf8_file!($path));
}
};
}

#[doc(hidden)]
pub fn decode(bytes: &[u8]) -> Vec<u8> {
use std::io::{Cursor, Read};
pub fn decode(bytes: &[u8]) -> codegen::Vec<u8> {
use core2::io::{Cursor, Read};

let mut dec = deflate::Decoder::new(Cursor::new(bytes));
let mut ret = Vec::new();
let mut ret = codegen::Vec::new();
dec.read_to_end(&mut ret)
.expect("Compiled DEFLATE buffer was corrupted");
ret
}

#[doc(hidden)]
pub fn decode_string(bytes: &[u8]) -> String {
pub fn decode_string(bytes: &[u8]) -> codegen::String {
// We should have checked for utf8 correctness in encode_utf8_file!
String::from_utf8(decode(bytes))
codegen::String::from_utf8(decode(bytes))
.expect("flate_str has malformed UTF-8 despite checked at compile time")
}