1
Fork 0
mirror of https://github.com/RGBCube/embd-rs synced 2025-07-27 05:27:44 +00:00

Fix relativity and macros

This commit is contained in:
RGBCube 2024-01-04 12:47:19 +03:00
parent c8472c50dc
commit 77520c0364
No known key found for this signature in database
4 changed files with 49 additions and 25 deletions

View file

@ -58,8 +58,11 @@ fn read_dir(path: &PathBuf) -> Vec<DirEntry> {
entries entries
} }
pub fn __include_dir(path: &str) -> Dir { pub fn __include_dir(caller: &str, path: &str) -> Dir {
let path = PathBuf::from(path); let path = PathBuf::from(caller)
.parent()
.expect("Failed to get the parent of file")
.join(path);
let children = read_dir(&path); let children = read_dir(&path);
@ -71,11 +74,11 @@ macro_rules! dir {
($path:literal) => {{ ($path:literal) => {{
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
::embed::__include_dir($path) ::embed::__include_dir(file!(), $path)
} }
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
{ {
::embed_macros::__include_dir!($path) ::embed_macros::__include_dir!(file!(), $path) // FIXME
} }
}}; }};
} }

View file

@ -12,12 +12,12 @@ macro_rules! string {
let file = Path::new(file!()) let file = Path::new(file!())
.parent() .parent()
.expect("embed: file has no parent") .expect("embed: Failed to get the parent of file")
.join($path); .join($path);
Cow::<'static, str>::Owned(fs::read_to_string(&file).unwrap_or_else(|error| { Cow::<'static, str>::Owned(fs::read_to_string(&file).unwrap_or_else(|error| {
panic!( panic!(
"embed: failed to read file {file}: {error}", "embed: Failed to read file {file}: {error}",
file = file.display() file = file.display()
) )
})) }))
@ -43,7 +43,7 @@ macro_rules! bytes {
let file = Path::new(file!()) let file = Path::new(file!())
.parent() .parent()
.expect("embed: file has no parent") .expect("embed: Failed to get the parent of file")
.join($path); .join($path);
Cow::<'static, [u8]>::Owned(fs::read(&file).unwrap_or_else(|error| { 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");
// }

View file

@ -3,3 +3,7 @@ pub use dir::*;
mod file; mod file;
pub use file::*; pub use file::*;
// fn expand() {
// dir!("")
// }

View file

@ -7,21 +7,42 @@ use proc_macro as pm1;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::quote; use quote::quote;
use syn::{ use syn::{
parse::{
Parse,
ParseStream,
},
parse_macro_input, parse_macro_input,
LitStr, LitStr,
Token,
}; };
struct TwoStrArgs {
caller: String,
path: String,
}
impl Parse for TwoStrArgs {
fn parse(input: ParseStream) -> syn::Result<Self> {
let caller = input.parse::<LitStr>()?.value();
input.parse::<Token![,]>()?;
let path = input.parse::<LitStr>()?.value();
Ok(Self { caller, path })
}
}
#[proc_macro] #[proc_macro]
pub fn __include_dir(tokens: pm1::TokenStream) -> pm1::TokenStream { pub fn __include_dir(input: pm1::TokenStream) -> pm1::TokenStream {
let path = parse_macro_input!(tokens as LitStr).value(); 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 let children = read_dir(&path, &path);
.to_str()
.expect("Failed to get the string representation of PathBuf");
let children = read_dir(&path);
let children_tokens = quote! { let children_tokens = quote! {
vec![#(#children),*] vec![#(#children),*]
}; };
@ -29,13 +50,13 @@ pub fn __include_dir(tokens: pm1::TokenStream) -> pm1::TokenStream {
(quote! { (quote! {
::embed::Dir { ::embed::Dir {
children: #children_tokens, children: #children_tokens,
path: ::std::path::PathBuf::from(#path_str), path: ::std::path::PathBuf::from(""),
} }
}) })
.into() .into()
} }
fn read_dir(path: &PathBuf) -> Vec<TokenStream> { fn read_dir(base: &PathBuf, path: &PathBuf) -> Vec<TokenStream> {
let mut entries = Vec::new(); let mut entries = Vec::new();
for entry in fs::read_dir(path).expect("Failed to list directory contents") { for entry in fs::read_dir(path).expect("Failed to list directory contents") {
@ -44,6 +65,8 @@ fn read_dir(path: &PathBuf) -> Vec<TokenStream> {
let path = entry.path(); let path = entry.path();
let path_str = path let path_str = path
.strip_prefix(base)
.expect("Failed to strip prefix of path")
.to_str() .to_str()
.expect("Failed to get the string representation of PathBuf"); .expect("Failed to get the string representation of PathBuf");
@ -52,7 +75,7 @@ fn read_dir(path: &PathBuf) -> Vec<TokenStream> {
.file_type(); .file_type();
if filetype.is_dir() { if filetype.is_dir() {
let children = read_dir(&path); let children = read_dir(base, &path);
let children_tokens = quote! { let children_tokens = quote! {
vec![#(#children),*] vec![#(#children),*]
}; };
@ -66,7 +89,7 @@ fn read_dir(path: &PathBuf) -> Vec<TokenStream> {
} else if filetype.is_file() { } else if filetype.is_file() {
entries.push(quote! { entries.push(quote! {
::embed::DirEntry(::embed::File { ::embed::DirEntry(::embed::File {
content: ::include_bytes!(#path_str), content: include_bytes!(#path_str),
path: ::std::path::PathBuf::from(#path_str), path: ::std::path::PathBuf::from(#path_str),
}) })
}); });