mirror of
https://github.com/RGBCube/embd-rs
synced 2025-07-25 20:47:45 +00:00
Use SourceFile
This commit is contained in:
parent
d3c1b3e590
commit
55390702a5
3 changed files with 16 additions and 37 deletions
|
@ -5,11 +5,13 @@ use std::{
|
|||
path::PathBuf,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum DirEntry {
|
||||
Dir(Dir),
|
||||
File(File),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Dir {
|
||||
pub children: Vec<DirEntry>,
|
||||
pub path: PathBuf,
|
||||
|
@ -30,6 +32,7 @@ impl Dir {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct File {
|
||||
pub content: Vec<u8>,
|
||||
pub path: PathBuf,
|
||||
|
@ -70,22 +73,15 @@ pub fn __include_dir(caller: &str, path: &str) -> Dir {
|
|||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! __dir {
|
||||
($caller:literal, $path:literal) => {{
|
||||
macro_rules! dir {
|
||||
($path:literal) => {{
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
::embed::__include_dir($caller, $path)
|
||||
::embed::__include_dir(file!(), $path)
|
||||
}
|
||||
#[cfg(not(debug_assertions))]
|
||||
{
|
||||
::embed_macros::__include_dir!($caller, $path)
|
||||
::embed_macros::__include_dir!($path)
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! dir {
|
||||
($path:literal) => {
|
||||
::embed::__dir!(file!(), $path)
|
||||
};
|
||||
}
|
||||
|
|
|
@ -13,6 +13,6 @@ edition = "2021"
|
|||
proc_macro = true
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = "1"
|
||||
proc-macro2 = { version = "1", features = [ "span-locations" ] }
|
||||
quote = "1"
|
||||
syn = "2"
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![cfg(procmacro2_semver_exempt)]
|
||||
|
||||
use std::{
|
||||
fs,
|
||||
path::PathBuf,
|
||||
|
@ -7,38 +9,19 @@ use proc_macro as pm1;
|
|||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{
|
||||
parse::{
|
||||
Parse,
|
||||
ParseStream,
|
||||
},
|
||||
parse_macro_input,
|
||||
spanned::Spanned,
|
||||
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(input: pm1::TokenStream) -> pm1::TokenStream {
|
||||
let input2 = input.clone();
|
||||
let TwoStrArgs { caller, path } = parse_macro_input!(input2 as TwoStrArgs);
|
||||
let caller = TokenStream::from(input).span().source_file().path();
|
||||
|
||||
let path = PathBuf::from(caller)
|
||||
let input2 = input.clone();
|
||||
let path = parse_macro_input!(input2 as LitStr).value();
|
||||
|
||||
let path = caller
|
||||
.parent()
|
||||
.expect("Failed to get the parent of file")
|
||||
.join(path);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue