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
//! # Azure Functions for Rust
//!
//! This crate supports code generation for the `azure-functions-shared` crate.

#![feature(rust_2018_preview)]

extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;
use std::env;
use std::path::Path;
use syn::{parse, ItemMod};

/// A macro to generate a module from code created in the output directory.
///
/// This is a "procedural macro" solution to [https://github.com/rust-lang/rfcs/issues/752](https://github.com/rust-lang/rfcs/issues/752).
///
/// # Examples
///
/// This example uses $OUT_DIR/MyModule.rs to replace the annotated module with
/// the generated code in the output directory:
///
/// ```rust,ignore
/// use azure_functions_shared_codegen::generated_mod;
///
/// #[generated_mod]
/// mod MyModule {}
/// ```
#[proc_macro_attribute]
pub fn generated_mod(_: TokenStream, input: TokenStream) -> TokenStream {
    let m = parse::<ItemMod>(input).unwrap();

    let ident = &m.ident;

    let mut path = Path::new(&env::var("OUT_DIR").unwrap()).join(ident.to_string());

    path.set_extension("rs");

    let path = path.to_str().unwrap().to_string();

    quote!(
        #[path = #path]
        mod #ident;
    )
    .into()
}