mwapi_responses_derive/
builder.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Copyright (C) 2021 Kunal Mehta <legoktm@debian.org>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote, ToTokens};
use syn::{Ident, Visibility};

pub(crate) struct StructBuilder {
    pub(crate) ident: Ident,
    pub(crate) fields: Vec<StructField>,
    pub(crate) visibility: Visibility,
    pub(crate) extra_derive: Option<TokenStream2>,
}

impl ToTokens for StructBuilder {
    fn to_tokens(&self, tokens: &mut TokenStream2) {
        let ident = &self.ident;
        let visiblity = &self.visibility;
        let extra_derive = &self.extra_derive;
        let fields = &self.fields;
        let stream = quote! {
            #[doc(hidden)]
            #extra_derive
            #[derive(Debug, Clone, ::mwapi_responses::serde::Deserialize)]
            #[serde(crate = "::mwapi_responses::serde")]
            #visiblity struct #ident {
                #(#fields)*
            }
        };

        stream.to_tokens(tokens);
    }
}

#[derive(Debug)]
pub(crate) struct StructField {
    pub(crate) name: String,
    pub(crate) type_: TokenStream2,
    pub(crate) default: bool,
    pub(crate) rename: Option<String>,
    pub(crate) deserialize_with: Option<String>,
}

impl ToTokens for StructField {
    /// A field line looks like:
    /// ```ignore
    /// #[serde(default)] #[serde(rename = "foo")] foo: String;
    /// ```
    /// The serde attributes are optional.
    ///
    fn to_tokens(&self, tokens: &mut TokenStream2) {
        let (name, rename) = match &self.rename {
            Some(rename) => {
                let name = &self.name;
                (
                    rename.to_string(),
                    Some(quote! {
                        #[serde(rename = #name)]
                    }),
                )
            }
            None => (self.name.to_string(), None),
        };
        let name = format_ident!("{}", name);
        let type_ = &self.type_;
        let default = if self.default {
            Some(quote! {
                #[serde(default)]
            })
        } else {
            None
        };
        let deser_with = self.deserialize_with.as_ref().map(|function| {
            quote! {
                #[serde(deserialize_with = #function)]
            }
        });
        let stream = quote! {
            #default #rename #deser_with pub #name: #type_,
        };

        stream.to_tokens(tokens);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    fn stringify<P: ToTokens>(thing: P) -> String {
        let mut stream = quote! {};
        thing.to_tokens(&mut stream);
        stream.to_string()
    }

    #[test]
    fn test_builder() {
        let builder = StructBuilder {
            ident: format_ident!("{}", "Name"),
            fields: vec![StructField {
                name: "abc".to_string(),
                type_: quote! { u32 },
                default: false,
                rename: None,
                deserialize_with: None,
            }],
            visibility: Visibility::Inherited,
            extra_derive: None,
        };
        assert_eq!(
            &stringify(builder),
            "# [doc (hidden)] # [derive (Debug , Clone , :: mwapi_responses :: serde :: Deserialize)] # [serde (crate = \"::mwapi_responses::serde\")] struct Name { pub abc : u32 , }"
        );
    }

    #[test]
    fn test_field() {
        let field = StructField {
            name: "abc".to_string(),
            type_: quote! { u32 },
            default: false,
            rename: None,
            deserialize_with: None,
        };
        assert_eq!(&stringify(field), "pub abc : u32 ,");
        let field = StructField {
            name: "abc".to_string(),
            type_: quote! { u32 },
            default: true,
            rename: None,
            deserialize_with: None,
        };
        assert_eq!(&stringify(field), "# [serde (default)] pub abc : u32 ,");
        let field = StructField {
            name: "abc".to_string(),
            type_: quote! { u32 },
            default: true,
            rename: Some("def".to_string()),
            deserialize_with: None,
        };
        assert_eq!(
            &stringify(field),
            "# [serde (default)] # [serde (rename = \"abc\")] pub def : u32 ,"
        );
        let field = StructField {
            name: "abc".to_string(),
            type_: quote! { u32 },
            default: false,
            rename: None,
            deserialize_with: Some("::foo::bar".to_string()),
        };
        assert_eq!(
            &stringify(field),
            "# [serde (deserialize_with = \"::foo::bar\")] pub abc : u32 ,"
        );
    }
}