Skip to content

Commit

Permalink
Clean up each using with
Browse files Browse the repository at this point in the history
  • Loading branch information
TedDriggs committed Mar 3, 2022
1 parent 1a61c39 commit 7ba027d
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions derive_builder_core/src/macro_options/darling_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::BuildMethod;
use darling::util::{Flag, PathList};
use darling::{self, FromMeta};
use proc_macro2::Span;
use syn::Meta;
use syn::{self, spanned::Spanned, Attribute, Generics, Ident, Path, Visibility};

use crate::{
Expand Down Expand Up @@ -120,38 +121,25 @@ impl StructLevelSetter {
}
}

/// Adapter that enables:
///
/// 1. Use of a derived `FromMeta` on `Each`,
/// 2. Support for `each = "..."` and `each(name = "...")`
/// 3. The rest of the builder crate to directly access fields on `Each`
struct EachLongOrShort(Each);

/// Create `Each` from an attribute's `Meta`.
///
/// Two formats are supported:
///
/// * `each = "..."`, which provides the name of the `each` setter and otherwise uses default values
/// * `each(name = "...")`, which allows setting additional options on the `each` setter
impl FromMeta for EachLongOrShort {
fn from_value(value: &syn::Lit) -> darling::Result<Self> {
if let syn::Lit::Str(v) = value {
fn parse_each(meta: &Meta) -> darling::Result<Option<Each>> {
if let Meta::NameValue(mnv) = meta {
if let syn::Lit::Str(v) = &mnv.lit {
v.parse::<Ident>()
.map(Each::from)
.map(Self)
.map_err(|_| darling::Error::unknown_value(&v.value()).with_span(value))
.map(Some)
.map_err(|_| darling::Error::unknown_value(&v.value()).with_span(v))
} else {
Err(darling::Error::unexpected_lit_type(value))
Err(darling::Error::unexpected_lit_type(&mnv.lit))
}
} else {
Each::from_meta(meta).map(Some)
}

fn from_list(items: &[syn::NestedMeta]) -> darling::Result<Self> {
Each::from_list(items).map(Self)
}
}

fn unpack_each_shorthand(input: Option<EachLongOrShort>) -> Option<Each> {
input.map(|v| v.0)
}

/// The `setter` meta item on fields in the input type.
Expand All @@ -166,7 +154,7 @@ pub struct FieldLevelSetter {
strip_option: Option<bool>,
skip: Option<bool>,
custom: Option<bool>,
#[darling(map = "unpack_each_shorthand")]
#[darling(with = "parse_each")]
each: Option<Each>,
}

Expand Down

0 comments on commit 7ba027d

Please sign in to comment.