darling_core-0.10.2/Cargo.toml.orig010064400027200000024000000011241355634035000153230ustar0000000000000000[package] name = "darling_core" version = "0.10.2" authors = ["Ted Driggs "] repository = "https://github.com/TedDriggs/darling" description = """ Helper crate for proc-macro library for reading attributes into structs when implementing custom derives. Use https://crates.io/crates/darling in your code. """ license = "MIT" [features] diagnostics = [] suggestions = ["strsim"] [dependencies] ident_case = "1.0.0" proc-macro2 = "1" quote = "1" syn = { version = "1.0.1", features = ["full", "extra-traits"] } fnv = "1.0.6" strsim = { version = "0.9.0", optional = true } darling_core-0.10.2/Cargo.toml0000644000000022770000000000000116110ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "darling_core" version = "0.10.2" authors = ["Ted Driggs "] description = "Helper crate for proc-macro library for reading attributes into structs when\nimplementing custom derives. Use https://crates.io/crates/darling in your code.\n" license = "MIT" repository = "https://github.com/TedDriggs/darling" [dependencies.fnv] version = "1.0.6" [dependencies.ident_case] version = "1.0.0" [dependencies.proc-macro2] version = "1" [dependencies.quote] version = "1" [dependencies.strsim] version = "0.9.0" optional = true [dependencies.syn] version = "1.0.1" features = ["full", "extra-traits"] [features] diagnostics = [] suggestions = ["strsim"] darling_core-0.10.2/LICENSE010064400027200000024000000020531344377670500134570ustar0000000000000000MIT License Copyright (c) 2017 Ted Driggs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. darling_core-0.10.2/src/ast/data.rs010064400027200000024000000233361344445143000153000ustar0000000000000000use std::{slice, vec}; use syn; use usage::{ self, IdentRefSet, IdentSet, LifetimeRefSet, LifetimeSet, UsesLifetimes, UsesTypeParams, }; use {Error, FromField, FromVariant, Result}; /// A struct or enum body. /// /// `V` is the type which receives any encountered variants, and `F` receives struct fields. #[derive(Debug, Clone, PartialEq, Eq)] pub enum Data { Enum(Vec), Struct(Fields), } impl Data { /// Creates an empty body of the same shape as the passed-in body. pub fn empty_from(src: &syn::Data) -> Self { match *src { syn::Data::Enum(_) => Data::Enum(vec![]), syn::Data::Struct(ref vd) => Data::Struct(Fields::empty_from(&vd.fields)), syn::Data::Union(_) => unreachable!(), } } /// Creates a new `Data<&'a V, &'a F>` instance from `Data`. pub fn as_ref<'a>(&'a self) -> Data<&'a V, &'a F> { match *self { Data::Enum(ref variants) => Data::Enum(variants.iter().collect()), Data::Struct(ref data) => Data::Struct(data.as_ref()), } } /// Applies a function `V -> U` on enum variants, if this is an enum. pub fn map_enum_variants(self, map: T) -> Data where T: FnMut(V) -> U, { match self { Data::Enum(v) => Data::Enum(v.into_iter().map(map).collect()), Data::Struct(f) => Data::Struct(f), } } /// Applies a function `F -> U` on struct fields, if this is a struct. pub fn map_struct_fields(self, map: T) -> Data where T: FnMut(F) -> U, { match self { Data::Enum(v) => Data::Enum(v), Data::Struct(f) => Data::Struct(f.map(map)), } } /// Applies a function to the `Fields` if this is a struct. pub fn map_struct(self, mut map: T) -> Data where T: FnMut(Fields) -> Fields, { match self { Data::Enum(v) => Data::Enum(v), Data::Struct(f) => Data::Struct(map(f)), } } /// Consumes the `Data`, returning `Fields` if it was a struct. pub fn take_struct(self) -> Option> { match self { Data::Enum(_) => None, Data::Struct(f) => Some(f), } } /// Consumes the `Data`, returning `Vec` if it was an enum. pub fn take_enum(self) -> Option> { match self { Data::Enum(v) => Some(v), Data::Struct(_) => None, } } /// Returns `true` if this instance is `Data::Enum`. pub fn is_enum(&self) -> bool { match *self { Data::Enum(_) => true, Data::Struct(_) => false, } } /// Returns `true` if this instance is `Data::Struct`. pub fn is_struct(&self) -> bool { !self.is_enum() } } impl Data { /// Attempt to convert from a `syn::Data` instance. pub fn try_from(body: &syn::Data) -> Result { match *body { syn::Data::Enum(ref data) => { let mut items = Vec::with_capacity(data.variants.len()); let mut errors = Vec::new(); for v_result in data.variants.iter().map(FromVariant::from_variant) { match v_result { Ok(val) => items.push(val), Err(err) => errors.push(err), } } if !errors.is_empty() { Err(Error::multiple(errors)) } else { Ok(Data::Enum(items)) } } syn::Data::Struct(ref data) => Ok(Data::Struct(Fields::try_from(&data.fields)?)), syn::Data::Union(_) => unreachable!(), } } } impl UsesTypeParams for Data { fn uses_type_params<'a>( &self, options: &usage::Options, type_set: &'a IdentSet, ) -> IdentRefSet<'a> { match *self { Data::Struct(ref v) => v.uses_type_params(options, type_set), Data::Enum(ref v) => v.uses_type_params(options, type_set), } } } impl UsesLifetimes for Data { fn uses_lifetimes<'a>( &self, options: &usage::Options, lifetimes: &'a LifetimeSet, ) -> LifetimeRefSet<'a> { match *self { Data::Struct(ref v) => v.uses_lifetimes(options, lifetimes), Data::Enum(ref v) => v.uses_lifetimes(options, lifetimes), } } } /// Equivalent to `syn::Fields`, but replaces the AST element with a generic. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Fields { pub style: Style, pub fields: Vec, } impl Fields { pub fn empty_from(vd: &syn::Fields) -> Self { Fields { style: vd.into(), fields: Vec::new(), } } /// Splits the `Fields` into its style and fields for further processing. /// Returns an empty `Vec` for `Unit` data. pub fn split(self) -> (Style, Vec) { (self.style, self.fields) } /// Returns true if this variant's data makes it a newtype. pub fn is_newtype(&self) -> bool { self.style == Style::Tuple && self.len() == 1 } pub fn is_unit(&self) -> bool { self.style.is_unit() } pub fn is_tuple(&self) -> bool { self.style.is_tuple() } pub fn is_struct(&self) -> bool { self.style.is_struct() } pub fn as_ref<'a>(&'a self) -> Fields<&'a T> { Fields { style: self.style, fields: self.fields.iter().collect(), } } pub fn map(self, map: F) -> Fields where F: FnMut(T) -> U, { Fields { style: self.style, fields: self.fields.into_iter().map(map).collect(), } } pub fn iter(&self) -> slice::Iter { self.fields.iter() } /// Returns the number of fields in the structure. pub fn len(&self) -> usize { self.fields.len() } /// Returns `true` if the `Fields` contains no fields. pub fn is_empty(&self) -> bool { self.fields.is_empty() } } impl Fields { pub fn try_from(fields: &syn::Fields) -> Result { let (items, errors) = match *fields { syn::Fields::Named(ref fields) => { let mut items = Vec::with_capacity(fields.named.len()); let mut errors = Vec::new(); for field in &fields.named { match FromField::from_field(field) { Ok(val) => items.push(val), Err(err) => errors.push(if let Some(ref ident) = field.ident { err.at(ident) } else { err }), } } (items, errors) } syn::Fields::Unnamed(ref fields) => { let mut items = Vec::with_capacity(fields.unnamed.len()); let mut errors = Vec::new(); for field in &fields.unnamed { match FromField::from_field(field) { Ok(val) => items.push(val), Err(err) => errors.push(if let Some(ref ident) = field.ident { err.at(ident) } else { err }), } } (items, errors) } syn::Fields::Unit => (vec![], vec![]), }; if !errors.is_empty() { Err(Error::multiple(errors)) } else { Ok(Fields { style: fields.into(), fields: items, }) } } } impl IntoIterator for Fields { type Item = T; type IntoIter = vec::IntoIter; fn into_iter(self) -> Self::IntoIter { self.fields.into_iter() } } impl From