Skip to content

Support from_expr when deriving FromMeta #369

@TedDriggs

Description

@TedDriggs

A common pattern (seen in #367, among other places) is to want to have a field that supports multiple formats: The simple word form, the key-value form, or a sub-object "list" form.

The from_word override allows someone to derive FromMeta while specifying how that format should work, but there is no clean override for the key-value form.

The example in #367 would become:

#[derive(Default, FromMeta)]
#[darling(
    from_word = Default::default,
    from_expr = |expr| Ok(Self { value: Some(expr.clone()), ..Default::default() })
)]
struct ErrorPolicy {
    warn: Flag,
    value: Option<syn::Expr>
}

That is more concise, as it avoids duplication of the struct declaration to get a derived impl.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions