mirror of
https://github.com/RGBCube/embd-rs
synced 2025-07-26 13:07:47 +00:00
Rename to embd
This commit is contained in:
parent
7c2eb614db
commit
53f550c9c0
10 changed files with 86 additions and 91 deletions
18
embd-macros/Cargo.toml
Normal file
18
embd-macros/Cargo.toml
Normal file
|
@ -0,0 +1,18 @@
|
|||
[package]
|
||||
name = "embd-macros"
|
||||
description = "Read files or directories from the filesystem at runtime on debug, embed on release."
|
||||
repository = "https://github.com/RGBCube/embd-rs"
|
||||
license = "MIT"
|
||||
keywords = [ "embedding", "files", "debug-optimization", "bundling" ]
|
||||
categories = [ "filesystem" ]
|
||||
authors = [ "RGBCube" ]
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = { version = "1", features = [ "span-locations" ] }
|
||||
quote = "1"
|
||||
syn = "2"
|
118
embd-macros/src/lib.rs
Normal file
118
embd-macros/src/lib.rs
Normal file
|
@ -0,0 +1,118 @@
|
|||
#![cfg(feature = "procmacro2_semver_exempt")]
|
||||
use std::{
|
||||
fs,
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use proc_macro as pm1;
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{
|
||||
quote,
|
||||
ToTokens,
|
||||
};
|
||||
use syn::{
|
||||
parse_macro_input,
|
||||
spanned::Spanned,
|
||||
LitStr,
|
||||
};
|
||||
|
||||
struct TokenVec(Vec<TokenStream>);
|
||||
|
||||
impl ToTokens for TokenVec {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
let inner = &self.0;
|
||||
|
||||
tokens.extend(quote! {
|
||||
::std::borrow::Cow::Borrowed(&[#(#inner),*])
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
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! {
|
||||
{
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
#debug
|
||||
}
|
||||
#[cfg(not(debug_assertions))]
|
||||
{
|
||||
#release
|
||||
}
|
||||
}
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
fn dir_debug(path: &str) -> TokenStream {
|
||||
quote! {
|
||||
::embd::__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 directory = base
|
||||
.join(path)
|
||||
.canonicalize()
|
||||
.expect("Failed to canonicalize path");
|
||||
|
||||
let directory_str = directory.to_str().expect("Failed to convert OsStr to str");
|
||||
|
||||
let children = read_dir(&directory);
|
||||
|
||||
quote! {
|
||||
::embd::Dir {
|
||||
__children: #children,
|
||||
__path: ::std::borrow::Cow::Borrowed(#directory_str),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_dir(directory: &Path) -> TokenVec {
|
||||
let mut entries = Vec::new();
|
||||
|
||||
for entry in fs::read_dir(directory).expect("Failed to list directory contents") {
|
||||
let entry = entry.expect("Failed to read entry");
|
||||
|
||||
let path = entry.path();
|
||||
|
||||
let path_str = path
|
||||
.to_str()
|
||||
.expect("Failed to get the string representation of PathBuf");
|
||||
|
||||
let filetype = fs::metadata(&path)
|
||||
.expect("Failed to get file metadata")
|
||||
.file_type();
|
||||
|
||||
if filetype.is_dir() {
|
||||
let children = read_dir(&path);
|
||||
|
||||
entries.push(quote! {
|
||||
::embd::DirEntry::Dir(::embd::Dir {
|
||||
__children: #children,
|
||||
__path: ::std::borrow::Cow::Borrowed(#path_str),
|
||||
})
|
||||
});
|
||||
} else if filetype.is_file() {
|
||||
entries.push(quote! {
|
||||
::embd::DirEntry::File(::embd::File {
|
||||
__content: ::std::borrow::Cow::Borrowed(include_bytes!(#path_str)),
|
||||
__path: ::std::borrow::Cow::Borrowed(#path_str),
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
TokenVec(entries)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue