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
extern crate proc_macro;
use proc_macro::TokenStream;
use syn::{Expr, Lit};
use quote::quote;
use std::path::{Path, PathBuf};
use std::env::var;
use std::fs::read_dir;
#[proc_macro]
pub fn embed(input: TokenStream) -> TokenStream {
let directory = match syn::parse_macro_input!(input as Expr) {
Expr::Lit(lit) => match lit.lit {
Lit::Str(lit) => lit.value(),
_ => panic!("expected a string literal"),
},
_ => panic!("expected a string literal"),
};
let mut directory = PathBuf::from(directory);
if directory.is_relative() {
directory = Path::new(&var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set!")).join(directory);
}
if !directory.is_dir() {
panic!("{:?} does not exist or is not a directory!", directory);
}
let mut filenames = Vec::new();
let mut paths = Vec::new();
for file in read_dir(&directory).expect("Couldn't read the contents of the specified directory") {
let file = file.unwrap();
if !file.file_type().unwrap().is_file() {
continue;
}
let filename = file.file_name();
filenames.push(filename.clone().into_string().unwrap());
paths.push(directory.join(filename).to_str().unwrap().to_owned());
}
TokenStream::from(quote!{{
let mut map = std::collections::HashMap::new();
#( map.insert(#filenames, &include_bytes!(#paths)[..]); )*
map
}})
}