Files
ansi_term
anyhow
atty
bitflags
bstr
byteorder
cargo_metadata
cargo_preflight
cfg_if
clap
csv
csv_core
darling
darling_core
darling_macro
dlopen
dlopen_derive
fnv
getrandom
glob
heck
ident_case
indoc
itoa
lazy_static
lerp
lerp_derive
libc
libm
maplit
memchr
num_traits
open
pest
pest_derive
pest_generator
pest_meta
ppv_lite86
preflight
preflight_macros
proc_macro2
proc_macro_error
proc_macro_error_attr
quote
rand
rand_chacha
rand_core
regex_automata
ryu
semver
semver_parser
serde
serde_derive
serde_json
smawk
strsim
structopt
structopt_derive
syn
termcolor
textwrap
timescale
timescale_macros
typenum
ucd_trie
unicode_segmentation
unicode_width
unicode_xid
unindent
uom
uuid
vec_map
  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
use ident_case::RenameRule;
use syn;

use ast::{Data, Fields, Style};
use codegen;
use options::{DefaultExpression, InputField, InputVariant, ParseAttribute, ParseData};
use util::Flag;
use {Error, FromMeta, Result};

/// A struct or enum which should have `FromMeta` or `FromDeriveInput` implementations
/// generated.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Core {
    /// The type identifier.
    pub ident: syn::Ident,

    /// The type's generics. If the type does not use any generics, this will
    /// be an empty instance.
    pub generics: syn::Generics,

    /// Controls whether missing properties should cause errors or should be filled by
    /// the result of a function call. This can be overridden at the field level.
    pub default: Option<DefaultExpression>,

    /// The rule that should be used to rename all fields/variants in the container.
    pub rename_rule: RenameRule,

    /// An infallible function with the signature `FnOnce(T) -> T` which will be called after the
    /// target instance is successfully constructed.
    pub map: Option<syn::Path>,

    /// The body of the _deriving_ type.
    pub data: Data<InputVariant, InputField>,

    /// The custom bound to apply to the generated impl
    pub bound: Option<Vec<syn::WherePredicate>>,

    /// Whether or not unknown fields should produce an error at compilation time.
    pub allow_unknown_fields: Flag,
}

impl Core {
    /// Partially initializes `Core` by reading the identity, generics, and body shape.
    pub fn start(di: &syn::DeriveInput) -> Self {
        Core {
            ident: di.ident.clone(),
            generics: di.generics.clone(),
            data: Data::empty_from(&di.data),
            default: Default::default(),
            // See https://github.com/TedDriggs/darling/issues/10: We default to snake_case
            // for enums to help authors produce more idiomatic APIs.
            rename_rule: if let syn::Data::Enum(_) = di.data {
                RenameRule::SnakeCase
            } else {
                Default::default()
            },
            map: Default::default(),
            bound: Default::default(),
            allow_unknown_fields: Default::default(),
        }
    }

    fn as_codegen_default<'a>(&'a self) -> Option<codegen::DefaultExpression<'a>> {
        self.default.as_ref().map(|expr| match *expr {
            DefaultExpression::Explicit(ref path) => codegen::DefaultExpression::Explicit(path),
            DefaultExpression::Inherit | DefaultExpression::Trait => {
                codegen::DefaultExpression::Trait
            }
        })
    }
}

impl ParseAttribute for Core {
    fn parse_nested(&mut self, mi: &syn::Meta) -> Result<()> {
        let path = mi.path();

        if path.is_ident("default") {
            if self.default.is_some() {
                return Err(Error::duplicate_field("default").with_span(mi));
            }

            self.default = FromMeta::from_meta(mi)?;
        } else if path.is_ident("rename_all") {
            // WARNING: This may have been set based on body shape previously,
            // so an overwrite may be permissible.
            self.rename_rule = FromMeta::from_meta(mi)?;
        } else if path.is_ident("map") {
            if self.map.is_some() {
                return Err(Error::duplicate_field("map").with_span(mi));
            }

            self.map = FromMeta::from_meta(mi)?;
        } else if path.is_ident("bound") {
            self.bound = FromMeta::from_meta(mi)?;
        } else if path.is_ident("allow_unknown_fields") {
            if self.allow_unknown_fields.is_some() {
                return Err(Error::duplicate_field("allow_unknown_fields").with_span(mi));
            }

            self.allow_unknown_fields = FromMeta::from_meta(mi)?;
        } else {
            return Err(Error::unknown_field_path(&path).with_span(mi));
        }

        Ok(())
    }
}

impl ParseData for Core {
    fn parse_variant(&mut self, variant: &syn::Variant) -> Result<()> {
        let v = InputVariant::from_variant(variant, Some(&self))?;

        match self.data {
            Data::Enum(ref mut variants) => {
                variants.push(v);
                Ok(())
            }
            Data::Struct(_) => panic!("Core::parse_variant should never be called for a struct"),
        }
    }

    fn parse_field(&mut self, field: &syn::Field) -> Result<()> {
        let f = InputField::from_field(field, Some(&self))?;

        match self.data {
            Data::Struct(Fields {
                style: Style::Unit, ..
            }) => panic!("Core::parse_field should not be called on unit"),
            Data::Struct(Fields { ref mut fields, .. }) => {
                fields.push(f);
                Ok(())
            }
            Data::Enum(_) => panic!("Core::parse_field should never be called for an enum"),
        }
    }
}

impl<'a> From<&'a Core> for codegen::TraitImpl<'a> {
    fn from(v: &'a Core) -> Self {
        codegen::TraitImpl {
            ident: &v.ident,
            generics: &v.generics,
            data: v
                .data
                .as_ref()
                .map_struct_fields(InputField::as_codegen_field)
                .map_enum_variants(|variant| variant.as_codegen_variant(&v.ident)),
            default: v.as_codegen_default(),
            map: v.map.as_ref(),
            bound: v.bound.as_ref().map(|i| i.as_slice()),
            allow_unknown_fields: v.allow_unknown_fields.into(),
        }
    }
}