Skip to content

Commit

Permalink
Merge pull request #70 from TedDriggs/v0.9-no-rustc-bump
Browse files Browse the repository at this point in the history
v0.9
  • Loading branch information
TedDriggs authored Mar 20, 2019
2 parents c00ed9e + cd92bc7 commit c9ef060
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 52 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ env:
# Test with and without the suggestion feature enabled
# See https://github.com/servo/rust-smallvec/blob/master/.travis.yml for guide
script: |
cargo test --verbose &&
([ $TRAVIS_RUST_VERSION = 1.18.0 ] || cargo test --verbose --features suggestions)
cargo test --verbose
# To enable version-specific feature tests, add `&&` to the script, followed by this line:
# ([ $TRAVIS_RUST_VERSION = 1.18.0 ] || cargo test --verbose --features suggestions)
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v0.9.0 (March 20, 2019)
- Enable "did you mean" suggestions by default
- Make `darling_core::{codegen, options}` private [#58](https://github.com/TedDriggs/darling/issues/58)
- Fix `Override::as_mut`: [#66](https://github.com/TedDriggs/darling/issues/66)

## v0.8.6 (March 18, 2019)
- Added "did you mean" suggestions for unknown fields behind the `suggestions` flag [#60](https://github.com/TedDriggs/issues/60)
- Added `Error::unknown_field_with_alts` to support the suggestion use-case.
Expand Down
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "darling"
version = "0.8.6"
version = "0.9.0"
authors = ["Ted Driggs <ted.driggs@outlook.com>"]
repository = "https://github.com/TedDriggs/darling"
documentation = "https://docs.rs/darling/0.8.6"
documentation = "https://docs.rs/darling/0.9.0"
description = """
A proc-macro library for reading attributes into structs when
implementing custom derives.
Expand All @@ -14,17 +14,19 @@ exclude = ["/.travis.yml", "/publish.sh"]

[badges]
travis-ci = { repository = "TedDriggs/darling" }
maintenance = { status = "actively-developed" }

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

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

[features]
default = ["suggestions"]
diagnostics = ["darling_core/diagnostics"]
suggestions = ["darling_core/suggestions"]

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ Darling

[![Build Status](https://travis-ci.org/TedDriggs/darling.svg?branch=master)](https://travis-ci.org/TedDriggs/darling)
[![Latest Version](https://img.shields.io/crates/v/darling.svg)](https://crates.io/crates/darling)
[![Rustc Version 1.18+](https://img.shields.io/badge/rustc-1.18+-lightgray.svg)](https://blog.rust-lang.org/2017/06/08/Rust-1.18.html)

`darling` is a crate for proc macro authors, which enables parsing attributes into structs. It is heavily inspired by `serde` both in its internals and in its API.

# Benefits
* Easy and declarative parsing of macro input - make your proc-macros highly controllable with minimal time investment.
* Great validation and errors, no work required. When users of your proc-macro make a mistake, `darling` makes sure they get error markers at the right place in their source, and provides "did you mean" suggestions for misspelled fields.

# Usage
`darling` provides a set of traits which can be derived or manually implemented.

Expand All @@ -14,6 +19,11 @@ Darling
3. `FromField` is implemented or derived by each proc-macro crate which depends on `darling`. Structs deriving this trait will get access to the identity (if it exists), type, and visibility of the field.
4. `FromVariant` is implemented or derived by each proc-macro crate which depends on `darling`. Structs deriving this trait will get access to the identity and contents of the variant, which can be transformed the same as any other `darling` input.

## Additional Modules
* `darling::ast` provides generic types for representing the AST.
* `darling::usage` provides traits and functions for determining where type parameters and lifetimes are used in a struct or enum.
* `darling::util` provides helper types with special `FromMeta` implementations, such as `IdentList`.

# Example

```rust,ignore
Expand Down Expand Up @@ -100,3 +110,4 @@ Darling's features are built to work well for real-world projects.
* **Skip fields**: Use `#[darling(skip)]` to mark a field that shouldn't be read from attribute meta-items.
* **Multiple-occurrence fields**: Use `#[darling(multiple)]` on a `Vec` field to allow that field to appear multiple times in the meta-item. Each occurrence will be pushed into the `Vec`.
* **Span access**: Use `darling::util::SpannedValue` in a struct to get access to that meta item's source code span. This can be used to emit warnings that point at a specific field from your proc macro. In addition, you can use `darling::Error::write_errors` to automatically get precise error location details in most cases.
* **"Did you mean" suggestions**: Compile errors from derived darling trait impls include suggestions for misspelled fields.
9 changes: 3 additions & 6 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "darling_core"
version = "0.8.6"
version = "0.9.0"
authors = ["Ted Driggs <ted.driggs@outlook.com>"]
repository = "https://github.com/TedDriggs/darling"
description = """
Expand All @@ -10,16 +10,13 @@ implementing custom derives. Use https://crates.io/crates/darling in your code.
license = "MIT"

[features]
# temporary hack to make Racer autocomplete work; it requires a 3-part version
# number and doesn't allow for any feature declarations.
default = ["syn/full"]
diagnostics = []
suggestions = ["strsim"]

[dependencies]
ident_case = "1.0.0"
proc-macro2 = "0.4.26"
quote = "0.6"
syn = { version = "0.15.26", features = ["extra-traits"] }
syn = { version = "0.15.26", features = ["full", "extra-traits"] }
fnv = "1.0.6"
strsim = { version = "0.8.0", optional = true }
strsim = { version = "0.7.0", optional = true }
6 changes: 0 additions & 6 deletions core/src/ast/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ pub enum Data<V, F> {
Struct(Fields<F>),
}

#[deprecated(since = "0.3.0", note = "this has been renamed to Data")]
pub type Body<V, F> = Data<V, F>;

impl<V, F> Data<V, F> {
/// Creates an empty body of the same shape as the passed-in body.
pub fn empty_from(src: &syn::Data) -> Self {
Expand Down Expand Up @@ -159,9 +156,6 @@ pub struct Fields<T> {
pub fields: Vec<T>,
}

#[deprecated(since = "0.3.0", note = "this has been renamed to Fields")]
pub type VariantData<T> = Fields<T>;

impl<T> Fields<T> {
pub fn empty_from(vd: &syn::Fields) -> Self {
Fields {
Expand Down
9 changes: 3 additions & 6 deletions core/src/codegen/trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@ impl<'a> TraitImpl<'a> {
.collect()
}

/// Get the type parameters which are used by non-skipped fields.
/// Get the type parameters which are used by non-skipped, non-magic fields.
/// These type parameters will have a `FromMeta` bound applied to them in emitted
/// code.
pub fn used_type_params(&self) -> IdentSet {
self.type_params_matching(|f| !f.skip, |v| !v.skip)
}

/// Get the type parameters which are used by skipped fields.
pub fn skipped_type_params(&self) -> IdentSet {
self.type_params_matching(|f| f.skip, |v| v.skip)
}

fn type_params_matching<'b, F, V>(&'b self, field_filter: F, variant_filter: V) -> IdentSet
where
F: Fn(&&Field) -> bool,
Expand Down
11 changes: 1 addition & 10 deletions core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl Error {
///
/// impl FromMeta for Foo {
/// fn from_value(value: &Lit) -> Result<Self> {
/// if let Lit::Str(ref lit_str) = value {
/// if let Lit::Str(ref lit_str) = *value {
/// Ok(Foo(lit_str.value()))
/// } else {
/// Err(Error::unexpected_lit_type(value))
Expand Down Expand Up @@ -183,15 +183,6 @@ impl Error {
self.span.is_some()
}

/// Override the source code location of this error with a new one.
#[deprecated(
since = "0.8.3",
note = "Callers should not broaden error spans. Use with_span instead."
)]
pub fn set_span(&mut self, span: Span) {
self.span = Some(span)
}

/// Tie a span to the error if none is already present. This is used in `darling::FromMeta`
/// and other traits to attach errors to the most specific possible location in the input
/// source code.
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod macros_private;
mod macros_public;

pub mod ast;
pub mod codegen;
pub(crate) mod codegen;
pub mod derive;
pub mod error;
mod from_derive_input;
Expand All @@ -29,7 +29,7 @@ mod from_generics;
mod from_meta;
mod from_type_param;
mod from_variant;
pub mod options;
pub(crate) mod options;
pub mod usage;
pub mod util;

Expand Down
26 changes: 17 additions & 9 deletions core/src/options/shape.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
//! Types for "shape" validation. This allows types deriving `FromDeriveInput` etc. to declare
//! that they only work on - for example - structs with named fields, or newtype enum variants.
use proc_macro2::TokenStream;
use quote::{ToTokens, TokenStreamExt};
use syn::{Meta, NestedMeta};

use {Error, FromMeta, Result};

/// Receiver struct for shape validation. Shape validation allows a deriving type
/// to declare that it only accepts - for example - named structs, or newtype enum
/// variants.
///
/// # Usage
/// Because `Shape` implements `FromMeta`, the name of the field where it appears is
/// controlled by the struct that declares `Shape` as a member. That field name is
/// shown as `ignore` below.
///
/// ```rust,ignore
/// #[ignore(any, struct_named, enum_newtype)]
/// ```
#[derive(Debug, Clone)]
pub struct Shape {
enum_values: DataShape,
struct_values: DataShape,
any: bool,
}

impl Shape {
pub fn all() -> Self {
Shape {
any: true,
..Default::default()
}
}
}

impl Default for Shape {
fn default() -> Self {
Shape {
Expand Down Expand Up @@ -97,6 +103,8 @@ impl ToTokens for Shape {
}
}

/// Receiver for shape information within a struct or enum context. See `Shape` for more information
/// on valid uses of shape validation.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DataShape {
/// The kind of shape being described. This can be `struct_` or `enum_`.
Expand Down
2 changes: 1 addition & 1 deletion core/src/usage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
//! .params
//! .into_iter()
//! .filter(|gp| {
//! match gp {
//! match *gp {
//! GenericParam::Type(ref ty) => int_type_params.contains(&ty.ident),
//! GenericParam::Lifetime(ref lt) => int_lifetimes.contains(&lt.lifetime),
//! _ => true,
Expand Down
5 changes: 3 additions & 2 deletions core/src/util/ident_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ impl FromMeta for IdentList {
mod tests {
use super::IdentList;
use proc_macro2::TokenStream;
use syn::{Attribute, Meta};
use FromMeta;

/// 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]);
fn pm(tokens: TokenStream) -> ::std::result::Result<Meta, String> {
let attribute: Attribute = parse_quote!(#[#tokens]);
attribute.interpret_meta().ok_or("Unable to parse".into())
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/util/over_ride.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<T> Override<T> {
/// Converts from `Override<T>` to `Override<&mut T>`.
///
/// Produces a new `Override`, containing a mutable reference into the original.
pub fn as_mut<'a>(&'a mut self) -> Override<&'a T> {
pub fn as_mut<'a>(&'a mut self) -> Override<&'a mut T> {
match *self {
Inherit => Inherit,
Explicit(ref mut val) => Explicit(val),
Expand Down
4 changes: 2 additions & 2 deletions macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "darling_macro"
version = "0.8.6"
version = "0.9.0"
authors = ["Ted Driggs <ted.driggs@outlook.com>"]
repository = "https://github.com/TedDriggs/darling"
description = """
Expand All @@ -12,7 +12,7 @@ license = "MIT"
[dependencies]
quote = "0.6"
syn = "0.15.26"
darling_core = { version = "=0.8.6", path = "../core" }
darling_core = { version = "=0.9.0", path = "../core" }

[lib]
proc-macro = true
2 changes: 1 addition & 1 deletion tests/supports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct StructContainer {
}

mod source {
use syn::{self, DeriveInput};
use syn::DeriveInput;

pub fn newtype_enum() -> DeriveInput {
parse_quote!{
Expand Down

0 comments on commit c9ef060

Please sign in to comment.