file_check_macro 0.1.0

A macro for checking the presence of a template file at compile time. E.g. for Tera templates
Documentation
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, LitStr};

/// Verifies that the passed file path exists in `src` and
/// generates a `const TEMPLATE:&str`.
#[proc_macro]
pub fn generate_template(input: TokenStream) -> TokenStream {
    let path_token = parse_macro_input!(input as LitStr);
    let path = path_token.value();
    let full_path = format!("src/{}", path);
    if std::path::Path::new(&full_path).exists() {
        TokenStream::from(quote! {
            const TEMPLATE: &str = #path_token;
        })
    } else {
        TokenStream::from(quote! {
            compile_error!("The specified file does not exist.");
        })
    }
}