mirror of
https://github.com/RGBCube/embd-rs
synced 2025-07-27 05:27:44 +00:00
YOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
This commit is contained in:
parent
6c33b6177d
commit
907051343d
2 changed files with 60 additions and 37 deletions
|
@ -9,6 +9,8 @@ use std::{
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub use embed_macros::__dir as dir;
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn __string_runtime(neighbor: &str, path: &str) -> String {
|
pub fn __string_runtime(neighbor: &str, path: &str) -> String {
|
||||||
let base = Path::new(neighbor)
|
let base = Path::new(neighbor)
|
||||||
|
@ -125,13 +127,6 @@ pub struct File {
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl File {
|
|
||||||
/// Returns the content of the file as a String if it is valid UTF-8.
|
|
||||||
pub fn content_string(&self) -> Option<String> {
|
|
||||||
String::from_utf8(&self.content).ok()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_dir(path: &PathBuf) -> Vec<DirEntry> {
|
fn read_dir(path: &PathBuf) -> Vec<DirEntry> {
|
||||||
let mut entries = Vec::new();
|
let mut entries = Vec::new();
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,11 @@
|
||||||
#![cfg(procmacro2_semver_exempt)]
|
#![cfg(procmacro2_semver_exempt)]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
env,
|
|
||||||
fs,
|
fs,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
use proc_macro::{
|
use proc_macro as pm1;
|
||||||
self as pm1,
|
|
||||||
TokenStream,
|
|
||||||
};
|
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::{
|
use quote::{
|
||||||
quote,
|
quote,
|
||||||
|
@ -21,46 +17,74 @@ use syn::{
|
||||||
LitStr,
|
LitStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl ToTokens for PathBuf {
|
struct PathBuf2(PathBuf);
|
||||||
|
|
||||||
|
impl ToTokens for PathBuf2 {
|
||||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||||
let raw = self
|
let raw = self
|
||||||
.as_str()
|
.0
|
||||||
|
.to_str()
|
||||||
.expect("Failed to get the string representation of PathBuf");
|
.expect("Failed to get the string representation of PathBuf");
|
||||||
|
|
||||||
tokens.append(quote! {
|
tokens.extend(quote! {
|
||||||
::std::path::PathBuf::from(#raw)
|
::std::path::PathBuf::from(#raw)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToTokens for Vec<TokenStream> {
|
struct TokenVec(Vec<TokenStream>);
|
||||||
|
|
||||||
|
impl ToTokens for TokenVec {
|
||||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||||
tokens.append(quote! {
|
let inner = &self.0;
|
||||||
vec![#(#self),*]
|
|
||||||
|
tokens.extend(quote! {
|
||||||
|
vec![#(#inner),*]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dir_debug(input: pm1::TokenStream) -> pm1::TokenStream {
|
#[proc_macro]
|
||||||
let path = parse_macro_input!(input as LitStr).value();
|
pub fn __dir(input: pm1::TokenStream) -> pm1::TokenStream {
|
||||||
|
let input2 = input.clone();
|
||||||
|
let path = parse_macro_input!(input2 as LitStr).value();
|
||||||
|
|
||||||
|
let debug = dir_debug(&path);
|
||||||
|
let release = dir_release(input.into(), &path);
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
::embed::__include_dir_runtime(file!(), #path)
|
{
|
||||||
};
|
#[cfg(debug_assertions)]
|
||||||
|
{
|
||||||
|
#debug
|
||||||
|
}
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
{
|
||||||
|
#release
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dir_release(input: pm1::TokenStream) -> pm1::TokenStream {
|
fn dir_debug(path: &str) -> TokenStream {
|
||||||
let neigbor = TokenStream::from(input.clone()).span().source_file().path();
|
quote! {
|
||||||
let path = parse_macro_input!(input as LitStr).value();
|
::embed::__dir_runtime(file!(), #path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dir_release(input: TokenStream, path: &str) -> TokenStream {
|
||||||
|
let neighbor = TokenStream::from(input).span().source_file().path();
|
||||||
|
|
||||||
let base = neighbor.parent().expect("Failed to get the parent of file");
|
let base = neighbor.parent().expect("Failed to get the parent of file");
|
||||||
|
|
||||||
let directory = base
|
let directory = PathBuf2(
|
||||||
.join(path)
|
base.join(path)
|
||||||
.canonicalize()
|
.canonicalize()
|
||||||
.expect("Failed to canonicalize path");
|
.expect("Failed to canonicalize path"),
|
||||||
|
);
|
||||||
|
|
||||||
let children = read_dir(&directory);
|
let children = read_dir(&directory.0);
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
::embed::Dir {
|
::embed::Dir {
|
||||||
|
@ -68,23 +92,22 @@ pub fn dir_release(input: pm1::TokenStream) -> pm1::TokenStream {
|
||||||
path: #directory,
|
path: #directory,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.into()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_dir(directory: &PathBuf) -> Vec<DirEntry> {
|
fn read_dir(directory: &PathBuf) -> TokenVec {
|
||||||
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(directory).expect("Failed to list directory contents") {
|
||||||
let entry = entry.expect("Failed to read entry");
|
let entry = entry.expect("Failed to read entry");
|
||||||
|
|
||||||
let path = entry.path();
|
let path = PathBuf2(entry.path());
|
||||||
|
|
||||||
let filetype = fs::metadata(&path)
|
let filetype = fs::metadata(&path.0)
|
||||||
.expect("Failed to get file metadata")
|
.expect("Failed to get file metadata")
|
||||||
.file_type();
|
.file_type();
|
||||||
|
|
||||||
if filetype.is_dir() {
|
if filetype.is_dir() {
|
||||||
let children = read_dir(&path);
|
let children = read_dir(&path.0);
|
||||||
|
|
||||||
entries.push(quote! {
|
entries.push(quote! {
|
||||||
::embed::DirEntry::Dir(::embed::Dir {
|
::embed::DirEntry::Dir(::embed::Dir {
|
||||||
|
@ -93,6 +116,11 @@ fn read_dir(directory: &PathBuf) -> Vec<DirEntry> {
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
} else if filetype.is_file() {
|
} else if filetype.is_file() {
|
||||||
|
let path_str = path
|
||||||
|
.0
|
||||||
|
.to_str()
|
||||||
|
.expect("Failed to get the string representation of PathBuf");
|
||||||
|
|
||||||
entries.push(quote! {
|
entries.push(quote! {
|
||||||
::embed::DirEntry::File(::embed::File {
|
::embed::DirEntry::File(::embed::File {
|
||||||
content: ::std::borrow::Cow::Borrowed(include_bytes!(#path_str)),
|
content: ::std::borrow::Cow::Borrowed(include_bytes!(#path_str)),
|
||||||
|
@ -102,5 +130,5 @@ fn read_dir(directory: &PathBuf) -> Vec<DirEntry> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
entries
|
TokenVec(entries)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue