partially
Provides a configurable derive macro that generates another struct with the same fields, but wrapped in
Option<T>
, and implementsPartial
to allow conditionally applying the generated struct fields to the base struct fields.
partially
provides the Partial
trait, which allows applying another structs values to self
, with the intent that the other struct mirrors the fields of self
, but wrapped in Option<T>
.
Further, partially_derive
(or partially
with the derive
feature enabled) supports automatically generating a mirrored struct with each field wrapped in Option<T>
, and generates a Partial
implementation that allows applying the Some
fields of the mirrored struct to the base struct. I expect most folks will be most interested in using the derive macro.
Usage
With derive:
// `partially` installed with feature `derive`
use Partial;
// define a base structure, with the `Partial` derive macro
// further, instruct the macro to derive `Default` on the generated structure
// example usage
Without derive:
use Partial;
Struct Options
derive
Usage example:
#[partially(derive(Debug, Default))]
.
Instructs the macro to generate a #[derive(...)]
attribute on the generated struct.
Note: When using this option with the skip_attributes
option, the derive attribute will still be added to the generated struct.
rename
Usage example:
#[partially(rename = "MyGeneratedStruct")]
.
Instructs the macro to use a given identifier for the generated struct. By default, PartialBaseStructName
is used, where BaseStructName
is the name of the original struct.
attribute
Usage example:
#[partially(attribute(serde(rename_all = "PascalCase")))]
Instructs the macro to add an additional attribute to the generated struct. By default, the attributes defined on the base struct are forwarded to the generated struct, unless the skip_attributes
option is present.
skip_attributes
Usage example:
#[partially(skip_attributes)]
.
Instructs the macro to skip forwarding attributes from the original struct to the generated struct. By default, all attributes that are present on the base struct are added to the generated struct.
Note: When using this option with the derive
option, the derive attribute will still be added to the generated struct.
Note: When using this option with the attribute
option, the specified attribute(s) will still be added to the generated struct.
crate
Usage example:
#[partially(crate = "my_partially_crate")]
.
Instructs the macro to use a different base path for the Partial
trait implementation. By default, partially
is used. This can be useful if you've forked the partially
crate.
Field Options
rename
Usage example:
#[partially(rename = "new_field_name")]
.
Instructs the macro to use a given identifier for the generated field. By default, the same name as the base struct is used.
omit
Usage example:
#[partially(omit)]
.
Instructs the macro to omit the field from the generated struct. By default, no fields are omitted.
transparent
Usage example:
#[partially(transparent)]
.
Instructs the macro to skip wrapping the generated field in Option<T>
, instead transparently mirroring the field type into the generated struct.
as_type
Usage example:
#[partially(as_type = "Option<f32>")]
.
Instructs the macro to use the provided type instead of Option<T>
when generating the field. Note that the provided type will be used verbatim, so if you expect an Option<T>
value, you'll need to manually specify that.
Note: When using as_type
, the given type must Into<BaseType>
where BaseType
is the original field type. This is required for Partial
trait implementation.