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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! Please visit [docs.rs/structconf](https://docs.rs/structconf/) for the
//! documentation.

extern crate darling;
extern crate proc_macro;

mod error;
mod opt;

use crate::error::{Error, ErrorKind, Result};
use crate::opt::{Opt, OptArgData, OptFileData};

use proc_macro::TokenStream;
use proc_macro2::{Ident, TokenStream as TokenStream2};
use quote::quote;
use syn::{Data, DataStruct, DeriveInput, Expr, Fields, FieldsNamed};

#[proc_macro_derive(StructConf, attributes(conf))]
pub fn derive_conf(input: TokenStream) -> TokenStream {
    // Construct a representation of Rust code as a syntax tree
    // that we can manipulate
    let ast: DeriveInput = syn::parse(input).unwrap();
    let name = &ast.ident;

    // Build the trait implementation
    let result: Result<TokenStream> = match ast.data {
        Data::Struct(DataStruct {
            fields: Fields::Named(named_fields),
            ..
        }) => impl_conf_macro(name, named_fields),
        Data::Struct(_) => Err(Error {
            kind: ErrorKind::DeriveType(String::from("unnamed struct")),
            span: ast.ident.span(),
        }),
        Data::Enum(_) => Err(Error {
            kind: ErrorKind::DeriveType(String::from("enum")),
            span: ast.ident.span(),
        }),
        Data::Union(_) => Err(Error {
            kind: ErrorKind::DeriveType(String::from("union")),
            span: ast.ident.span(),
        }),
    };

    match result {
        Ok(tokens) => tokens.into(),
        Err(e) => syn::Error::from(e).to_compile_error().into(),
    }
}

fn impl_conf_macro(name: &Ident, fields: FieldsNamed) -> Result<TokenStream> {
    let mut options = Vec::new();
    for field in fields.named.into_iter() {
        options.push(Opt::parse(field)?);
    }
    let new_fields = parse_field_init(&options);
    let new_args = parse_args_init(&options);
    let to_file = parse_to_file(&options);

    Ok(quote! {
        impl StructConf for #name {
            fn parse(
                app: ::clap::App,
                path: &str
            ) -> Result<#name, ::structconf::Error> where Self: Sized {
                let args = #name::parse_args(app);
                #name::parse_file(args, path)
            }

            fn parse_args<'a>(
                app: ::clap::App<'a, 'a>
            ) -> ::clap::ArgMatches<'a> {
                app.args(&[
                    #(#new_args,)*
                ]).get_matches()
            }

            fn parse_file(
                args: ::clap::ArgMatches,
                path: &str
            ) -> Result<#name, ::structconf::Error> where Self: Sized {
                // Checking that the config file exists, and creating it
                // otherwise.
                let path_wrap = ::std::path::Path::new(path);
                if !path_wrap.exists() {
                    ::std::fs::File::create(&path_wrap)?;
                    println!("Created config file at {}", path);
                }

                let file = ::ini::Ini::load_from_file(path)?;
                Ok(#name {
                    #(#new_fields,)*
                })
            }

            fn write_file(
                &self,
                path: &str
            ) -> Result<(), ::structconf::Error> {
                let mut conf = ::ini::Ini::new();
                #(#to_file)*
                conf.write_to_file(path)?;

                Ok(())
            }
        }
    }
    .into())
}

/// This will return a tokenstream for each field that has to be initialized,
/// depending on whether the option is an argument, a config file option,
/// none, or both:
///
/// ```rust
/// field: {
///     // May go through no branches, branch A, branch B, or both branches.
///     let mut opt: Option<String> = None;
///
///     // Branch A (argument)
///     opt = arg.value_of(...);
///
///     // Branch B (config file)
///     if let None = opt {
///         opt = file.get_from(...);
///     }
///
///     match opt {
///         // Branch A, B, or both
///         Some(opt) => {
///             // Parse `opt`
///             let opt = opt.parse()?;
///         
///             // Branch A, if `inverse_arg`, after parsing. `inverse_arg`
///             // may only be true if it's an argument.
///             if arg.value_of(...).is_some() {
///                 opt = !opt;
///             }
///         },
///         // None
///         None => default
///     }
/// }
/// ```
///
/// This is intended to be as flexible as possible, and to avoid repetition
/// in the code, so it may not be optimized for some cases. For example, if
/// the option is neither a config file option nor an argument, the `match`
/// block is useless because it's obvious it's going to go through the `None`
/// branch. But these cases are easy to optimize by the compiler, so it's not
/// important.
///
/// Methods like `unwrap_or` can't be used here because this has to be able
/// to propagate errors with `?`.
fn parse_field_init(opts: &Vec<Opt>) -> Vec<TokenStream2> {
    opts.iter()
        .map(|opt| {
            let Opt {
                name,
                ty,
                is_option,
                default,
                file,
                arg,
                ..
            } = opt;

            // Obtains a TokenStream with what the default value of the option
            // is going to be. If `default` was used, the expression is used.
            // Otherwise, the type's default value is used, which may be `None`
            // in case the type is an `Option<T>`.
            let default = match default.to_owned() {
                Some(expr) => {
                    // TODO remove unwrap
                    let expr = syn::parse_str::<Expr>(&expr).unwrap();
                    if *is_option {
                        quote! { Some(#expr) }
                    } else {
                        quote! { #expr }
                    }
                }
                None => {
                    if *is_option {
                        quote! { None }
                    } else {
                        quote! { ::std::default::Default::default() }
                    }
                }
            };

            let val_ret = if *is_option {
                quote! { Some(opt) }
            } else {
                quote! { opt }
            };

            let mut value = quote! {
                let mut opt: Option<&str> = None;
            };

            let mut invert_opt = TokenStream2::new();
            if let Some(OptArgData { inverse, .. }) = arg {
                value.extend(quote! {
                    opt = args.value_of(stringify!(#name));
                });

                if *inverse {
                    invert_opt.extend(quote! {
                        if args.value_of(stringify!(#name)).is_some() {
                            opt = !opt;
                        }
                    });
                }
            }

            if let Some(OptFileData { section, .. }) = file {
                value.extend(quote! {
                    if let None = opt {
                        opt = file.get_from(Some(#section), stringify!(#name));
                    }
                });
            }

            value.extend(quote! {
                match opt {
                    Some(opt) => {
                        let mut opt = opt
                            .parse::<#ty>()
                            .map_err(|e| {
                                ::structconf::Error::Parse(e.to_string())
                            })?;
                        #invert_opt
                        #val_ret
                    },
                    None => #default
                }
            });

            quote! {
                #name: { #value }
            }
        })
        .collect()
}

fn parse_args_init(opts: &Vec<Opt>) -> Vec<TokenStream2> {
    opts.iter()
        .filter_map(|opt| {
            // In case it's not an argument, an empty TokenStream will be
            // returned.
            opt.arg.as_ref().and_then(|data| {
                let name = opt.name.to_string();
                let mut init = quote! {
                    ::clap::Arg::with_name(#name)
                };

                if let Some(help) = &data.help {
                    init.extend(quote! {
                        .help(#help)
                    });
                }

                if let Some(long) = &data.long {
                    init.extend(quote! {
                        .long(#long)
                    });
                }

                if let Some(short) = &data.short {
                    init.extend(quote! {
                        .short(#short)
                    });
                }

                Some(init)
            })
        })
        .collect()
}

fn parse_to_file(opts: &Vec<Opt>) -> Vec<TokenStream2> {
    opts.iter()
        .filter_map(|opt| {
            opt.file.as_ref().and_then(|file| {
                let name = opt.name.clone();
                let section = file.section.as_str();

                Some(if opt.is_option {
                    quote! {
                        if let Some(val) = &self.#name {
                            conf.with_section(Some(#section))
                                .set(stringify!(#name), val.to_string());
                        }
                    }
                } else {
                    quote! {
                        conf.with_section(Some(#section))
                            .set(stringify!(#name), self.#name.to_string());
                    }
                })
            })
        })
        .collect()
}