pub struct Flag(_);
Expand description

A meta-item that can be present as a word - with no value - or absent.

Defaulting

Like Option, Flag does not require #[darling(default)] to be optional. If the caller does not include the property, then an absent Flag will be included in the receiver struct.

Spans

Flag keeps the span where its word was seen. This enables attaching custom error messages to the word, such as in the case of two conflicting flags being present.

Example

#[derive(FromMeta)]
#[darling(and_then = "Self::not_both")]
struct Demo {
    flag_a: Flag,
    flag_b: Flag,
}

impl Demo {
    fn not_both(self) -> Result<Self> {
        if self.flag_a.is_present() && self.flag_b.is_present() {
            Err(Error::custom("Cannot set flag_a and flag_b").with_span(self.flag_b))
        } else {
            Ok(self)
        }
    }
}

The above struct would then produce the following error.

#[example(flag_a, flag_b)]
//                ^^^^^^ Cannot set flag_a and flag_b

Implementations

Creates a new Flag which corresponds to the presence of a value.

Check if the flag is present.

👎 Deprecated since 0.14.0:

Use Flag::is_present

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Converts to this type from the input type.

Converts to this type from the input type.

When a field is omitted from a parent meta-item, from_none is used to attempt recovery before a missing field error is generated. Read more

Create an instance from a syn::Meta by dispatching to the format-appropriate trait function. This generally should not be overridden by implementers. Read more

Create an instance from the presence of the word in the attribute with no additional options specified. Read more

Create an instance from a list of nested meta items.

Create an instance from a literal value of either foo = "bar" or foo("bar"). This dispatches to the appropriate method based on the type of literal encountered, and generally should not be overridden by implementers. Read more

Create an instance from a char literal in a value position.

Create an instance from a string literal in a value position.

Create an instance from a bool literal in a value position.

Returns a Span covering the complete contents of this syntax tree node, or Span::call_site() if this node is empty. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.