include_dir_impl/
lib.rs

1//! Implementation crate for the [include_dir!()] macro.
2//!
3//! [include_dir!()]: https://github.com/Michael-F-Bryan/include_dir
4
5extern crate proc_macro;
6
7use proc_macro::TokenStream;
8use proc_macro_hack::proc_macro_hack;
9use quote::quote;
10use syn::{parse_macro_input, LitStr};
11
12use crate::dir::Dir;
13use std::env;
14use std::path::PathBuf;
15
16mod dir;
17mod file;
18
19#[proc_macro_hack]
20pub fn include_dir(input: TokenStream) -> TokenStream {
21    let input: LitStr = parse_macro_input!(input as LitStr);
22    let crate_root = env::var("CARGO_MANIFEST_DIR").unwrap();
23
24    let path = PathBuf::from(crate_root).join(input.value());
25
26    if !path.exists() {
27        panic!("\"{}\" doesn't exist", path.display());
28    }
29
30    let path = path.canonicalize().expect("Can't normalize the path");
31
32    let dir = Dir::from_disk(&path, &path).expect("Couldn't load the directory");
33
34    TokenStream::from(quote! {
35        #dir
36    })
37}