mirror of
https://github.com/RGBCube/embd-rs
synced 2025-07-26 21:17:44 +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,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub enum DirEntry {
|
pub enum DirEntry {
|
||||||
Dir(Dir),
|
Dir(Dir),
|
||||||
File(File),
|
File(File),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub struct Dir {
|
pub struct Dir {
|
||||||
pub children: Vec<DirEntry>,
|
pub children: Vec<DirEntry>,
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
|
@ -30,6 +32,7 @@ impl Dir {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub struct File {
|
pub struct File {
|
||||||
pub content: Vec<u8>,
|
pub content: Vec<u8>,
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
|
@ -70,22 +73,15 @@ pub fn __include_dir(caller: &str, path: &str) -> Dir {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! __dir {
|
macro_rules! dir {
|
||||||
($caller:literal, $path:literal) => {{
|
($path:literal) => {{
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
{
|
{
|
||||||
::embed::__include_dir($caller, $path)
|
::embed::__include_dir(file!(), $path)
|
||||||
}
|
}
|
||||||
#[cfg(not(debug_assertions))]
|
#[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
|
proc_macro = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
proc-macro2 = "1"
|
proc-macro2 = { version = "1", features = [ "span-locations" ] }
|
||||||
quote = "1"
|
quote = "1"
|
||||||
syn = "2"
|
syn = "2"
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![cfg(procmacro2_semver_exempt)]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fs,
|
fs,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
|
@ -7,38 +9,19 @@ 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,
|
||||||
|
spanned::Spanned,
|
||||||
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(input: pm1::TokenStream) -> pm1::TokenStream {
|
pub fn __include_dir(input: pm1::TokenStream) -> pm1::TokenStream {
|
||||||
let input2 = input.clone();
|
let caller = TokenStream::from(input).span().source_file().path();
|
||||||
let TwoStrArgs { caller, path } = parse_macro_input!(input2 as TwoStrArgs);
|
|
||||||
|
|
||||||
let path = PathBuf::from(caller)
|
let input2 = input.clone();
|
||||||
|
let path = parse_macro_input!(input2 as LitStr).value();
|
||||||
|
|
||||||
|
let path = caller
|
||||||
.parent()
|
.parent()
|
||||||
.expect("Failed to get the parent of file")
|
.expect("Failed to get the parent of file")
|
||||||
.join(path);
|
.join(path);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue