| 1 |
//! Functions to derive `darling`'s traits from well-formed input, without directly depending |
| 2 |
//! on `proc_macro`. |
| 3 |
|
| 4 |
use proc_macro2::TokenStream; |
| 5 |
use quote::ToTokens; |
| 6 |
use syn::DeriveInput; |
| 7 |
|
| 8 |
use crate::options; |
| 9 |
|
| 10 |
/// Run an expression which returns a `darling::Result`, then either return the tokenized |
| 11 |
/// representation of the `Ok` value, or the tokens of the compiler errors in the `Err` case. |
| 12 |
macro_rules! emit_impl_or_error { |
| 13 |
($e:expr) => { |
| 14 |
match $e { |
| 15 |
Ok(val) => val.into_token_stream(), |
| 16 |
Err(err) => err.write_errors(), |
| 17 |
} |
| 18 |
}; |
| 19 |
} |
| 20 |
|
| 21 |
/// Create tokens for a `darling::FromMeta` impl from a `DeriveInput`. If |
| 22 |
/// the input cannot produce a valid impl, the returned tokens will contain |
| 23 |
/// compile errors instead. |
| 24 |
pub fn from_meta(input: &DeriveInput) -> TokenStream { |
| 25 |
emit_impl_or_error!(options::FromMetaOptions::new(input)) |
| 26 |
} |
| 27 |
|
| 28 |
/// Create tokens for a `darling::FromAttributes` impl from a `DeriveInput`. If |
| 29 |
/// the input cannot produce a valid impl, the returned tokens will contain |
| 30 |
/// compile errors instead. |
| 31 |
pub fn from_attributes(input: &DeriveInput) -> TokenStream { |
| 32 |
emit_impl_or_error!(options::FromAttributesOptions::new(input)) |
| 33 |
} |
| 34 |
|
| 35 |
/// Create tokens for a `darling::FromDeriveInput` impl from a `DeriveInput`. If |
| 36 |
/// the input cannot produce a valid impl, the returned tokens will contain |
| 37 |
/// compile errors instead. |
| 38 |
pub fn from_derive_input(input: &DeriveInput) -> TokenStream { |
| 39 |
emit_impl_or_error!(options::FdiOptions::new(input)) |
| 40 |
} |
| 41 |
|
| 42 |
/// Create tokens for a `darling::FromField` impl from a `DeriveInput`. If |
| 43 |
/// the input cannot produce a valid impl, the returned tokens will contain |
| 44 |
/// compile errors instead. |
| 45 |
pub fn from_field(input: &DeriveInput) -> TokenStream { |
| 46 |
emit_impl_or_error!(options::FromFieldOptions::new(input)) |
| 47 |
} |
| 48 |
|
| 49 |
/// Create tokens for a `darling::FromTypeParam` impl from a `DeriveInput`. If |
| 50 |
/// the input cannot produce a valid impl, the returned tokens will contain |
| 51 |
/// compile errors instead. |
| 52 |
pub fn from_type_param(input: &DeriveInput) -> TokenStream { |
| 53 |
emit_impl_or_error!(options::FromTypeParamOptions::new(input)) |
| 54 |
} |
| 55 |
|
| 56 |
/// Create tokens for a `darling::FromVariant` impl from a `DeriveInput`. If |
| 57 |
/// the input cannot produce a valid impl, the returned tokens will contain |
| 58 |
/// compile errors instead. |
| 59 |
pub fn from_variant(input: &DeriveInput) -> TokenStream { |
| 60 |
emit_impl_or_error!(options::FromVariantOptions::new(input)) |
| 61 |
} |