service_builder_macro/
lib.rs

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// service-builder/src/lib.rs
use proc_macro::TokenStream;
use quote::{quote, format_ident};
use syn::{parse_macro_input, DeriveInput, Data, Fields};

#[proc_macro_attribute]
pub fn builder(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let input = parse_macro_input!(item as DeriveInput);
    let name = &input.ident;
    let builder_name = format_ident!("{}Builder", name);

    let fields = match &input.data {
        Data::Struct(data) => {
            match &data.fields {
                Fields::Named(fields) => &fields.named,
                Fields::Unit => return quote! {
                    #input

                    pub struct #builder_name {}

                    impl #builder_name {
                        pub fn new() -> Self {
                            Self {}
                        }

                        pub fn build(self) -> Result<#name, service_builder::error::BuildError> {
                            Ok(#name {})
                        }
                    }

                    impl #name {
                        pub fn builder() -> #builder_name {
                            #builder_name::new()
                        }
                    }
                }.into(),
                _ => panic!("Only named fields or unit structs are supported")
            }
        },
        _ => panic!("Only structs are supported")
    };

    let field_names: Vec<_> = fields.iter()
        .map(|f| f.ident.as_ref().unwrap())
        .collect();

    let field_types: Vec<_> = fields.iter()
        .map(|f| &f.ty)
        .collect();

    let builder_fields = field_names.iter().zip(field_types.iter()).map(|(name, ty)| {
        quote! { #name: Option<#ty> }
    });

    let with_methods = field_names.iter().zip(field_types.iter()).map(|(name, ty)| {
        quote! {
            pub fn #name(mut self, value: #ty) -> Self {
                self.#name = Some(value);
                self
            }
        }
    });

    let expanded = quote! {
        #input

        pub struct #builder_name {
            #(#builder_fields,)*
        }

        impl #builder_name {
            pub fn new() -> Self {
                Self {
                    #(#field_names: None,)*
                }
            }

            #(#with_methods)*

            pub fn build(self) -> Result<#name, service_builder::error::BuildError> {
                // Get all fields 
                #(
                    let #field_names = self.#field_names.ok_or_else(||
                        service_builder::error::BuildError::MissingDependency(stringify!(#field_names).to_string())
                    )?;
                )*

                Ok(#name {
                    #(#field_names: #field_names,)*
                })
            }
        }

        impl #name {
            pub fn builder() -> #builder_name {
                #builder_name::new()
            }
        }
    };

    eprintln!("Generated code:\n{}", expanded);

    TokenStream::from(expanded)
}