ori_macro/
build.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse::ParseStream, punctuated::Punctuated, DeriveInput};

use crate::get_option_type;

syn::custom_keyword!(ignore);

enum FieldAttribute {
    Ignore,
}

impl syn::parse::Parse for FieldAttribute {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let lookahead = input.lookahead1();

        if lookahead.peek(ignore) {
            input.parse::<ignore>()?;
            Ok(Self::Ignore)
        } else {
            Err(lookahead.error())
        }
    }
}

#[derive(Default)]
struct FieldAttributes {
    ignore: bool,
}

impl FieldAttributes {
    fn new(attrs: &[syn::Attribute]) -> manyhow::Result<Self> {
        let mut this = Self::default();

        for attr in attrs {
            if attr.path().is_ident("build") {
                let updates = attr.parse_args_with(|input: ParseStream| {
                    Punctuated::<FieldAttribute, syn::Token![,]>::parse_terminated(input)
                })?;

                for update in updates {
                    match update {
                        FieldAttribute::Ignore => this.ignore = true,
                    }
                }
            }
        }

        Ok(this)
    }
}

pub fn derive_build(input: proc_macro::TokenStream) -> manyhow::Result<proc_macro::TokenStream> {
    let input = syn::parse::<DeriveInput>(input)?;

    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

    let build = build_impl(&input)?;

    let expanded = quote! {
        impl #impl_generics #name #ty_generics #where_clause {
            #(#build)*
        }
    };

    Ok(expanded.into())
}

fn build_impl(input: &syn::DeriveInput) -> manyhow::Result<Vec<TokenStream>> {
    match input.data {
        syn::Data::Struct(ref data) => match data.fields {
            syn::Fields::Named(ref fields) => {
                let names = named_fields(fields);
                build_fields(names, fields.named.iter())
            }
            syn::Fields::Unnamed(_) => {
                manyhow::bail!("`Build` can only be derived for named structs")
            }
            syn::Fields::Unit => Ok(Vec::new()),
        },
        _ => manyhow::bail!("`Build` can only be derived for structs"),
    }
}

fn named_fields(fields: &syn::FieldsNamed) -> impl Iterator<Item = TokenStream> + '_ {
    fields.named.iter().map(|field| {
        let ident = field.ident.as_ref().unwrap();
        quote!(#ident)
    })
}

fn build_fields<'a>(
    names: impl Iterator<Item = TokenStream>,
    fields: impl Iterator<Item = &'a syn::Field>,
) -> manyhow::Result<Vec<TokenStream>> {
    let mut build = Vec::new();

    for (name, field) in names.zip(fields) {
        build.push(build_field(name, field)?);
    }

    Ok(build)
}

fn build_field(name: TokenStream, field: &syn::Field) -> manyhow::Result<TokenStream> {
    let attrs = FieldAttributes::new(&field.attrs)?;

    if attrs.ignore {
        return Ok(quote!());
    }

    let doc = format!("Set `self.{}`.", name);

    let mut field_doc = Vec::new();

    for attr in &field.attrs {
        if attr.path().is_ident("doc") {
            field_doc.push(attr);
        }
    }

    match get_option_type(&field.ty) {
        Some(ty) => Ok(quote! {
            #[doc = #doc]
            #[doc = ""]
            #(#field_doc)*
            pub fn #name(mut self, #name: impl Into<#ty>) -> Self {
                self.#name = ::std::option::Option::Some(::std::convert::Into::into(#name));
                self
            }
        }),
        None => {
            let ty = &field.ty;

            Ok(quote! {
                #[doc = #doc]
                #[doc = ""]
                #(#field_doc)*
                pub fn #name(mut self, #name: impl Into<#ty>) -> Self {
                    self.#name = ::std::convert::Into::into(#name);
                    self
                }
            })
        }
    }
}