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
#[macro_use]
extern crate proc_macro_hack;
extern crate failure;
extern crate proc_macro2;
extern crate syn;
#[macro_use]
extern crate quote;

mod dir;
mod file;

use dir::Dir;
use std::env;
use std::path::PathBuf;
use syn::LitStr;

proc_macro_expr_impl! {
    /// Add one to an expression.
    pub fn include_dir_impl(input: &str) -> String {
        let input: LitStr =  syn::parse_str(input)
            .expect("include_dir!() only accepts a single string argument");
        let crate_root = env::var("CARGO_MANIFEST_DIR").unwrap();

        let path = PathBuf::from(crate_root)
            .join(input.value());

        if !path.exists() {
            panic!("\"{}\" doesn't exist", path.display());
        }

        let path = path.canonicalize().expect("Can't normalize the path");

        let mut dir = Dir::from_disk(&path).expect("Couldn't load the directory");
        dir.normalize(&path);

        quote!(#dir).to_string()
    }
}