From 41e02a9b3b8af8bd9eea241587142cb7a47872f9 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Thu, 4 Jan 2024 13:29:42 +0300 Subject: [PATCH] Add error message for trying to embed . --- embed/src/dir.rs | 1 + embed/src/lib.rs | 2 +- macros/src/lib.rs | 19 +++++++++++++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/embed/src/dir.rs b/embed/src/dir.rs index a86cc04..dc4bdab 100644 --- a/embed/src/dir.rs +++ b/embed/src/dir.rs @@ -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) } }}; } diff --git a/embed/src/lib.rs b/embed/src/lib.rs index fcffd27..43a4553 100644 --- a/embed/src/lib.rs +++ b/embed/src/lib.rs @@ -5,5 +5,5 @@ mod file; pub use file::*; // fn expand() { -// dir!("") +// dir!(".") // } diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 4fcd9a0..9cb461f 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -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 { entries.push(quote! { ::embed::DirEntry(::embed::File { content: include_bytes!(#path_str), + // content: include_bytes(#path_str), path: ::std::path::PathBuf::from(#path_str), }) });