darling/lib.rs
1//! # Darling
2//! Darling is a tool for declarative attribute parsing in proc macro implementations.
3//!
4//!
5//! ## Design
6//! Darling takes considerable design inspiration from [`serde`](https://serde.rs). A data structure that can be
7//! read from any attribute implements `FromMeta` (or has an implementation automatically
8//! generated using `derive`). Any crate can provide `FromMeta` implementations, even one not
9//! specifically geared towards proc-macro authors.
10//!
11//! Proc-macro crates should provide their own structs which implement or derive `FromDeriveInput`,
12//! `FromField`, `FromVariant`, `FromGenerics`, _et alia_ to gather settings relevant to their operation.
13//!
14//! ## Attributes
15//! There are a number of attributes that `darling` exposes to enable finer-grained control over the code
16//! it generates.
17//!
18//! * **Field renaming**: You can use `#[darling(rename="new_name")]` on a field to change the name Darling looks for.
19//! You can also use `#[darling(rename_all="...")]` at the struct or enum level to apply a casing rule to all fields or variants.
20//! * **Map function**: You can use `#[darling(map="path::to::function")]` to run code on a field before it's stored in the struct.
21//! * **Default values**: You can use `#[darling(default)]` at the type or field level to use that type's default value to fill
22//! in values not specified by the caller. You can also set a custom default value by passing in a function path or a closure:
23//! `#[darling(default = path::to::function)]` or `#[darling(default = || get_default())]`.
24//! * **Skipped fields**: You can skip a variant or field using `#[darling(skip)]`. Fields marked with this will fall back to
25//! `Default::default()` for their value, but you can override that with an explicit default or a value from the type-level default.
26//! * **Custom shorthand**: Use `#[darling(from_word = ...)]` on a struct or enum to override how a simple word is interpreted.
27//! By default, it is an error for your macro's user to fail to specify the fields of your struct, but with this you can choose to
28//! instead produce a set of default values. This takes either a path or a closure whose signature matches `FromMeta::from_word`.
29//! * **Custom handling for missing fields**: When a field is not present and `#[darling(default)]` is not used, derived impls will
30//! call `FromMeta::from_none` on that field's type to try and get the fallback value for the field. Usually, there is not a fallback
31//! value, so a missing field error is generated. `Option<T: FromMeta>` uses this to make options optional without requiring
32//! `#[darling(default)]` declarations, and structs and enums can use this themselves with `#[darling(from_none = ...)]`.
33//! This takes either a path or a closure whose signature matches `FromMeta::from_none`.
34//! * **Generate `syn::parse::Parse` impl**: When deriving `FromMeta`, add `#[darling(derive_syn_parse)]` to also generate an impl
35//! of the `Parse` trait.
36//!
37//! ## Forwarded Fields
38//! All derivable traits except `FromMeta` support forwarding some fields from the input AST to the derived struct.
39//! These fields are matched up by identifier **before** `rename` attribute values are considered,
40//! allowing you to use their names for your own properties.
41//! The deriving struct is responsible for making sure the types of fields it chooses to declare are compatible with this table.
42//!
43//! A deriving struct is free to include or exclude any of the fields below.
44//!
45//! ### `FromDeriveInput`
46//! |Field name|Type|Meaning|
47//! |---|---|---|
48//! |`ident`|`syn::Ident`|The identifier of the passed-in type|
49//! |`vis`|`syn::Visibility`|The visibility of the passed-in type|
50//! |`generics`|`T: darling::FromGenerics`|The generics of the passed-in type. This can be `syn::Generics`, `darling::ast::Generics`, or any compatible type.|
51//! |`data` (or anything, using `#[darling(with = ...)]`)|`darling::ast::Data`|The body of the passed-in type|
52//! |`attrs`|`Vec<syn::Attribute>` (or anything, using `#[darling(with = ...)]`)|The forwarded attributes from the passed in type. These are controlled using the `forward_attrs` attribute.|
53//!
54//! ### `FromField`
55//! |Field name|Type|Meaning|
56//! |---|---|---|
57//! |`ident`|`Option<syn::Ident>`|The identifier of the passed-in field, or `None` for tuple fields|
58//! |`vis`|`syn::Visibility`|The visibility of the passed-in field|
59//! |`ty`|`syn::Type`|The type of the passed-in field|
60//! |`attrs`|`Vec<syn::Attribute>` (or anything, using `#[darling(with = ...)]`)|The forwarded attributes from the passed in field. These are controlled using the `forward_attrs` attribute.|
61//!
62//! ### `FromTypeParam`
63//! |Field name|Type|Meaning|
64//! |---|---|---|
65//! |`ident`|`syn::Ident`|The identifier of the passed-in type param|
66//! |`bounds`|`Vec<syn::TypeParamBound>`|The bounds applied to the type param|
67//! |`default`|`Option<syn::Type>`|The default type of the parameter, if one exists|
68//! |`attrs`|`Vec<syn::Attribute>` (or anything, using `#[darling(with = ...)]`)|The forwarded attributes from the passed in type param. These are controlled using the `forward_attrs` attribute.|
69//!
70//! ### `FromVariant`
71//! |Field name|Type|Meaning|
72//! |---|---|---|
73//! |`ident`|`syn::Ident`|The identifier of the passed-in variant|
74//! |`discriminant`|`Option<syn::Expr>`|For a variant such as `Example = 2`, the `2`|
75//! |`fields`|`darling::ast::Fields<T> where T: FromField`|The fields associated with the variant|
76//! |`attrs`|`Vec<syn::Attribute>` (or anything, using `#[darling(with = ...)]`)|The forwarded attributes from the passed in variant. These are controlled using the `forward_attrs` attribute.|
77#![warn(rust_2018_idioms)]
78
79#[allow(unused_imports)]
80#[macro_use]
81extern crate darling_macro;
82
83#[doc(hidden)]
84pub use darling_macro::*;
85
86#[doc(inline)]
87pub use darling_core::{
88 FromAttributes, FromDeriveInput, FromField, FromGenericParam, FromGenerics, FromMeta,
89 FromTypeParam, FromVariant,
90};
91
92#[doc(inline)]
93pub use darling_core::{Error, Result};
94
95#[doc(inline)]
96pub use darling_core::{ast, error, usage, util};
97
98// XXX exported so that `ExtractAttribute::extractor` can convert a path into tokens.
99// This is likely to change in the future, so only generated code should depend on this export.
100#[doc(hidden)]
101pub use darling_core::ToTokens;
102
103/// Core/std trait re-exports. This should help produce generated code which doesn't
104/// depend on `std` unnecessarily, and avoids problems caused by aliasing `std` or any
105/// of the referenced types.
106#[doc(hidden)]
107pub mod export {
108 pub use core::convert::{identity, From, Into};
109 pub use core::default::Default;
110 pub use core::iter::IntoIterator;
111 pub use core::option::Option::{self, None, Some};
112 pub use core::result::Result::{self, Err, Ok};
113 pub use darling_core::syn;
114 pub use std::string::ToString;
115 pub use std::vec::Vec;
116
117 pub use crate::ast::NestedMeta;
118}
119
120#[macro_use]
121mod macros_public;