tf_bindgen_codegen/
lib.rs

1use construct::Construct;
2use darling::FromDeriveInput;
3use proc_macro::TokenStream;
4use syn::DeriveInput;
5
6mod construct;
7mod resource;
8
9/// Used to generate a builder chain for a specified Terraform resource or data source. To
10/// describe the resource this macro will use a simplified version of Terraform's HCL syntax.
11///
12/// # Usage
13///
14/// ```rust
15/// use tf_codegen::resource;
16///
17/// resource! {
18///		&scope,
19///		resource "kubernetes_pod" "nginx" {
20///			metadata {
21///				name = "nginx"
22///			}
23///			spec {
24///				container {
25///					image = "nginx"
26///					port {
27///						container_port = 80
28///					}
29///				}
30///			}
31///		}
32/// }
33/// ```
34#[proc_macro]
35pub fn resource(item: TokenStream) -> TokenStream {
36    let block = syn::parse_macro_input!(item as resource::Block);
37    quote::quote!(#block).into()
38}
39
40/// Used to generate an implementation of [`tf_bindgen::Construct`]. Requires to fields to be
41/// annotated with `#[id]` and `#[scope]`.
42///
43/// # Usage
44///
45/// ```rust
46/// #[derive(tf_codegen::Construct)]
47/// #[construct(crate = "::tf_codegen")] // optional
48/// pub struct Custom {
49///		#[construct(scope)]
50///		__m_scope: Rc<dyn ::tf_bindgen::Construct>,
51///		#[construct(id)]
52///		__m_name: String
53/// }
54/// ```
55#[proc_macro_derive(Construct, attributes(construct))]
56pub fn construct_derive(input: TokenStream) -> TokenStream {
57    let input = syn::parse_macro_input!(input as DeriveInput);
58    let construct = Construct::from_derive_input(&input).unwrap();
59    quote::quote!(#construct).into()
60}