Skip to content

Commit

Permalink
Update syn related dependencies to 1.0 and bump versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Eijebong committed Aug 15, 2019
1 parent 0528752 commit cc4d553
Show file tree
Hide file tree
Showing 26 changed files with 195 additions and 187 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "darling"
version = "0.9.0"
version = "0.10.0"
authors = ["Ted Driggs <[email protected]>"]
repository = "https://github.com/TedDriggs/darling"
documentation = "https://docs.rs/darling/0.9.0"
documentation = "https://docs.rs/darling/0.10.0"
description = """
A proc-macro library for reading attributes into structs when
implementing custom derives.
Expand All @@ -17,13 +17,13 @@ travis-ci = { repository = "TedDriggs/darling" }
maintenance = { status = "actively-developed" }

[dependencies]
darling_core = { version = "=0.9.0", path = "core" }
darling_macro = { version = "=0.9.0", path = "macro" }
darling_core = { version = "=0.10.0", path = "core" }
darling_macro = { version = "=0.10.0", path = "macro" }

[dev-dependencies]
proc-macro2 = "0.4"
quote = "0.6"
syn = "0.15.26"
proc-macro2 = "1"
quote = "1"
syn = "1"

[features]
default = ["suggestions"]
Expand Down
10 changes: 5 additions & 5 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "darling_core"
version = "0.9.0"
version = "0.10.0"
authors = ["Ted Driggs <[email protected]>"]
repository = "https://github.com/TedDriggs/darling"
description = """
Expand All @@ -15,8 +15,8 @@ suggestions = ["strsim"]

[dependencies]
ident_case = "1.0.0"
proc-macro2 = "0.4.26"
quote = "0.6"
syn = { version = "0.15.26", features = ["full", "extra-traits"] }
proc-macro2 = "1"
quote = "1"
syn = { version = "1", features = ["full", "extra-traits"] }
fnv = "1.0.6"
strsim = { version = "0.7.0", optional = true }
strsim = { version = "0.7.0", optional = true }
6 changes: 3 additions & 3 deletions core/src/codegen/attr_extractor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::TokenStream;

use options::ForwardAttrs;
use util::IdentList;
use util::PathList;

/// Infrastructure for generating an attribute extractor.
pub trait ExtractAttribute {
Expand All @@ -14,7 +14,7 @@ pub trait ExtractAttribute {
fn immutable_declarations(&self) -> TokenStream;

/// Gets the list of attribute names that should be parsed by the extractor.
fn attr_names(&self) -> &IdentList;
fn attr_names(&self) -> &PathList;

fn forwarded_attrs(&self) -> Option<&ForwardAttrs>;

Expand Down Expand Up @@ -57,7 +57,7 @@ pub trait ExtractAttribute {
let core_loop = self.core_loop();
quote!(
#(#attr_names)|* => {
if let ::darling::export::Some(::syn::Meta::List(ref __data)) = __attr.interpret_meta() {
if let ::darling::export::Ok(::syn::Meta::List(ref __data)) = __attr.parse_meta() {
let __items = &__data.nested;

#core_loop
Expand Down
6 changes: 3 additions & 3 deletions core/src/codegen/from_derive_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use syn::{self, Ident};
use ast::Data;
use codegen::{ExtractAttribute, OuterFromImpl, TraitImpl};
use options::{ForwardAttrs, Shape};
use util::IdentList;
use util::PathList;

pub struct FromDeriveInputImpl<'a> {
pub ident: Option<&'a Ident>,
Expand All @@ -14,7 +14,7 @@ pub struct FromDeriveInputImpl<'a> {
pub attrs: Option<&'a Ident>,
pub data: Option<&'a Ident>,
pub base: TraitImpl<'a>,
pub attr_names: &'a IdentList,
pub attr_names: &'a PathList,
pub forward_attrs: Option<&'a ForwardAttrs>,
pub from_ident: bool,
pub supports: Option<&'a Shape>,
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<'a> ToTokens for FromDeriveInputImpl<'a> {
}

impl<'a> ExtractAttribute for FromDeriveInputImpl<'a> {
fn attr_names(&self) -> &IdentList {
fn attr_names(&self) -> &PathList {
&self.attr_names
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/codegen/from_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use syn::{self, Ident};

use codegen::{ExtractAttribute, OuterFromImpl, TraitImpl};
use options::ForwardAttrs;
use util::IdentList;
use util::PathList;

/// `impl FromField` generator. This is used for parsing an individual
/// field and its attributes.
Expand All @@ -14,7 +14,7 @@ pub struct FromFieldImpl<'a> {
pub ty: Option<&'a Ident>,
pub attrs: Option<&'a Ident>,
pub base: TraitImpl<'a>,
pub attr_names: &'a IdentList,
pub attr_names: &'a PathList,
pub forward_attrs: Option<&'a ForwardAttrs>,
pub from_ident: bool,
}
Expand Down Expand Up @@ -75,7 +75,7 @@ impl<'a> ToTokens for FromFieldImpl<'a> {
}

impl<'a> ExtractAttribute for FromFieldImpl<'a> {
fn attr_names(&self) -> &IdentList {
fn attr_names(&self) -> &PathList {
&self.attr_names
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/codegen/from_meta_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<'a> ToTokens for FromMetaImpl<'a> {
0 => ::darling::export::Err(::darling::Error::too_few_items(1)),
1 => {
if let ::syn::NestedMeta::Meta(ref __nested) = __outer[0] {
match __nested.name().to_string().as_ref() {
match __nested.path().segments.iter().map(|s| s.ident.to_string()).collect::<Vec<String>>().join("::").as_ref() {
#(#struct_arms)*
__other => ::darling::export::Err(::darling::Error::#unknown_variant_err.with_span(__nested))
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/codegen/from_type_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use syn::{self, Ident};

use codegen::{ExtractAttribute, OuterFromImpl, TraitImpl};
use options::ForwardAttrs;
use util::IdentList;
use util::PathList;

pub struct FromTypeParamImpl<'a> {
pub base: TraitImpl<'a>,
pub ident: Option<&'a Ident>,
pub attrs: Option<&'a Ident>,
pub bounds: Option<&'a Ident>,
pub default: Option<&'a Ident>,
pub attr_names: &'a IdentList,
pub attr_names: &'a PathList,
pub forward_attrs: Option<&'a ForwardAttrs>,
pub from_ident: bool,
}
Expand Down Expand Up @@ -74,7 +74,7 @@ impl<'a> ToTokens for FromTypeParamImpl<'a> {
}

impl<'a> ExtractAttribute for FromTypeParamImpl<'a> {
fn attr_names(&self) -> &IdentList {
fn attr_names(&self) -> &PathList {
&self.attr_names
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/codegen/from_variant_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use syn::{self, Ident};

use codegen::{ExtractAttribute, OuterFromImpl, TraitImpl};
use options::{DataShape, ForwardAttrs};
use util::IdentList;
use util::PathList;

pub struct FromVariantImpl<'a> {
pub base: TraitImpl<'a>,
pub ident: Option<&'a Ident>,
pub fields: Option<&'a Ident>,
pub attrs: Option<&'a Ident>,
pub attr_names: &'a IdentList,
pub attr_names: &'a PathList,
pub forward_attrs: Option<&'a ForwardAttrs>,
pub from_ident: bool,
pub supports: Option<&'a DataShape>,
Expand Down Expand Up @@ -86,7 +86,7 @@ impl<'a> ExtractAttribute for FromVariantImpl<'a> {
self.base.immutable_declarations()
}

fn attr_names(&self) -> &IdentList {
fn attr_names(&self) -> &PathList {
&self.attr_names
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/codegen/outer_from_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn compute_impl_bounds(bound: Path, mut generics: Generics, applies_to: &IdentSe
path: bound,
});

for mut param in generics.params.iter_mut() {
for param in generics.params.iter_mut() {
if let GenericParam::Type(ref mut typ) = *param {
if applies_to.contains(&typ.ident) {
typ.bounds.push(added_bound.clone());
Expand Down
5 changes: 4 additions & 1 deletion core/src/codegen/variant_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,20 @@ impl<'a> FieldsGen<'a> {
quote!(unknown_field(__other))
} else {
let names = self.fields.as_ref().map(Field::as_name);
let names = names.iter();
quote!(unknown_field_with_alts(__other, &[#(#names),*]))
};

quote! {
__errors.push(::darling::Error::#err_fn.with_span(__inner));
}
};
let arms = arms.iter();

quote!(
for __item in __items {
if let ::syn::NestedMeta::Meta(ref __inner) = *__item {
let __name = __inner.name().to_string();
let __name = __inner.path().segments.iter().map(|s| s.ident.to_string()).collect::<Vec<String>>().join("::");
match __name.as_str() {
#(#arms)*
__other => { #handle_unknown }
Expand All @@ -82,6 +84,7 @@ impl<'a> FieldsGen<'a> {

pub(in codegen) fn initializers(&self) -> TokenStream {
let inits = self.fields.as_ref().map(Field::as_initializer);
let inits = inits.iter();

quote!(#(#inits),*)
}
Expand Down
26 changes: 25 additions & 1 deletion core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::iter::{self, Iterator};
use std::string::ToString;
use std::vec;
use syn::spanned::Spanned;
use syn::{Lit, LitStr};
use syn::{Lit, LitStr, Path};

mod kind;

Expand All @@ -35,6 +35,11 @@ pub struct Error {
span: Option<Span>,
}

/// Transform a syn::Path to a readable String
fn path_to_string(path: &syn::Path) -> String {
path.segments.iter().map(|s| s.ident.to_string()).collect::<Vec<String>>().join("::")
}

/// Error creation functions
impl Error {
pub(in error) fn new(kind: ErrorKind) -> Self {
Expand All @@ -55,6 +60,12 @@ impl Error {
Error::new(ErrorKind::DuplicateField(name.into()))
}

/// Creates a new error for a field that appears twice in the input. Helper to avoid repeating
/// the syn::Path to String conversion.
pub fn duplicate_field_path(path: &Path) -> Self {
Error::duplicate_field(&path_to_string(path))
}

/// Creates a new error for a non-optional field that does not appear in the input.
pub fn missing_field(name: &str) -> Self {
Error::new(ErrorKind::MissingField(name.into()))
Expand All @@ -66,6 +77,12 @@ impl Error {
Error::new(ErrorKind::UnknownField(name.into()))
}

/// Creates a new error for a field name that appears in the input but does not correspond
/// to a known field. Helper to avoid repeating the syn::Path to String conversion.
pub fn unknown_field_path(path: &Path) -> Self {
Error::unknown_field(&path_to_string(path))
}

/// Creates a new error for a field name that appears in the input but does not correspond to
/// a known attribute. The second argument is the list of known attributes; if a similar name
/// is found that will be shown in the emitted error message.
Expand Down Expand Up @@ -227,6 +244,13 @@ impl Error {
self
}

/// Adds a location to the error, such as a field or variant.
/// Locations must be added in reverse order of specificity. This is a helper function to avoid
/// repeating path to string logic.
pub fn at_path(self, path: &Path) -> Self {
self.at(path_to_string(path))
}

/// Gets the number of individual errors in this error.
///
/// This function never returns `0`, as it's impossible to construct
Expand Down
20 changes: 11 additions & 9 deletions core/src/from_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use {Error, Result};
pub trait FromMeta: Sized {
fn from_nested_meta(item: &NestedMeta) -> Result<Self> {
(match *item {
NestedMeta::Literal(ref lit) => Self::from_value(lit),
NestedMeta::Lit(ref lit) => Self::from_value(lit),
NestedMeta::Meta(ref mi) => Self::from_meta(mi),
})
.map_err(|e| e.with_span(item))
Expand All @@ -60,7 +60,7 @@ pub trait FromMeta: Sized {
/// source code.
fn from_meta(item: &Meta) -> Result<Self> {
(match *item {
Meta::Word(_) => Self::from_word(),
Meta::Path(_) => Self::from_word(),
Meta::List(ref value) => Self::from_list(
&value
.nested
Expand Down Expand Up @@ -168,7 +168,7 @@ macro_rules! from_meta_num {
fn from_value(value: &Lit) -> Result<Self> {
(match *value {
Lit::Str(ref s) => Self::from_string(&s.value()),
Lit::Int(ref s) => Ok(s.value() as $ty),
Lit::Int(ref s) => Ok(s.base10_parse::<$ty>().unwrap()),
_ => Err(Error::unexpected_lit_type(value)),
})
.map_err(|e| e.with_span(value))
Expand Down Expand Up @@ -200,7 +200,7 @@ macro_rules! from_meta_float {
fn from_value(value: &Lit) -> Result<Self> {
(match *value {
Lit::Str(ref s) => Self::from_string(&s.value()),
Lit::Float(ref s) => Ok(s.value() as $ty),
Lit::Float(ref s) => Ok(s.base10_parse::<$ty>().unwrap()),
_ => Err(Error::unexpected_lit_type(value)),
})
.map_err(|e| e.with_span(value))
Expand Down Expand Up @@ -276,7 +276,7 @@ from_meta_lit!(syn::LitByte, Lit::Byte);
from_meta_lit!(syn::LitByteStr, Lit::ByteStr);
from_meta_lit!(syn::LitChar, Lit::Char);
from_meta_lit!(syn::LitBool, Lit::Bool);
from_meta_lit!(syn::LitVerbatim, Lit::Verbatim);
from_meta_lit!(proc_macro2::Literal, Lit::Verbatim);

impl FromMeta for syn::Meta {
fn from_meta(value: &syn::Meta) -> Result<Self> {
Expand Down Expand Up @@ -354,16 +354,18 @@ impl<V: FromMeta, S: BuildHasher + Default> FromMeta for HashMap<String, V, S> {
let mut map = HashMap::with_capacity_and_hasher(nested.len(), Default::default());
for item in nested {
if let syn::NestedMeta::Meta(ref inner) = *item {
match map.entry(inner.name().to_string()) {
let path = inner.path();
let name = path.segments.iter().map(|s| s.ident.to_string()).collect::<Vec<String>>().join("::");
match map.entry(name) {
Entry::Occupied(_) => {
return Err(
Error::duplicate_field(&inner.name().to_string()).with_span(inner)
Error::duplicate_field_path(&path).with_span(inner)
);
}
Entry::Vacant(entry) => {
// In the error case, extend the error's path, but assume the inner `from_meta`
// set the span, and that subsequently we don't have to.
entry.insert(FromMeta::from_meta(inner).map_err(|e| e.at(inner.name()))?);
entry.insert(FromMeta::from_meta(inner).map_err(|e| e.at_path(&path))?);
}
}
}
Expand All @@ -385,7 +387,7 @@ mod tests {
/// parse a string as a syn::Meta instance.
fn pm(tokens: TokenStream) -> ::std::result::Result<syn::Meta, String> {
let attribute: syn::Attribute = parse_quote!(#[#tokens]);
attribute.interpret_meta().ok_or("Unable to parse".into())
attribute.parse_meta().ok_or("Unable to parse".into())
}

fn fm<T: FromMeta>(tokens: TokenStream) -> T {
Expand Down
Loading

0 comments on commit cc4d553

Please sign in to comment.