Crate optional_struct

source ·
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§

  • 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§

  • The core of this crate. Call this proc macro on your structures to generate another structure containing Optional 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 an Option, unless the field already is an Option. 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 stays T. This is enabled by default if T is a Optiona<U>. optional_wrap => this forces wrapping a value, e.g. U becomes Option<U>. Enabling this allows nested Option, e.g. Option<V> can become Option<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.