From 77520c0364525b54e5eee73adb69940cf517d1f9 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Thu, 4 Jan 2024 12:47:19 +0300 Subject: [PATCH] Fix relativity and macros --- embed/src/dir.rs | 11 +++++++---- embed/src/file.rs | 12 +++--------- embed/src/lib.rs | 4 ++++ macros/src/lib.rs | 47 +++++++++++++++++++++++++++++++++++------------ 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/embed/src/dir.rs b/embed/src/dir.rs index 9301fcf..a86cc04 100644 --- a/embed/src/dir.rs +++ b/embed/src/dir.rs @@ -58,8 +58,11 @@ fn read_dir(path: &PathBuf) -> Vec { entries } -pub fn __include_dir(path: &str) -> Dir { - let path = PathBuf::from(path); +pub fn __include_dir(caller: &str, path: &str) -> Dir { + let path = PathBuf::from(caller) + .parent() + .expect("Failed to get the parent of file") + .join(path); let children = read_dir(&path); @@ -71,11 +74,11 @@ macro_rules! dir { ($path:literal) => {{ #[cfg(debug_assertions)] { - ::embed::__include_dir($path) + ::embed::__include_dir(file!(), $path) } #[cfg(not(debug_assertions))] { - ::embed_macros::__include_dir!($path) + ::embed_macros::__include_dir!(file!(), $path) // FIXME } }}; } diff --git a/embed/src/file.rs b/embed/src/file.rs index 4ddd0e8..3748076 100644 --- a/embed/src/file.rs +++ b/embed/src/file.rs @@ -12,12 +12,12 @@ macro_rules! string { let file = Path::new(file!()) .parent() - .expect("embed: file has no parent") + .expect("embed: Failed to get the parent of file") .join($path); Cow::<'static, str>::Owned(fs::read_to_string(&file).unwrap_or_else(|error| { panic!( - "embed: failed to read file {file}: {error}", + "embed: Failed to read file {file}: {error}", file = file.display() ) })) @@ -43,7 +43,7 @@ macro_rules! bytes { let file = Path::new(file!()) .parent() - .expect("embed: file has no parent") + .expect("embed: Failed to get the parent of file") .join($path); Cow::<'static, [u8]>::Owned(fs::read(&file).unwrap_or_else(|error| { @@ -59,9 +59,3 @@ macro_rules! bytes { } }}; } - -// #[allow(dead_code)] -// fn check_validity() { -// string!("test.txt"); -// bytes!("test.txt"); -// } diff --git a/embed/src/lib.rs b/embed/src/lib.rs index 54d51d6..fcffd27 100644 --- a/embed/src/lib.rs +++ b/embed/src/lib.rs @@ -3,3 +3,7 @@ pub use dir::*; mod file; pub use file::*; + +// fn expand() { +// dir!("") +// } diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 22cd623..534bb58 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -7,21 +7,42 @@ use proc_macro as pm1; use proc_macro2::TokenStream; use quote::quote; use syn::{ + parse::{ + Parse, + ParseStream, + }, parse_macro_input, LitStr, + Token, }; +struct TwoStrArgs { + caller: String, + path: String, +} + +impl Parse for TwoStrArgs { + fn parse(input: ParseStream) -> syn::Result { + let caller = input.parse::()?.value(); + + input.parse::()?; + + let path = input.parse::()?.value(); + + Ok(Self { caller, path }) + } +} + #[proc_macro] -pub fn __include_dir(tokens: pm1::TokenStream) -> pm1::TokenStream { - let path = parse_macro_input!(tokens as LitStr).value(); +pub fn __include_dir(input: pm1::TokenStream) -> pm1::TokenStream { + let TwoStrArgs { caller, path } = parse_macro_input!(input as TwoStrArgs); - let path = PathBuf::from(path); + let path = PathBuf::from(caller) + .parent() + .expect("Failed to get the parent of file") + .join(path); - let path_str = path - .to_str() - .expect("Failed to get the string representation of PathBuf"); - - let children = read_dir(&path); + let children = read_dir(&path, &path); let children_tokens = quote! { vec![#(#children),*] }; @@ -29,13 +50,13 @@ pub fn __include_dir(tokens: pm1::TokenStream) -> pm1::TokenStream { (quote! { ::embed::Dir { children: #children_tokens, - path: ::std::path::PathBuf::from(#path_str), + path: ::std::path::PathBuf::from(""), } }) .into() } -fn read_dir(path: &PathBuf) -> Vec { +fn read_dir(base: &PathBuf, path: &PathBuf) -> Vec { let mut entries = Vec::new(); for entry in fs::read_dir(path).expect("Failed to list directory contents") { @@ -44,6 +65,8 @@ fn read_dir(path: &PathBuf) -> Vec { let path = entry.path(); let path_str = path + .strip_prefix(base) + .expect("Failed to strip prefix of path") .to_str() .expect("Failed to get the string representation of PathBuf"); @@ -52,7 +75,7 @@ fn read_dir(path: &PathBuf) -> Vec { .file_type(); if filetype.is_dir() { - let children = read_dir(&path); + let children = read_dir(base, &path); let children_tokens = quote! { vec![#(#children),*] }; @@ -66,7 +89,7 @@ fn read_dir(path: &PathBuf) -> Vec { } else if filetype.is_file() { entries.push(quote! { ::embed::DirEntry(::embed::File { - content: ::include_bytes!(#path_str), + content: include_bytes!(#path_str), path: ::std::path::PathBuf::from(#path_str), }) });