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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#![doc(html_root_url = "https://docs.rs/optfield/0.3.0")]

//! `optfield` is a macro that, given a struct, generates another struct with
//! the same fields, but wrapped in `Option<T>`.
//!
//! Features:
//! * [Simple examples](#simple-examples)
//! * [Visibility](#visibility)
//! * [Rewrapping `Option` fields](#rewrapping-option-fields)
//! * [Documentation](#documentation)
//! * [Attributes](#attributes)
//! * [Field documentation](#field-documentation)
//! * [Field attributes](#field-attributes)
//! * [Merging](#merging)
//! * [From](#from)
//!
//! # Simple examples
//! The first argument is the name of the generated struct:
//! ```
//! use optfield::optfield;
//!
//! #[optfield(Opt)]
//! struct MyStruct {
//!     text: String
//! }
//! ```
//! Will generate the following __second__ struct (leaving `MyStruct` as is):
//! ```
//! struct Opt {
//!     text: Option<String>
//! }
//! ```
//! It also works with tuple structs:
//! ```
//! # use optfield::*;
//! #[optfield(Opt)]
//! struct MyTuple(String, i32);
//! ```
//! Will generate:
//! ```
//! struct Opt(Option<String>, Option<i32>);
//! ```
//! Generics and lifetimes are preserved:
//! ```
//! # use optfield::*;
//! #[optfield(Opt)]
//! struct MyStruct<'a, T> {
//!     field: &'a T
//! }
//! ```
//! Will generate:
//! ```
//! struct Opt<'a, T> {
//!     field: Option<&'a T>
//! }
//! ```
//!
//! # Visibility
//! By default, opt structs are private. To use custom visibility simply add it
//! right before the opt struct name:
//! ```
//! # use optfield::*;
//! #[optfield(pub(crate) Opt)]
//! struct MyStruct {
//!     text: String
//! }
//! ```
//! Will generate:
//! ```
//! pub(crate) struct Opt {
//!     text: Option<String>
//! }
//! ```
//! Field visibility is preserved.
//!
//! # Rewrapping `Option` fields
//! By default, fields that are already wrapped in `Option<T>` are not wrapped
//! again:
//! ```
//! # use optfield::*;
//! #[optfield(Opt)]
//! struct MyStruct {
//!     text: Option<String>,
//!     number: i32
//! }
//! ```
//! Will generate:
//! ```
//! struct Opt {
//!     text: Option<String>,
//!     number: Option<i32>
//! }
//! ```
//! To rewrap them pass the `rewrap` argument:
//!
//! ```
//! # use optfield::*;
//! #[optfield(Opt, rewrap)]
//! struct MyStruct {
//!     text: Option<String>,
//!     number: i32
//! }
//! ```
//! Will generate:
//! ```
//! struct Opt {
//!     text: Option<Option<String>>,
//!     number: Option<i32>
//! }
//! ```
//!
//! # Documentation
//! To document the opt struct, either duplicate the same documentation as the
//! original using the `doc` argument by itself:
//! ```
//! # use optfield::*;
//! /// My struct documentation
//! /// ...
//! # use optfield::*;
//! #[optfield(Opt, doc)]
//! struct MyStruct {
//!     text: String
//! }
//! ```
//! Will generate:
//! ```
//! /// My struct documentation
//! /// ...
//! struct Opt {
//!     text: Option<String>
//! }
//! ```
//! Or write custom documentation by giving `doc` a value:
//! ```
//! # use optfield::*;
//! #[optfield(
//!     Opt,
//!     doc = "
//!         Custom documentation
//!         for Opt struct...
//!     "
//! )]
//! struct MyStruct {
//!     text: String
//! }
//! ```
//! Will generate:
//! ```
//! /// Custom documentation
//! /// for Opt struct...
//! struct Opt {
//!     text: Option<String>
//! }
//! ```
//!
//! # Attributes
//! The `attrs` argument alone makes optfield insert the same attributes as the
//! original:
//! ```
//! # use optfield::*;
//! #[optfield(Opt, attrs)]
//! #[cfg(test)]
//! #[derive(Clone)]
//! struct MyStruct {
//!     text: String
//! }
//! ```
//! Will generate:
//! ```
//! #[cfg(test)]
//! #[derive(Clone)]
//! struct Opt {
//!     text: Option<String>
//! }
//! ```
//! To add more attributes besides the original ones, use `attrs = add(...)`:
//! ```
//! # use optfield::*;
//! #[optfield(
//!     Opt,
//!     attrs = add(
//!         cfg(test),
//!         derive(Clone)
//!     )
//! )]
//! #[derive(Debug)]
//! struct MyStruct {
//!     text: String
//! }
//! ```
//! Will generate:
//! ```
//! #[derive(Debug)]
//! #[cfg(test)]
//! #[derive(Clone)]
//! struct Opt {
//!     text: Option<String>
//! }
//! ```
//! To replace with other attributes, `attrs = (...)`:
//! ```
//! # use optfield::*;
//! #[optfield(
//!     Opt,
//!     attrs = (
//!         cfg(test),
//!         derive(Clone)
//!     )
//! )]
//! #[derive(Debug)]
//! struct MyStruct {
//!     text: String
//! }
//! ```
//! Will generate:
//! ```
//! #[cfg(test)]
//! #[derive(Clone)]
//! struct Opt {
//!     text: Option<String>
//! }
//! ```
//! **NOTE** on attribute order: `optfield`, like any other proc macro, only
//! sees the attributes defined after it:
//!
//! ```
//! # use optfield::*;
//! #[cfg(test)] // optfield is unaware of this attribute
//! #[optfield(Opt, attrs)]
//! #[derive(Debug)]
//! struct MyStruct;
//! ```
//! Will generate:
//! ```
//! #[derive(Debug)]
//! struct Opt;
//! ```
//!
//! # Field documentation
//! By default, field documentation is removed:
//! ```
//! # use optfield::*;
//! #[optfield(Opt)]
//! struct MyStruct {
//!     /// Field
//!     /// documentation
//!     text: String
//! }
//! ```
//! Will generate:
//! ```
//! struct Opt {
//!     text: Option<String>
//! }
//! ```
//! To preserve field documentation use the `field_doc` argument:
//! ```
//! # use optfield::*;
//! #[optfield(Opt, field_doc)]
//! struct MyStruct {
//!     /// Field
//!     /// documentation
//!     text: String
//! }
//! ```
//! Will generate:
//! ```
//! struct Opt {
//!     /// Field
//!     /// documentation
//!     text: Option<String>
//! }
//! ```
//!
//! # Field attributes
//! Field attributes can be handled using the `field_attrs` argument which works
//! similarly to `attrs`, but applies to all fields.
//!
//! `field_attrs` can be used independently of `attrs`.
//!
//! By default, no field attributes are inserted:
//! ```
//! # use optfield::*;
//! # use serde::Deserialize;
//! #[optfield(Opt, attrs)]
//! #[derive(Deserialize)]
//! struct MyStruct {
//!     #[serde(rename = "text")]
//!     my_text: String
//! }
//! ```
//! Will generate:
//! ```
//! # use serde::Deserialize;
//! #[derive(Deserialize)]
//! struct Opt {
//!     my_text: Option<String>
//! }
//! ```
//! To keep them:
//! ```
//! # use optfield::*;
//! # use serde::Deserialize;
//! #[optfield(Opt, attrs, field_attrs)]
//! #[derive(Deserialize)]
//! struct MyStruct {
//!     #[serde(rename = "text")]
//!     my_text: String
//! }
//! ```
//! Will generate:
//! ```
//! # use serde::Deserialize;
//! #[derive(Deserialize)]
//! struct Opt {
//!     #[serde(rename = "text")]
//!     my_text: Option<String>
//! }
//! ```
//! To add more attributes:
//! ```
//! # use optfield::*;
//! # use serde::Deserialize;
//! #[optfield(
//!     Opt,
//!     attrs,
//!     field_attrs = add(
//!         serde(default)
//!     )
//! )]
//! #[derive(Deserialize)]
//! struct MyStruct {
//!     #[serde(rename = "text")]
//!     my_text: String,
//!     #[serde(rename = "number")]
//!     my_number: i32
//! }
//! ```
//! Will generate:
//! ```
//! # use serde::Deserialize;
//! #[derive(Deserialize)]
//! struct Opt {
//!     #[serde(rename = "text")]
//!     #[serde(default)]
//!     my_text: Option<String>,
//!     #[serde(rename = "number")]
//!     #[serde(default)]
//!     my_number: Option<i32>
//! }
//! ```
//! To replace all field attributes:
//! ```
//! # use optfield::*;
//! # use serde::Deserialize;
//! #[optfield(
//!     Opt,
//!     attrs,
//!     field_attrs = (
//!         serde(default)
//!     )
//! )]
//! #[derive(Deserialize)]
//! struct MyStruct {
//!     #[serde(rename = "text")]
//!     my_text: String,
//!     #[serde(rename = "number")]
//!     my_number: i32
//! }
//! ```
//! Will generate:
//! ```
//! # use serde::Deserialize;
//! #[derive(Deserialize)]
//! struct Opt {
//!     #[serde(default)]
//!     my_text: Option<String>,
//!     #[serde(default)]
//!     my_number: Option<i32>
//! }
//! ```
//!
//! # Merging
//! When the  `merge_fn` argument is used `optfield` will add a method to the
//! original struct that merges an opt struct back into the original.
//!
//! By default, the method is named `merge_opt` and has the following signature:
//! ```
//! # struct Opt;
//! # impl Opt {
//! // assuming the opt struct is named Opt;
//! // takes opt by value;
//! fn merge_opt(&mut self, opt: Opt)
//! # {}
//! # }
//! ```
//! When merging, all values of the opt struct that are `Some(...)` are set as
//! values of the original struct fields.
//!
//! To use it:
//! ```
//! # use optfield::*;
//! #[optfield(Opt, merge_fn)]
//! struct MyStruct {
//!     text: String,
//!     number: i32
//! }
//!
//! let mut original = MyStruct {
//!     text: "awesome".to_string(),
//!     number: 1
//! };
//!
//! let opt = Opt {
//!     text: Some("amazing".to_string()),
//!     number: None
//! };
//!
//! original.merge_opt(opt);
//!
//! // text field value is merged
//! assert_eq!(original.text, "amazing");
//! // number field stays the same
//! assert_eq!(original.number, 1);
//! ```
//! The merge function can be given:
//! * custom name: `merge_fn = my_merge_fn`
//! * custom visibility (default is private): `merge_fn = pub(crate)`
//! * both: `merge_fn = pub my_merge_fn`
//!
//! # From
//! When the `from` argument is used, `From<MyStruct>` is implemented for `Opt`.
//! ```
//! # use optfield::*;
//! #[optfield(Opt, from)]
//! struct MyStruct {
//!     text: String,
//!     number: i32,
//! }
//!
//! let original = MyStruct {
//!     text: "super".to_string(),
//!     number: 2,
//! };
//!
//! let from = Opt::from(original);
//! assert_eq!(from.text.unwrap(), "super");
//! assert_eq!(from.number.unwrap(), 2);
//! ```
extern crate proc_macro;

use crate::proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemStruct};

mod args;
mod attrs;
mod error;
mod fields;
mod from;
mod generate;
mod merge;

use args::Args;
use generate::generate;

/// The macro
///
/// Check [crate documentation] for more information.
///
/// [crate documentation]: ./index.html
#[proc_macro_attribute]
pub fn optfield(attr: TokenStream, item: TokenStream) -> TokenStream {
    let item: ItemStruct = parse_macro_input!(item);
    let args: Args = parse_macro_input!(attr);

    let opt_item = generate(&item, args);

    let out = quote! {
        #item

        #opt_item
    };

    out.into()
}

#[cfg(test)]
mod test_util {
    use proc_macro2::TokenStream;
    use syn::{parse::Parser, parse2, Attribute, Field, Fields, ItemStruct, Type, Visibility};

    use crate::args::Args;
    use crate::attrs::generator::is_doc_attr;

    pub fn parse_item_and_args(
        item_tokens: TokenStream,
        args_tokens: TokenStream,
    ) -> (ItemStruct, Args) {
        (parse_item(item_tokens), parse_args(args_tokens))
    }

    pub fn parse_field_and_args(
        field_tokens: TokenStream,
        args_tokens: TokenStream,
    ) -> (Field, Args) {
        (parse_field(field_tokens), parse_args(args_tokens))
    }

    pub fn parse_item(tokens: TokenStream) -> ItemStruct {
        parse2(tokens).unwrap()
    }

    pub fn parse_field(tokens: TokenStream) -> Field {
        Field::parse_named.parse2(tokens).unwrap()
    }

    pub fn parse_args(tokens: TokenStream) -> Args {
        parse2(tokens).unwrap()
    }

    pub fn parse_attr(tokens: TokenStream) -> Attribute {
        parse_attrs(tokens).get(0).unwrap().clone()
    }

    pub fn parse_attrs(tokens: TokenStream) -> Vec<Attribute> {
        Attribute::parse_outer.parse2(tokens).unwrap()
    }

    pub fn parse_type(tokens: TokenStream) -> Type {
        parse2(tokens).unwrap()
    }

    pub fn parse_types(tokens: Vec<TokenStream>) -> Vec<Type> {
        tokens.into_iter().map(parse_type).collect()
    }

    pub fn field_types(fields: Fields) -> Vec<Type> {
        fields.iter().map(|f| f.ty.clone()).collect()
    }

    pub fn parse_visibility(tokens: TokenStream) -> Visibility {
        parse2(tokens).unwrap()
    }

    pub fn attrs_contain_all(attrs: &[Attribute], other_attrs: &[Attribute]) -> bool {
        for attr in other_attrs {
            if !attrs.contains(attr) {
                return false;
            }
        }

        true
    }

    pub fn attrs_contain_any(attrs: &[Attribute], any_attrs: &[Attribute]) -> bool {
        for attr in any_attrs {
            if attrs.contains(attr) {
                return true;
            }
        }

        false
    }

    pub fn doc_attrs(attrs: &[Attribute]) -> Vec<Attribute> {
        attrs
            .iter()
            .filter(|a| is_doc_attr(a))
            .map(|a| a.clone())
            .collect()
    }
}