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
use either_n::Either2;
use proc_macro2::Ident;
use syn::Attribute;

/// The data type the field is under
pub enum Structure<'a> {
    Struct {
        struct_name: &'a Ident,
        struct_attrs: &'a Vec<Attribute>,
    },
    EnumVariant {
        enum_name: &'a Ident,
        enum_attrs: &'a Vec<Attribute>,
        variant_name: &'a Ident,
        variant_attrs: &'a Vec<Attribute>,
    },
}

impl<'a> Structure<'a> {
    /// Returns attributes on the declaration of the fields AND if a enum on the enum item
    pub fn all_attributes(&'a self) -> impl Iterator<Item = &'a Attribute> {
        match self {
            Structure::Struct { struct_attrs, .. } => Either2::One(struct_attrs.iter()),
            Structure::EnumVariant {
                enum_attrs,
                variant_attrs,
                ..
            } => Either2::Two(enum_attrs.iter().chain(variant_attrs.iter())),
        }
    }

    /// Returns attributes on the declaration of the fields
    pub fn field_attributes(&'a self) -> impl Iterator<Item = &'a Attribute> {
        match self {
            Structure::Struct {
                struct_attrs: attrs,
                ..
            }
            | Structure::EnumVariant {
                variant_attrs: attrs,
                ..
            } => attrs.iter(),
        }
    }

    /// Struct name or just the variant
    pub fn own_name(&self) -> String {
        match self {
            Structure::Struct {
                struct_name: name, ..
            }
            | Structure::EnumVariant {
                variant_name: name, ..
            } => name.to_string(),
        }
    }

    /// Struct name or enum and variant name concatenated with double colon
    pub fn full_name(&self) -> String {
        match self {
            Structure::Struct { struct_name, .. } => struct_name.to_string(),
            Structure::EnumVariant {
                enum_name,
                variant_name,
                ..
            } => format!("{}::{}", enum_name, variant_name),
        }
    }
}