mirror of
https://github.com/RGBCube/embd-rs
synced 2025-07-27 05:27:44 +00:00
Add error message for trying to embed .
This commit is contained in:
parent
2b624eb362
commit
41e02a9b3b
3 changed files with 17 additions and 5 deletions
|
@ -79,6 +79,7 @@ macro_rules! dir {
|
|||
#[cfg(not(debug_assertions))]
|
||||
{
|
||||
::embed_macros::__include_dir!(file!(), $path) // FIXME
|
||||
// ::embed_macros::__include_dir!("embed/src/lib.rs", $path)
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
|
|
@ -5,5 +5,5 @@ mod file;
|
|||
pub use file::*;
|
||||
|
||||
// fn expand() {
|
||||
// dir!("")
|
||||
// dir!(".")
|
||||
// }
|
||||
|
|
|
@ -35,23 +35,33 @@ impl Parse for TwoStrArgs {
|
|||
|
||||
#[proc_macro]
|
||||
pub fn __include_dir(input: pm1::TokenStream) -> pm1::TokenStream {
|
||||
let TwoStrArgs { caller, path } = parse_macro_input!(input as TwoStrArgs);
|
||||
let input2 = input.clone();
|
||||
let TwoStrArgs { caller, path } = parse_macro_input!(input2 as TwoStrArgs);
|
||||
|
||||
let path = PathBuf::from(caller)
|
||||
.parent()
|
||||
.expect("Failed to get the parent of file")
|
||||
.join(path);
|
||||
|
||||
let path = if !path.ends_with("..") {
|
||||
path
|
||||
} else {
|
||||
let path = if path.ends_with("..") {
|
||||
path.parent().unwrap().to_path_buf()
|
||||
} else {
|
||||
path
|
||||
};
|
||||
|
||||
let path_str = path
|
||||
.to_str()
|
||||
.expect("Failed to get the string representation of PathBuf");
|
||||
|
||||
if path_str.ends_with(".") {
|
||||
return syn::Error::new_spanned(
|
||||
TokenStream::from(input),
|
||||
"Can't embed current file as it is not a directory",
|
||||
)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
}
|
||||
|
||||
let children = read_dir(&path, &path);
|
||||
let children_tokens = quote! {
|
||||
vec![#(#children),*]
|
||||
|
@ -100,6 +110,7 @@ fn read_dir(base: &PathBuf, path: &PathBuf) -> Vec<TokenStream> {
|
|||
entries.push(quote! {
|
||||
::embed::DirEntry(::embed::File {
|
||||
content: include_bytes!(#path_str),
|
||||
// content: include_bytes(#path_str),
|
||||
path: ::std::path::PathBuf::from(#path_str),
|
||||
})
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue