pub trait Attribute where
    Self: Sized
{ fn from_attributes(
        attrs: impl IntoIterator<Item = Attribute>
    ) -> Result<Self>; }
Expand description

The trait you actually derive on your attribute struct.

Basic gist is a struct like this:

#[derive(Attribute)]
#[attribute(ident = "collection")]
#[attribute(invalid_field = "Error when an unsupported value is set (e.g. meaning=42")]
struct CollectionAttribute {
    // Options are optional by default (will be set to None if not specified)
    authority: Option<String>,
    #[attribute(missing = "Error when the value is not set")]
    name: String,
    // Any type implementing default can be flagged as default
    // This will be set to Vec::default() when not specified
    #[attribute(default)]
    #[attribute(expected = "Error when an error occured while parsing")]
    views: Vec<Type>,
    // Booleans can be used without assiging a value. as a flag.
    // If omitted they are set to false
    some_flag: bool
}

Will be able to parse an attribute like this:

#[collection(authority="Some String", name = r#"Another string"#, views = [Option, ()], some_flag)]

Required methods

Parses an IntoIterator of syn::Attributes e.g. Vec<Attribute>.

It can therefore parse fields set over multiple attributes like:

#[collection(authority = "Authority", name = "Name")]
#[collection(views = [A, B])]

and also catch duplicate/conflicting settings over those.

Fails with a syn::Error so you can conveniently return that as a compiler error in a proc macro.

Implementors