Skip to content

Commit

Permalink
Reintroduce include_bytes! macro in expanded code
Browse files Browse the repository at this point in the history
  • Loading branch information
zombiepigdragon committed Aug 5, 2022
1 parent 0536fd6 commit 898c4a0
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,17 @@ fn expand_dir(root: &Path, path: &Path) -> proc_macro2::TokenStream {
}

fn expand_file(root: &Path, path: &Path) -> proc_macro2::TokenStream {
let contents = read_file(path);
let literal = Literal::byte_string(&contents);
let abs = path
.canonicalize()
.unwrap_or_else(|e| panic!("failed to resolve \"{}\": {}", path.display(), e));
let literal = match abs.to_str() {
Some(abs) => quote!(include_bytes!(#abs)),
None => {
let contents = read_file(path);
let literal = Literal::byte_string(&contents);
quote!(#literal)
}
};

let normalized_path = normalize_path(root, path);

Expand Down Expand Up @@ -285,7 +294,7 @@ mod tests {

let resolved = resolve_path(path, |name| match name {
"TOP_LEVEL" => Some("$NESTED".to_string()),
"$NESTED" => unreachable!("Shouln't resolve recursively"),
"$NESTED" => unreachable!("Shouldn't resolve recursively"),
_ => unreachable!(),
})
.unwrap();
Expand Down

0 comments on commit 898c4a0

Please sign in to comment.