file_check_macro/
lib.rs

1extern crate proc_macro;
2
3use proc_macro::TokenStream;
4use quote::quote;
5use syn::{parse_macro_input, LitStr};
6
7/// Verifies that the passed file path exists in `src` and
8/// generates a `const TEMPLATE:&str`.
9#[proc_macro]
10pub fn generate_template(input: TokenStream) -> TokenStream {
11    let path_token = parse_macro_input!(input as LitStr);
12    let path = path_token.value();
13    let full_path = format!("src/{}", path);
14    if std::path::Path::new(&full_path).exists() {
15        TokenStream::from(quote! {
16            const TEMPLATE: &str = #path_token;
17        })
18    } else {
19        TokenStream::from(quote! {
20            compile_error!("The specified file does not exist.");
21        })
22    }
23}