1extern crate proc_macro;
4use proc_macro::TokenStream;
5use quote::{quote, quote_spanned};
6use std::fs;
7use std::path::Path;
8use syn::{
9 parse::{Parse, ParseStream},
10 parse_macro_input, LitStr, Token,
11};
12use walkdir::WalkDir;
13
14type EmbedMeta = (String, String, usize, u64);
15type CollectResult = (Vec<EmbedMeta>, Vec<proc_macro2::TokenStream>);
16
17struct SiloMacroInput {
20 path: LitStr,
21 force: Option<(syn::Ident, syn::LitBool)>,
22 crate_path: Option<syn::Path>,
23}
24
25impl Parse for SiloMacroInput {
27 fn parse(input: ParseStream) -> syn::Result<Self> {
28 let path: LitStr = input.parse()?;
29 let mut force = None;
30 let mut crate_path = None;
31 while input.peek(Token![,]) {
32 input.parse::<Token![,]>()?;
33 let ident: syn::Ident = input.parse()?;
34 input.parse::<Token![=]>()?;
35 if ident == "force" {
36 let value: syn::LitBool = input.parse()?;
37 force = Some((ident, value));
38 } else if ident == "crate" {
39 let path: syn::Path = input.parse()?;
40 crate_path = Some(path);
41 } else {
42 return Err(syn::Error::new(ident.span(), "Unknown argument to embed_silo!"));
43 }
44 }
45 Ok(SiloMacroInput { path, force, crate_path })
46 }
47}
48
49#[proc_macro]
55pub fn embed_silo(input: TokenStream) -> TokenStream {
56 let SiloMacroInput { path, force, crate_path } = parse_macro_input!(input as SiloMacroInput);
57 let dir_path = path.value();
58 let call_span = path.span();
59 let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| String::new());
60 if manifest_dir.is_empty() {
61 return compile_error("embed_silo!: CARGO_MANIFEST_DIR not set", call_span);
62 }
63 let manifest_dir_canon = match Path::new(&manifest_dir).canonicalize() {
64 Ok(p) => p,
65 Err(_) => return compile_error("embed_silo!: failed to resolve CARGO_MANIFEST_DIR", call_span),
66 };
67
68 let abs_path = manifest_dir_canon.join(&dir_path);
69 let abs_path = match abs_path.canonicalize() {
70 Ok(p) => p,
71 Err(_) => {
72 return compile_error(
73 format!("embed_silo!: failed to resolve path: {}", dir_path),
74 call_span,
75 )
76 }
77 };
78 let abs_path_str = match abs_path.to_str() {
79 Some(p) => p,
80 None => return compile_error("embed_silo!: path must be valid UTF-8", call_span),
81 };
82
83 if !abs_path.starts_with(&manifest_dir_canon) {
85 let msg = format!(
86 "embed_silo!: directory not found:\n {}\n expected to be inside crate root:\n {}\n relative path: {}",
87 abs_path_str,
88 manifest_dir_canon.display(),
89 dir_path
90 );
91 return compile_error(&msg, call_span);
92 }
93
94 let force_embed = force.as_ref().is_some_and(|(_, v)| v.value());
95 let debug = cfg!(debug_assertions);
96 let use_embed = force_embed || !debug;
97 let crate_root = crate_path
98 .map(|p| quote! { #p })
99 .unwrap_or_else(|| quote! { ::rust_silos });
100
101 let abs_root_lit = syn::LitStr::new(abs_path_str, call_span);
103 if use_embed {
104 let (entries, errors) = collect_embed_entries(abs_path_str, call_span);
106 if !errors.is_empty() {
107 return quote! { #(#errors)* }.into();
108 }
109 let phf_pairs = generate_phf_map(&entries, &crate_root);
110 let mut hasher = std::collections::hash_map::DefaultHasher::new();
112 use std::hash::{Hash, Hasher};
113 abs_path_str.hash(&mut hasher);
114 let hash = hasher.finish();
115 let map_ident = quote::format_ident!("__EMBED_MAP_{:x}", hash);
116 let expanded = quote! {
117 {
118 static #map_ident: #crate_root::phf::Map<&'static str, #crate_root::EmbedEntry> = #crate_root::phf::phf_map! {
119 #phf_pairs
120 };
121 #crate_root::Silo::from_embedded(&#map_ident, #abs_root_lit)
122 }
123 };
124 expanded.into()
125 } else {
126 let expanded = quote! {
127 #crate_root::Silo::from_static(#abs_root_lit)
128 };
129 expanded.into()
130 }
131}
132
133fn collect_embed_entries(dir: &str, span: proc_macro2::Span) -> CollectResult {
138 let mut entries = Vec::new();
139 let mut errors = Vec::new();
140 let root = Path::new(dir);
141 for entry in WalkDir::new(root).into_iter() {
142 let entry = match entry {
143 Ok(e) => e,
144 Err(e) => {
145 let msg = format!("embed_silo!: failed to read entry: {}", e);
146 errors.push(quote_spanned! {span=> compile_error!(#msg); });
147 continue;
148 }
149 };
150 if entry.file_type().is_file() {
151 let path = entry.path();
152 let rel_path = match path.strip_prefix(root) {
153 Ok(r) => r.to_string_lossy().replace('\\', "/"),
154 Err(_) => {
155 let msg = "embed_silo!: failed to get relative path";
156 errors.push(quote_spanned! {span=> compile_error!(#msg); });
157 continue;
158 }
159 };
160 let abs_path = match path.canonicalize() {
161 Ok(p) => p.to_string_lossy().to_string(),
162 Err(_) => {
163 let msg = format!("embed_silo!: failed to canonicalize file: {}", path.display());
164 errors.push(quote_spanned! {span=> compile_error!(#msg); });
165 continue;
166 }
167 };
168 let size = match fs::metadata(path) {
169 Ok(meta) => meta.len() as usize,
170 Err(_) => 0,
171 };
172 let modified = match fs::metadata(path)
173 .and_then(|m| m.modified())
174 .ok()
175 .and_then(|mtime| mtime.duration_since(std::time::UNIX_EPOCH).ok())
176 {
177 Some(d) => d.as_secs(),
178 None => 0,
179 };
180 entries.push((rel_path, abs_path, size, modified));
181 }
182 }
183
184 entries.sort_by(|(a, _, _, _), (b, _, _, _)| a.cmp(b));
186 (entries, errors)
187}
188
189fn compile_error<S: AsRef<str>>(msg: S, span: proc_macro2::Span) -> proc_macro::TokenStream {
193 let lit = syn::LitStr::new(msg.as_ref(), span);
194 let tokens = quote!(compile_error!(#lit));
195 tokens.into()
196}
197
198fn generate_phf_map(entries: &[EmbedMeta], crate_root: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
201 let pairs = entries.iter().map(|(rel_path, abs_path, size, modified)| {
202 let rel_path_lit = syn::LitStr::new(rel_path, proc_macro2::Span::call_site());
203 let abs_path_lit = syn::LitStr::new(abs_path, proc_macro2::Span::call_site());
204 let size_lit = syn::LitInt::new(&size.to_string(), proc_macro2::Span::call_site());
205 let mod_lit = syn::LitInt::new(&modified.to_string(), proc_macro2::Span::call_site());
206 quote! {
207 #rel_path_lit => #crate_root::EmbedEntry {
208 path: #rel_path_lit,
209 contents: include_bytes!(#abs_path_lit),
210 size: #size_lit,
211 modified: #mod_lit,
212 },
213 }
214 });
215 quote! {
216 #(#pairs)*
217 }
218}