mirror of
https://github.com/RGBCube/embd-rs
synced 2025-07-26 21:17:44 +00:00
Fix relativity and macros
This commit is contained in:
parent
c8472c50dc
commit
77520c0364
4 changed files with 49 additions and 25 deletions
|
@ -58,8 +58,11 @@ fn read_dir(path: &PathBuf) -> Vec<DirEntry> {
|
|||
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
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
// }
|
||||
|
|
|
@ -3,3 +3,7 @@ pub use dir::*;
|
|||
|
||||
mod file;
|
||||
pub use file::*;
|
||||
|
||||
// fn expand() {
|
||||
// dir!("")
|
||||
// }
|
||||
|
|
|
@ -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<Self> {
|
||||
let caller = input.parse::<LitStr>()?.value();
|
||||
|
||||
input.parse::<Token![,]>()?;
|
||||
|
||||
let path = input.parse::<LitStr>()?.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<TokenStream> {
|
||||
fn read_dir(base: &PathBuf, path: &PathBuf) -> Vec<TokenStream> {
|
||||
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<TokenStream> {
|
|||
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<TokenStream> {
|
|||
.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<TokenStream> {
|
|||
} 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),
|
||||
})
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue