property 0.3.3

Generate several common methods for structs automatically.
Documentation
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// Copyright (C) 2019-2020 Boyu Yang
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use quote::quote;
use syn::{parse::Result as ParseResult, spanned::Spanned as _, Error as SynError};

const ATTR_NAME: &str = "property";
const SKIP: &str = "skip";
const GET_TYPE_OPTIONS: (&str, Option<&[&str]>) = ("type", Some(&["ref", "copy", "clone"]));
const SET_TYPE_OPTIONS: (&str, Option<&[&str]>) =
    ("type", Some(&["ref", "own", "none", "replace"]));
const NAME_OPTION: (&str, Option<&[&str]>) = ("name", None);
const PREFIX_OPTION: (&str, Option<&[&str]>) = ("prefix", None);
const SUFFIX_OPTION: (&str, Option<&[&str]>) = ("suffix", None);
const VISIBILITY_OPTIONS: &[&str] = &["disable", "public", "crate", "private"];
const SORT_TYPE_OPTIONS: &[&str] = &["asc", "desc"];

pub(crate) struct PropertyDef {
    pub(crate) name: syn::Ident,
    pub(crate) generics: syn::Generics,
    pub(crate) fields: Vec<FieldDef>,
}

pub(crate) struct FieldDef {
    pub(crate) ident: syn::Ident,
    pub(crate) ty: syn::Type,
    pub(crate) conf: FieldConf,
}

#[derive(Clone, Copy)]
pub(crate) enum GetTypeConf {
    NotSet,
    Ref,
    Copy_,
    Clone_,
}

#[derive(Clone, Copy)]
pub(crate) enum SetTypeConf {
    Ref,
    Own,
    None_,
    Replace,
}

#[derive(Clone, Copy)]
pub(crate) enum VisibilityConf {
    Disable,
    Public,
    Crate,
    Private,
}

#[derive(Clone, Copy)]
pub(crate) enum SortTypeConf {
    Ascending,
    Descending,
}

#[derive(Clone)]
pub(crate) enum MethodNameConf {
    Name(String),
    Format { prefix: String, suffix: String },
}

#[derive(Clone)]
pub(crate) struct GetFieldConf {
    pub(crate) vis: VisibilityConf,
    pub(crate) name: MethodNameConf,
    pub(crate) typ: GetTypeConf,
}

#[derive(Clone)]
pub(crate) struct SetFieldConf {
    pub(crate) vis: VisibilityConf,
    pub(crate) name: MethodNameConf,
    pub(crate) typ: SetTypeConf,
}

#[derive(Clone)]
pub(crate) struct MutFieldConf {
    pub(crate) vis: VisibilityConf,
    pub(crate) name: MethodNameConf,
}

#[derive(Clone)]
pub(crate) struct OrdFieldConf {
    pub(crate) number: Option<usize>,
    pub(crate) sort_type: SortTypeConf,
}

#[derive(Clone)]
pub(crate) struct FieldConf {
    pub(crate) get: GetFieldConf,
    pub(crate) set: SetFieldConf,
    pub(crate) mut_: MutFieldConf,
    pub(crate) ord: OrdFieldConf,
    pub(crate) skip: bool,
}

impl syn::parse::Parse for PropertyDef {
    fn parse(input: syn::parse::ParseStream) -> ParseResult<Self> {
        let derive_input: syn::DeriveInput = input.parse()?;
        let attrs_span = derive_input.span();
        let syn::DeriveInput {
            attrs,
            ident,
            generics,
            data,
            ..
        } = derive_input;
        let ident_span = ident.span();
        match data {
            syn::Data::Struct(data) => match data.fields {
                syn::Fields::Named(named_fields) => {
                    let conf = Self::parse_attrs(attrs_span, &attrs[..])?;
                    Ok(Self {
                        name: ident,
                        generics,
                        fields: FieldDef::parse_named_fields(named_fields, conf, ident_span)?,
                    })
                }
                _ => Err(SynError::new(ident_span, "only support named fields")),
            },
            _ => Err(SynError::new(ident_span, "only support structs")),
        }
    }
}

impl PropertyDef {
    fn parse_attrs(span: proc_macro2::Span, attrs: &[syn::Attribute]) -> ParseResult<FieldConf> {
        Ok(parse_attrs(span, Default::default(), attrs, false)?)
    }
}

impl FieldDef {
    fn parse_named_fields(
        named_fields: syn::FieldsNamed,
        conf: FieldConf,
        span: proc_macro2::Span,
    ) -> ParseResult<Vec<Self>> {
        let mut fields = Vec::new();
        for f in named_fields.named.into_iter() {
            let syn::Field {
                attrs, ident, ty, ..
            } = f.clone();
            let conf = Self::parse_attrs(f.span(), conf.clone(), &attrs[..])?;
            let ident = ident.ok_or_else(|| SynError::new(f.span(), "unreachable"))?;
            let field = Self { ident, ty, conf };
            fields.push(field);
        }
        if fields.is_empty() {
            Err(SynError::new(span, "nothing can do for an empty struct"))
        } else {
            Ok(fields)
        }
    }

    fn parse_attrs(
        span: proc_macro2::Span,
        conf: FieldConf,
        attrs: &[syn::Attribute],
    ) -> ParseResult<FieldConf> {
        Ok(parse_attrs(span, conf, attrs, true)?)
    }
}

impl GetTypeConf {
    pub(crate) fn parse_from_input(
        namevalue_params: &::std::collections::HashMap<&str, String>,
        span: proc_macro2::Span,
    ) -> ParseResult<Option<Self>> {
        let choice = match namevalue_params.get("type").map(AsRef::as_ref) {
            None => None,
            Some("ref") => Some(Self::Ref),
            Some("copy") => Some(Self::Copy_),
            Some("clone") => Some(Self::Clone_),
            _ => return Err(SynError::new(span, "unreachable result")),
        };
        Ok(choice)
    }
}

impl SetTypeConf {
    pub(crate) fn parse_from_input(
        namevalue_params: &::std::collections::HashMap<&str, String>,
        span: proc_macro2::Span,
    ) -> ParseResult<Option<Self>> {
        let choice = match namevalue_params.get("type").map(AsRef::as_ref) {
            None => None,
            Some("ref") => Some(Self::Ref),
            Some("own") => Some(Self::Own),
            Some("none") => Some(Self::None_),
            Some("replace") => Some(Self::Replace),
            _ => return Err(SynError::new(span, "unreachable result")),
        };
        Ok(choice)
    }
}

impl VisibilityConf {
    pub(crate) fn parse_from_input(
        input: Option<&str>,
        span: proc_macro2::Span,
    ) -> ParseResult<Option<Self>> {
        let choice = match input {
            None => None,
            Some("disable") => Some(Self::Disable),
            Some("public") => Some(Self::Public),
            Some("crate") => Some(Self::Crate),
            Some("private") => Some(Self::Private),
            _ => return Err(SynError::new(span, "unreachable result")),
        };
        Ok(choice)
    }

    pub(crate) fn to_ts(self) -> Option<proc_macro2::TokenStream> {
        match self {
            Self::Disable => None,
            Self::Public => Some(quote!(pub)),
            Self::Crate => Some(quote!(pub(crate))),
            Self::Private => Some(quote!()),
        }
    }
}

impl SortTypeConf {
    pub(crate) fn parse_from_input(
        input: Option<&str>,
        span: proc_macro2::Span,
    ) -> ParseResult<Option<Self>> {
        let choice = match input {
            None => None,
            Some("asc") => Some(Self::Ascending),
            Some("desc") => Some(Self::Descending),
            _ => return Err(SynError::new(span, "unreachable result")),
        };
        Ok(choice)
    }

    pub(crate) fn is_ascending(self) -> bool {
        match self {
            Self::Ascending => true,
            Self::Descending => false,
        }
    }
}

impl MethodNameConf {
    pub(crate) fn parse_from_input(
        namevalue_params: &::std::collections::HashMap<&str, String>,
        span: proc_macro2::Span,
    ) -> ParseResult<Option<Self>> {
        let name_opt = match namevalue_params.get("name") {
            None => None,
            Some(input) => Some(input.to_owned()),
        };
        let prefix_opt = match namevalue_params.get("prefix") {
            None => None,
            Some(input) => Some(input.to_owned()),
        };
        let suffix_opt = match namevalue_params.get("suffix") {
            None => None,
            Some(input) => Some(input.to_owned()),
        };
        if let Some(name) = name_opt {
            if prefix_opt.is_some() || suffix_opt.is_some() {
                Err(SynError::new(
                    span,
                    "do not set prefix or suffix if name was set",
                ))
            } else {
                Ok(Some(Self::Name(name)))
            }
        } else {
            let choice = match (prefix_opt, suffix_opt) {
                (Some(prefix), Some(suffix)) => Some(Self::Format { prefix, suffix }),
                (Some(prefix), None) => Some(Self::Format {
                    prefix,
                    suffix: "".to_owned(),
                }),
                (None, Some(suffix)) => Some(Self::Format {
                    prefix: "".to_owned(),
                    suffix,
                }),
                (None, None) => None,
            };
            Ok(choice)
        }
    }

    pub(crate) fn complete(&self, field_name: &syn::Ident) -> syn::Ident {
        let method_name = match self {
            Self::Name(ref name) => name.to_owned(),
            Self::Format { prefix, suffix } => {
                format!("{}{}{}", prefix, field_name.to_string(), suffix)
            }
        };
        syn::Ident::new(&method_name, field_name.span())
    }
}

impl OrdFieldConf {
    pub(crate) fn parse_from_path_params<'a>(
        path_params: &::std::collections::HashSet<&syn::Path>,
        options: &[&'a str],
        span: proc_macro2::Span,
        is_field: bool,
    ) -> ParseResult<(Option<&'a str>, Option<usize>)> {
        let mut sort_type = None;
        let mut number_opt = None;
        for p in path_params.iter() {
            match p
                .get_ident()
                .ok_or_else(|| SynError::new(p.span(), "this attribute should be a single ident"))?
                .to_string()
                .as_str()
            {
                t if options.iter().any(|opt| *opt == t) => {
                    if sort_type.is_some() {
                        return Err(SynError::new(
                            p.span(),
                            "this kind of attribute has been set twice",
                        ));
                    }
                    for opt in options.iter() {
                        if *opt == t {
                            sort_type = Some(*opt);
                            break;
                        }
                    }
                }
                n if &n.as_bytes()[..1] == b"_" => {
                    if !is_field {
                        return Err(SynError::new(
                            p.span(),
                            "the serial number could not be set as a container attribute",
                        ));
                    } else if let Ok(n) = n[1..].parse::<usize>() {
                        if number_opt.is_some() {
                            return Err(SynError::new(
                                p.span(),
                                "the serial number has been set twice",
                            ));
                        }
                        number_opt = Some(n);
                    } else {
                        return Err(SynError::new(
                            p.span(),
                            "the serial number should be an unsigned number with a `_` prefix",
                        ));
                    }
                }
                _ => {
                    return Err(SynError::new(p.span(), "this attribute was unknown"));
                }
            }
        }
        if is_field && number_opt.is_none() {
            Err(SynError::new(span, "no serial number was set"))
        } else {
            Ok((sort_type, number_opt))
        }
    }
}

impl ::std::default::Default for FieldConf {
    fn default() -> Self {
        Self {
            get: GetFieldConf {
                vis: VisibilityConf::Crate,
                name: MethodNameConf::Format {
                    prefix: "".to_owned(),
                    suffix: "".to_owned(),
                },
                typ: GetTypeConf::NotSet,
            },
            set: SetFieldConf {
                vis: VisibilityConf::Crate,
                name: MethodNameConf::Format {
                    prefix: "set_".to_owned(),
                    suffix: "".to_owned(),
                },
                typ: SetTypeConf::Ref,
            },
            mut_: MutFieldConf {
                vis: VisibilityConf::Crate,
                name: MethodNameConf::Format {
                    prefix: "mut_".to_owned(),
                    suffix: "".to_owned(),
                },
            },
            ord: OrdFieldConf {
                number: None,
                sort_type: SortTypeConf::Ascending,
            },
            skip: false,
        }
    }
}

impl FieldConf {
    fn apply_attrs(&mut self, meta: &syn::Meta, is_field: bool) -> ParseResult<()> {
        match meta {
            syn::Meta::Path(path) => {
                if path.is_ident(SKIP) {
                    if is_field {
                        self.skip = true;
                    } else {
                        return Err(SynError::new(
                            path.span(),
                            "don't derive it, rather than use skip as a container attribute",
                        ));
                    }
                } else {
                    return Err(SynError::new(path.span(), "this attribute was unknown"));
                }
            }
            syn::Meta::List(list) => {
                let mut path_params = ::std::collections::HashSet::new();
                let mut namevalue_params = ::std::collections::HashMap::new();
                for nested_meta in list.nested.iter() {
                    match nested_meta {
                        syn::NestedMeta::Meta(meta) => match meta {
                            syn::Meta::Path(path) => {
                                if !path_params.insert(path) {
                                    return Err(SynError::new(
                                        path.span(),
                                        "this attribute has been set twice",
                                    ));
                                }
                            }
                            syn::Meta::NameValue(mnv) => {
                                let syn::MetaNameValue { path, lit, .. } = mnv;
                                if let syn::Lit::Str(content) = lit {
                                    if namevalue_params.insert(path, content).is_some() {
                                        return Err(SynError::new(
                                            path.span(),
                                            "this attribute has been set twice",
                                        ));
                                    }
                                } else {
                                    return Err(SynError::new(
                                        lit.span(),
                                        "this literal should be a string literal",
                                    ));
                                }
                            }
                            _ => {
                                return Err(SynError::new(
                                    meta.span(),
                                    "this attribute should be a path or a name-value pair",
                                ));
                            }
                        },
                        syn::NestedMeta::Lit(lit) => {
                            return Err(SynError::new(
                                lit.span(),
                                "this attribute should not be a literal",
                            ));
                        }
                    }
                }
                if path_params.is_empty() && namevalue_params.is_empty() {
                    return Err(SynError::new(
                        list.span(),
                        "this attribute should not be empty",
                    ));
                }
                match list
                    .path
                    .get_ident()
                    .ok_or_else(|| {
                        SynError::new(list.path.span(), "this attribute should be a single ident")
                    })?
                    .to_string()
                    .as_ref()
                {
                    "get" => {
                        let paths = check_path_params(&path_params, &[VISIBILITY_OPTIONS])?;
                        let namevalues = check_namevalue_params(
                            &namevalue_params,
                            &[NAME_OPTION, PREFIX_OPTION, SUFFIX_OPTION, GET_TYPE_OPTIONS],
                        )?;
                        if let Some(choice) =
                            VisibilityConf::parse_from_input(paths[0], list.path.span())?
                        {
                            self.get.vis = choice;
                        }
                        if let Some(choice) =
                            MethodNameConf::parse_from_input(&namevalues, list.path.span())?
                        {
                            self.get.name = choice;
                        }
                        if let Some(choice) =
                            GetTypeConf::parse_from_input(&namevalues, list.path.span())?
                        {
                            self.get.typ = choice;
                        }
                    }
                    "set" => {
                        let paths = check_path_params(&path_params, &[VISIBILITY_OPTIONS])?;
                        let namevalues = check_namevalue_params(
                            &namevalue_params,
                            &[NAME_OPTION, PREFIX_OPTION, SUFFIX_OPTION, SET_TYPE_OPTIONS],
                        )?;
                        if let Some(choice) =
                            VisibilityConf::parse_from_input(paths[0], list.path.span())?
                        {
                            self.set.vis = choice;
                        }
                        if let Some(choice) =
                            MethodNameConf::parse_from_input(&namevalues, list.path.span())?
                        {
                            self.set.name = choice;
                        }
                        if let Some(choice) =
                            SetTypeConf::parse_from_input(&namevalues, list.path.span())?
                        {
                            self.set.typ = choice;
                        }
                    }
                    "mut" => {
                        let paths = check_path_params(&path_params, &[VISIBILITY_OPTIONS])?;
                        let namevalues = check_namevalue_params(
                            &namevalue_params,
                            &[NAME_OPTION, PREFIX_OPTION, SUFFIX_OPTION],
                        )?;
                        if let Some(choice) =
                            VisibilityConf::parse_from_input(paths[0], list.path.span())?
                        {
                            self.mut_.vis = choice;
                        }
                        if let Some(choice) =
                            MethodNameConf::parse_from_input(&namevalues, list.path.span())?
                        {
                            self.mut_.name = choice;
                        }
                    }
                    "ord" => {
                        let (sort_type_opt, number_opt) = OrdFieldConf::parse_from_path_params(
                            &path_params,
                            SORT_TYPE_OPTIONS,
                            list.path.span(),
                            is_field,
                        )?;
                        if let Some(choice) =
                            SortTypeConf::parse_from_input(sort_type_opt, list.path.span())?
                        {
                            self.ord.sort_type = choice;
                        }
                        self.ord.number = number_opt;
                    }
                    attr => {
                        return Err(SynError::new(
                            list.path.span(),
                            format!("unsupport attribute `{}`", attr),
                        ));
                    }
                }
            }
            syn::Meta::NameValue(name_value) => {
                return Err(SynError::new(
                    name_value.span(),
                    "this attribute should not be a name-value pair",
                ));
            }
        }
        Ok(())
    }
}

fn check_path_params<'a>(
    path_params: &::std::collections::HashSet<&syn::Path>,
    options: &[&[&'a str]],
) -> ParseResult<Vec<Option<&'a str>>> {
    let mut result = vec![None; options.len()];
    let mut find;
    for p in path_params.iter() {
        find = false;
        for (i, group) in options.iter().enumerate() {
            for opt in group.iter() {
                if p.is_ident(opt) {
                    find = true;
                    if result[i].is_some() {
                        return Err(SynError::new(
                            p.span(),
                            "this kind of attribute has been set twice",
                        ));
                    }
                    result[i] = Some(*opt);
                    break;
                }
            }
            if find {
                break;
            }
        }
        if !find {
            return Err(SynError::new(p.span(), "this attribute was unknown"));
        }
    }
    Ok(result)
}

fn check_namevalue_params<'a>(
    params: &::std::collections::HashMap<&syn::Path, &syn::LitStr>,
    options: &[(&'a str, Option<&[&'a str]>)],
) -> ParseResult<::std::collections::HashMap<&'a str, String>> {
    let mut result = ::std::collections::HashMap::new();
    let mut find;
    for (n, v) in params.iter() {
        find = false;
        let value = v.value();
        for (k, group_opt) in options.iter() {
            if n.is_ident(k) {
                if let Some(group) = group_opt {
                    for opt in group.iter() {
                        if &value == opt {
                            let _ = result.insert(*k, value.clone());
                            find = true;
                            break;
                        }
                    }
                    if find {
                        break;
                    }
                } else {
                    let _ = result.insert(*k, value);
                    find = true;
                    break;
                }
            }
        }
        if !find {
            return Err(SynError::new(n.span(), "this attribute was unknown"));
        }
    }
    Ok(result)
}

fn parse_attrs(
    span: proc_macro2::Span,
    mut conf: FieldConf,
    attrs: &[syn::Attribute],
    is_field: bool,
) -> ParseResult<FieldConf> {
    for attr in attrs.iter() {
        if let syn::AttrStyle::Outer = attr.style {
            let meta = attr
                .parse_meta()
                .map_err(|_| SynError::new(span, "failed to parse the attributes"))?;
            match meta {
                syn::Meta::Path(path) => {
                    if path.is_ident(ATTR_NAME) {
                        return Err(SynError::new(
                            path.span(),
                            "the attribute should not be a path",
                        ));
                    }
                }
                syn::Meta::List(list) => {
                    if list.path.is_ident(ATTR_NAME) {
                        if list.nested.is_empty() {
                            return Err(SynError::new(
                                list.span(),
                                "this attribute should not be empty",
                            ));
                        }
                        for nested_meta in list.nested.iter() {
                            match nested_meta {
                                syn::NestedMeta::Meta(meta) => {
                                    conf.apply_attrs(meta, is_field)?;
                                }
                                syn::NestedMeta::Lit(lit) => {
                                    return Err(SynError::new(
                                        lit.span(),
                                        "the attribute in nested meta should not be a literal",
                                    ));
                                }
                            }
                        }
                    }
                }
                syn::Meta::NameValue(name_value) => {
                    if name_value.path.is_ident(ATTR_NAME) {
                        return Err(SynError::new(
                            name_value.span(),
                            "the attribute should not be a name-value pair",
                        ));
                    }
                }
            }
        }
    }
    Ok(conf)
}