1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
#![recursion_limit = "1024"] extern crate proc_macro; use proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; use quote::quote; use std::path::{Path, PathBuf}; use syn::{parse_macro_input, AttrStyle, Data, DeriveInput, Lit, Meta, MetaNameValue}; use walkdir::WalkDir; fn generate_file_list<P>(item: &syn::DeriveInput, folder_path: P) -> TokenStream2 where P: AsRef<Path>, { let ident = &item.ident; let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl(); let values = WalkDir::new(&folder_path) .into_iter() .map(|entry| entry.unwrap().path().to_path_buf()) .filter(|path| path.is_file()) .map(|path| { path.strip_prefix(&folder_path) .unwrap() .to_str() .unwrap() .to_string() }); quote! { impl #impl_generics #ident #ty_generics #where_clause { pub fn list() -> impl std::iter::Iterator<Item = &'static str> { const FILES: &[&str] = &[#(#values),*]; FILES.into_iter().cloned() } pub fn get_str(file_path: &str) -> Option<&'static str> { Self::get(file_path).and_then(|s| ::std::str::from_utf8(s).ok()) } } } } #[cfg(debug_assertions)] fn generate_assets<P>(item: &syn::DeriveInput, folder_path: P) -> TokenStream2 where P: AsRef<Path>, { let ident = &item.ident; let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl(); let folder_path = folder_path.as_ref().to_str().unwrap(); quote! { impl #impl_generics #ident #ty_generics #where_clause { pub fn get(file_path: &str) -> Option<&'static [u8]> { use std::collections::HashSet; use std::fs::read; use std::path::{PathBuf, Path}; use std::sync::Mutex; packer::lazy_static! { static ref CACHE: Mutex<HashSet<&'static [u8]>> = Mutex::new(HashSet::new()); } let mut path = PathBuf::from(#folder_path); let fpath = PathBuf::from(file_path); path.push(fpath); let file = read(path).ok()?; let mut cache = CACHE.lock().unwrap(); if !cache.contains(&file as &[_]) { cache.insert(Box::leak(file.clone().into_boxed_slice())); } Some(cache.get(&file as &[_]).unwrap()) } } } } #[cfg(not(debug_assertions))] fn generate_assets<P>(item: &syn::DeriveInput, folder_path: P) -> TokenStream2 where P: AsRef<Path>, { let ident = &item.ident; let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl(); let values = WalkDir::new(&folder_path) .into_iter() .filter_map(|e| e.ok()) .filter(|e| e.file_type().is_file()) .map(|entry| { let base = folder_path.as_ref(); let key = String::from( entry .path() .strip_prefix(base) .unwrap() .to_str() .expect("Path does not have a string representation"), ); let canonical_path = std::fs::canonicalize(entry.path()).expect("Could not get canonical path"); let canonical_path_str = canonical_path.to_str(); quote! { #key => Some(include_bytes!(#canonical_path_str)) } }) .collect::<Vec<_>>(); quote! { impl #impl_generics #ident #ty_generics #where_clause { pub fn get(file_path: &str) -> Option<&'static [u8]> { match file_path { #(#values,)* _ => None, } } } } } fn help() { panic!( "#[derive(Packer)] should contain one attribute like this #[folder = \"examples/public/\"]" ); } fn impl_packer(ast: &syn::DeriveInput) -> TokenStream2 { match ast.data { Data::Enum(_) => help(), _ => (), }; if ast.attrs.len() < 1 { panic!("Missing #[folder = \"\"] attribute."); } let attr = &ast.attrs[0]; match attr.style { AttrStyle::Outer => (), _ => panic!("Attribute must be an outer attribute."), } let meta = attr.parse_meta().expect("Failed to parse meta"); let (name, value) = match meta { Meta::NameValue(MetaNameValue { ident, lit, .. }) => (ident, lit), _ => panic!("u dum but"), }; if name != "folder" { panic!("Attribute name must be 'folder'") } let folder_path = match value { Lit::Str(s) => PathBuf::from(s.value()), _ => panic!("Attribute value must be a string."), }; if !Path::new(&folder_path).exists() { panic!( "#[derive(Packer)] folder '{}' does not exist. cwd: '{}'", folder_path.to_str().unwrap(), std::env::current_dir().unwrap().to_str().unwrap() ); }; let generate_file_list_fn = generate_file_list(ast, &folder_path); let generate_assets_fn = generate_assets(ast, &folder_path); quote! { #generate_file_list_fn #generate_assets_fn } } #[proc_macro_derive(Packer, attributes(folder))] pub fn derive_input_object(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); let gen = impl_packer(&ast); TokenStream::from(gen) }