Expand description
This crate provides the user with a macro, optional_struct
, which allows the user to generate
structures with optional fields, as well as functions for “fusing” such structures to form a
concrete instance of the original, user-defined structure. This was developed with the goal of
simplifying aggregating configurations coming from different sources, such as e.g. file, env,
CLI, etc.
Traits§
- Applicable
- The trait is implemented for every generated structure. Thanks to this, you can use optional_struct in generic contexts. You should never have to implement this manually.
Attribute Macros§
- optional_
struct - The core of this crate. Call this proc macro on your structures to generate another structure
containing
Option
al fields, as well as helpers functions to convert those optional_struct to their base, or even update only fields that have been set. This makes aggregating structures from different sources (e.g. configuration from file/env/CLI) simple. The generated struct by default will wrap all fields in anOption
, unless the field already is anOption
. There are however other attributes that one can use to enforce a different behaviour: optional_rename => rename the type in the generated structure. Useful when the nested structure itself has an optional_struct. This enables arbitrary nesting of optional_struct (see tests for examples). optional_skip_wrap => this force not wrapping a value, e.g.T
staysT
. This is enabled by default ifT
is aOptiona<U>
. optional_wrap => this forces wrapping a value, e.g.U
becomesOption<U>
. Enabling this allows nestedOption
, e.g.Option<V>
can becomeOption<Option<V>>
optional_serde_skip_none => This generate an extra#[serde(skip_serializing_if = ... )]
to the generated structures. Useful if you want to (de)serialize those structures with serde.