Trait darling::FromMeta

source ·
pub trait FromMeta: Sized {
    // Provided methods
    fn from_nested_meta(item: &NestedMeta) -> Result<Self, Error> { ... }
    fn from_meta(item: &Meta) -> Result<Self, Error> { ... }
    fn from_none() -> Option<Self> { ... }
    fn from_word() -> Result<Self, Error> { ... }
    fn from_list(items: &[NestedMeta]) -> Result<Self, Error> { ... }
    fn from_value(value: &Lit) -> Result<Self, Error> { ... }
    fn from_char(value: char) -> Result<Self, Error> { ... }
    fn from_string(value: &str) -> Result<Self, Error> { ... }
    fn from_bool(value: bool) -> Result<Self, Error> { ... }
}
Expand description

Create an instance from an item in an attribute declaration.

Implementing FromMeta

  • Do not take a dependency on the ident of the passed-in meta item. The ident will be set by the field name of the containing struct.
  • Implement only the from_* methods that you intend to support. The default implementations will return useful errors.

Provided Implementations

bool

  • Word with no value specified - becomes true.
  • As a boolean literal, e.g. foo = true.
  • As a string literal, e.g. foo = "true".

char

  • As a char literal, e.g. foo = '#'.
  • As a string literal consisting of a single character, e.g. foo = "#".

String

  • As a string literal, e.g. foo = "hello".
  • As a raw string literal, e.g. foo = r#"hello "world""#.

Number

  • As a string literal, e.g. foo = "-25".
  • As an unquoted positive value, e.g. foo = 404. Negative numbers must be in quotation marks.

()

  • Word with no value specified, e.g. foo. This is best used with Option. See darling::util::Flag for a more strongly-typed alternative.

Option

  • Any format produces Some.

Result<T, darling::Error>

  • Allows for fallible parsing; will populate the target field with the result of the parse attempt.

Provided Methods§

source

fn from_nested_meta(item: &NestedMeta) -> Result<Self, Error>

source

fn from_meta(item: &Meta) -> Result<Self, Error>

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

Error Spans

If this method is overridden and can introduce errors that weren’t passed up from other from_meta calls, the override must call with_span on the error using the item to make sure that the emitted diagnostic points to the correct location in source code.

source

fn from_none() -> Option<Self>

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

Most types should not override this method. darling already allows field-level missing-field recovery using #[darling(default)] and #[darling(default = "...")], and users who add a String field to their FromMeta-deriving struct would be surprised if they get back "" instead of a missing field error when that field is omitted.

The primary use-case for this is Option<T> fields gracefully handlling absence without needing #[darling(default)].

source

fn from_word() -> Result<Self, Error>

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

source

fn from_list(items: &[NestedMeta]) -> Result<Self, Error>

Create an instance from a list of nested meta items.

source

fn from_value(value: &Lit) -> Result<Self, Error>

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.

Error Spans

If this method is overridden, the override must make sure to add value’s span information to the returned error by calling with_span(value) on the Error instance.

source

fn from_char(value: char) -> Result<Self, Error>

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

source

fn from_string(value: &str) -> Result<Self, Error>

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

source

fn from_bool(value: bool) -> Result<Self, Error>

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

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl FromMeta for RenameRule

source§

impl FromMeta for Meta

source§

impl FromMeta for Visibility

source§

impl FromMeta for Expr

source§

impl FromMeta for Lit

source§

impl FromMeta for Type

source§

impl FromMeta for bool

source§

impl FromMeta for char

source§

impl FromMeta for f32

source§

impl FromMeta for f64

source§

impl FromMeta for i8

source§

impl FromMeta for i16

source§

impl FromMeta for i32

source§

impl FromMeta for i64

source§

impl FromMeta for isize

source§

impl FromMeta for u8

source§

impl FromMeta for u16

source§

impl FromMeta for u32

source§

impl FromMeta for u64

source§

impl FromMeta for ()

source§

impl FromMeta for usize

source§

impl FromMeta for Ident

source§

impl FromMeta for Literal

source§

impl FromMeta for String

source§

impl FromMeta for Vec<WherePredicate>

source§

impl FromMeta for Vec<u8>

Parsing an unsigned integer array, i.e. example = "[1, 2, 3, 4]".

source§

impl FromMeta for Vec<u16>

Parsing an unsigned integer array, i.e. example = "[1, 2, 3, 4]".

source§

impl FromMeta for Vec<u32>

Parsing an unsigned integer array, i.e. example = "[1, 2, 3, 4]".

source§

impl FromMeta for Vec<u64>

Parsing an unsigned integer array, i.e. example = "[1, 2, 3, 4]".

source§

impl FromMeta for Vec<usize>

Parsing an unsigned integer array, i.e. example = "[1, 2, 3, 4]".

source§

impl FromMeta for AtomicBool

source§

impl FromMeta for ExprArray

source§

impl FromMeta for ExprPath

source§

impl FromMeta for TypeParam

source§

impl FromMeta for WhereClause

source§

impl FromMeta for LitBool

source§

impl FromMeta for LitByte

source§

impl FromMeta for LitByteStr

source§

impl FromMeta for LitChar

source§

impl FromMeta for LitFloat

source§

impl FromMeta for LitInt

source§

impl FromMeta for LitStr

source§

impl FromMeta for Path

source§

impl FromMeta for TypeArray

source§

impl FromMeta for TypeBareFn

source§

impl FromMeta for TypeGroup

source§

impl FromMeta for TypeImplTrait

source§

impl FromMeta for TypeInfer

source§

impl FromMeta for TypeMacro

source§

impl FromMeta for TypeNever

source§

impl FromMeta for TypeParen

source§

impl FromMeta for TypePath

source§

impl FromMeta for TypePtr

source§

impl FromMeta for TypeReference

source§

impl FromMeta for TypeSlice

source§

impl FromMeta for TypeTraitObject

source§

impl FromMeta for TypeTuple

source§

impl<T> FromMeta for Option<T>where T: FromMeta,

source§

impl<T> FromMeta for Result<T, Meta>where T: FromMeta,

Parses the meta-item, and in case of error preserves a copy of the input for later analysis.

source§

impl<T> FromMeta for Result<T, Error>where T: FromMeta,

source§

impl<T> FromMeta for Box<T>where T: FromMeta,

source§

impl<T> FromMeta for Rc<T>where T: FromMeta,

source§

impl<T> FromMeta for Arc<T>where T: FromMeta,

source§

impl<T> FromMeta for RefCell<T>where T: FromMeta,

source§

impl<T, P> FromMeta for Punctuated<T, P>where T: Parse, P: Parse,

Parsing support for punctuated. This attempts to preserve span information when available, but also supports parsing strings with the call site as the emitted span.

source§

impl<V, S> FromMeta for HashMap<Ident, V, S>where V: FromMeta, S: BuildHasher + Default,

source§

impl<V, S> FromMeta for HashMap<String, V, S>where V: FromMeta, S: BuildHasher + Default,

source§

impl<V, S> FromMeta for HashMap<Path, V, S>where V: FromMeta, S: BuildHasher + Default,

Implementors§

source§

impl FromMeta for Flag

source§

impl FromMeta for IdentString

source§

impl FromMeta for Ignored

source§

impl FromMeta for PathList

source§

impl<T> FromMeta for Override<T>where T: FromMeta,

Parses a Meta. A bare word will produce Override::Inherit, while any value will be forwarded to T::from_meta.

source§

impl<T> FromMeta for SpannedValue<T>where T: FromMeta,

source§

impl<T> FromMeta for WithOriginal<T, Meta>where T: FromMeta,