Deluxe

A Rust procedural macro attribute parser.
Abstract
This crate offers attribute parsing closer to the design of attributes in C#. It has an interface similar to serde. Attributes are written as plain Rust structs or enums, and then parsers for them are generated automatically. They can contain arbitrary expressions and can inherit from other attributes using a flattening mechanism.
The parsers in this crate directly parse token streams using
syn
. As a result, most built-in Rust types and syn
types can be used directly as fields.
Details
Functionality in this crate is centered around three traits, and their respective derive macros:
-
Extracts attributes from an object containing a list of
syn::Attribute
, and parses them into a Rust type. Should be implemented for top-level structures that will be parsed directly out of a set of matching attributes. -
Parses a Rust type from any object containing a list of
syn::Attribute
. Should be used if the set of matching attributes can potentially be shared between this type and other types. -
Parses a Rust type from a
syn::parse::ParseStream
. Should be implemented for any types that can be nested inside an attribute.
Basic usage of this crate requires simply deriving one (or a few) of these traits, and then
calling extract_attributes
or parse_attributes
. For more advanced functionality,
several #[deluxe(...)]
attributes are supported on structs, enums, variants
and fields. See the examples below, and the documentation for each derive macro
for a complete description of the supported attributes.
A list of field types supported by default can be seen in the list of provided
ParseMetaItem
implementations.
For more complex usage, manual implementations of these traits can be provided.
See the documentation on the individual traits for more details on how to
manually implement your own parsers.
Related Crates
Deluxe takes inspiration from the darling crate, but
offers a few enhancements over it. Darling is built around pre-parsed
syn::Meta
objects, and therefore is restricted to the meta
syntax.
Deluxe parses its types directly from TokenStream
objects in the attributes
and so is able to use any syntax that parses as a valid token tree. Deluxe also
does not provide extra traits for parsing special syn
objects like
DeriveInput
and Field
. Instead, Deluxe uses a generic trait to parse from
any type containing a Vec<syn::Attribute>
.
Examples
Basic
Inside your procedural macro, the ExtractAttributes
trait can be derived to
extract a struct from a named attribute:
// match only `my_desc` attributes
Then, try adding the attribute in some code that uses your macro:
;
let hello = Hello;
assert_eq!;
Field Attributes
The attributes alias
, default
, rename
, and skip
are supported, and
behave the same as in Serde. The append
attribute can be used
on Vec
fields to aggregate all duplicates of a key. The rest
attribute can
be used to do custom processing on any unknown keys.
// omitted fields will be set to defaults
;
// `expr` can be specified multiple times because of the `append` attribute
;
// `unknown` and `extra` will be stored in the `rest` hashmap
;
Inheritance
The flatten
attribute can be used to parse keys from one structure inside another:
Then, fields from both A
and B
can be used when deriving B
:
;
Tuple Structs, Tuples and Vecs
Deluxe also supports parsing into data structures with unnamed fields.
;
The standard attribute syntax with parenthesis can be used when specifying a
Vec
type. The alternative syntax key = [...]
can also be used to have an
appearance similar to an array literal.
;
;
// `idents` contains same values as above
;
C#-styled Attributes
Attributes in C# can support positional arguments first with the named
arguments afterwards. This style can be emulated by using a tuple struct with a
normal struct flattened at the end. Placing #[deluxe(default)]
on the struct
behaves the same as Serde, by filling in all fields with values from Default
,
allowing every named argument to be optional.
;
] Flags)
;
;
Enums
Enums are supported by using the variant name as a single key, in snake-case. Variants can be renamed, aliased and skipped in the same way as fields.
;
;
Complex Enums
Enums with struct and tuple variants are also supported. The data inside is used as arguments to the attribute. All field attributes from structs are also supported inside variants.
Additionally, enum variants with named fields can be flattened. The behavior of
a flattened variant is similar to Serde's untagged
mode. In a flattened
variant, the name of the variant will be ignored. Instead, Deluxe will attempt
to use the unique keys in each variant to determine if that variant was
specified. A compile error will be thrown if it is not possible to determine a
unique, unambiguous key between two variants.
;
;
;
// no inner parenthesis needed here due to flattening
;
Storing Containers
During parsing, Deluxe can store references to the container type holding the attributes for easier access. Container fields are skipped during attribute parsing.
To support both extracting and parsing, a container field can also be a value type. In that case, the container will be cloned into the structure.