A library to help you modify config structs. It provides:
Patchderive macro for partial updatesFillerderive macro to fill empty fieldsSubstrateandCatalystderive macros to extend a struct with extra fields
This crate provides the Patch, Filler, Substrate, Catalyst and Complex traits with accompanying derive macros in the following three use cases.
- If any field in a
PatchisSome, it overwrites the corresponding field when applied. - If any field in the instance is empty (
Noneor an empty collection),Fillerwill try to fill it. It supportsOption,Vec,VecDeque,LinkedList,HashMap,BTreeMap,HashSet,BTreeSet,BinaryHeapfields, as well as custom types via#[filler(extendable)]and any type via#[filler(empty_value = ...)]. - With the
catalystfeature,Substrate,CatalystandComplextraits with accompanying derive macros help you extend a struct with extra fields from another crate.
This crate supports no_std — check the no-std-examples.
The following are more specific scenarios to help you learn the details:
- A project with config from environments, files, and command line: you can use
Patchto keep your config organized. Please check this template. - A project extended from another project, where only some fields of the config differ. You can use the
catalystfeature to expose the base struct in a build script withSubstrate, then aCatalyststruct can bind to it and produce a complex struct. Check the complex-example and Quick Example: case 3.
Deriving Patch on a struct generates a struct similar to the original one, but with all fields wrapped in an Option.
An instance of such a patch struct can be applied onto the original struct, replacing values only if they are set to Some, leaving them unchanged otherwise.
use struct_patch::Patch;
use serde::{Deserialize, Serialize};
#[derive(Default, Debug, PartialEq, Patch)]
#[patch(attribute(derive(Debug, Default, Deserialize, Serialize)))]
struct Item {
field_bool: bool,
field_int: usize,
field_string: String,
}
fn patch_json() {
let mut item = Item {
field_bool: true,
field_int: 42,
field_string: String::from("hello"),
};
let data = r#"{
"field_int": 7
}"#;
let patch: ItemPatch = serde_json::from_str(data).unwrap();
item.apply(patch);
// You can do
// `let new_item = item << patch;`
// For multiple patches,
// you can do this
// `let new_item = item << patch_1 << patch_2;`
// or make an aggregated one, but please make sure the patch fields do not conflict, else it will panic
// ```
// let overall_patch = patch_1 + patch_2 + patch_3;
// let new_item = item << overall_patch;
// ```
assert_eq!(
item,
Item {
field_bool: true,
field_int: 7,
field_string: String::from("hello")
}
);
}Deriving Filler on a struct generates a struct similar to the original one, keeping only the fields that can be filled (Option, collections, extendable, or empty_value fields). Unlike Patch, the Filler only works on empty fields of the instance.
use struct_patch::Filler;
#[derive(Filler)]
struct Item {
field_int: usize,
maybe_field_int: Option<usize>,
list: Vec<usize>,
}
let mut item = Item {
field_int: 0,
maybe_field_int: None,
list: Vec::new(),
};
let filler_1 = ItemFiller { maybe_field_int: Some(7), list: Vec::new() };
item.apply(filler_1);
assert_eq!(item.maybe_field_int, Some(7));
let filler_2 = ItemFiller { maybe_field_int: Some(100), list: Vec::new() };
// The field is not empty, so the filler has no effect.
item.apply(filler_2);
assert_eq!(item.maybe_field_int, Some(7));
let filler_3 = ItemFiller { maybe_field_int: Some(100), list: vec![7] };
item.apply(filler_3);
assert_eq!(item.maybe_field_int, Some(7));
assert_eq!(item.list, vec![7]);Deriving Substrate on a struct exposes the field information so that other crates can access it in a build.rs.
Deriving Catalyst reads the field information of a Substrate and generates a new complex struct.
In the other words, the catalyst is a struct with extra fields that the developer writes down in the downstream crate. The complex is a generated struct that combines the substrate's fields with the catalyst's extra fields. The overall behavior is like chemical catalysts: a catalyst binds onto a substrate to form a complex struct, which has all fields from both.
A complex can also decouple without cloning, returning the original catalyst and substrate. Check the complex-example.
With the unsafe feature, bind and decouple use ManuallyDrop + ptr::read to avoid memory moves, and __substrate_new uses MaybeUninit + ptr::write while __substrate_unpack uses ManuallyDrop + ptr::read, such that the copy will be less.
In terms of crate dependencies, the crate using Substrate is upstream (a dependency), and the crate using Catalyst is downstream (it depends on the substrate crate). The downstream crate calls Substrate::expose() in its build.rs to read the field information at compile time, then uses #[catalyst(bind = ...)] to generate the complex struct.
/// In the substrate crate (src/lib.rs)
use struct_patch::Substrate;
#[derive(Substrate)]
pub struct Base {
pub field_bool: bool,
pub field_string: String,
}
/// In the catalyst crate (build.rs)
use struct_patch::Substrate;
fn main() {
substrate::Base::expose();
}
/// In the catalyst crate (src/lib.rs)
use struct_patch::Catalyst;
#[derive(Catalyst)]
#[catalyst(bind = Base)]
struct Amyloid {
pub extra_bool: bool,
pub extra_option: Option<usize>,
}
// Now AmyloidComplex is generated:
// struct AmyloidComplex {
// pub field_bool: bool,
// pub field_string: String,
// pub extra_bool: bool,
// pub extra_option: Option<usize>,
// }By default, deriving Patch wraps every field in an Option, so a field typed
Option<Vec<T>> becomes Option<Option<Vec<T>>> in the generated patch. When
this double wrapping is undesirable, annotate the field with
#[patch(skip_wrap)] to keep the original type in the patch. None in the
patch then means "no change" and Some(v) replaces the field — including
Some(vec![]) to explicitly clear the vector.
use struct_patch::Patch;
#[derive(Default, Patch)]
struct Item {
#[patch(skip_wrap)]
tags: Option<Vec<String>>,
}
// Generated struct
// struct ItemPatch {
// tags: Option<Vec<String>>,
// }
let mut item = Item { tags: Some(vec!["a".into()]) };
// `None` leaves the field unchanged.
item.apply(ItemPatch { tags: None });
assert_eq!(item.tags, Some(vec!["a".into()]));
// `Some(vec![])` still applies and clears the list.
item.apply(ItemPatch { tags: Some(vec![]) });
assert_eq!(item.tags, Some(vec![]));You can customize the generated structs by defining #[patch(...)], #[filler(...)], #[complex(...)] (catalyst feature), or #[catalyst(...)] (catalyst feature) attributes on the original struct or its fields.
Two attribute namespaces are provided for the catalyst feature because we need to handle the behaviors of two structs simultaneously — the catalyst itself and the product (complex). In general, the complex macro takes precedence over the catalyst macro when any conflict arises.
#[patch(name = "...")]: change the name of the generated patch struct.#[patch(attribute(...))]: add attributes to the generated patch struct.#[patch(attribute(derive(...)))]: add derives to the generated patch struct.#[filler(attribute(...))]: add attributes to the generated filler struct.#[catalyst(bind = "...")]: specify the base (substrate) structure. (catalyst feature)#[catalyst(keep_field_attribute)]: pass all field attributes from a substrate or catalyst through to the complex, unless an override is explicitly specified for that field. (catalyst feature)#[catalyst(exclude_field_attributes = ["..."])]: whenkeep_field_attributeis used, specifies attribute names to exclude from being passed through to the complex struct fields. For example,exclude_field_attributes = ["serde"]strips all#[serde(...)]field attributes from the substrate before they reach the complex. (catalyst feature)#[complex(override_field_attribute("$substrate_field_name", ...))]: override a complex field attribute, for exampleserde(default = "default_str"). (catalyst feature)#[complex(name = "...")]: change the name of the generated complex struct. (catalyst feature)#[complex(attribute(...))]: add attributes to the generated complex struct. (catalyst feature)
#[patch(skip)]: skip the field in the generated patch struct.#[patch(name = "...")]: change the type of the field in the generated patch struct.#[patch(attribute(...))]: add attributes to the field in the generated patch struct.#[patch(attribute(derive(...)))]: add derives to the field in the generated patch struct.#[patch(empty_value = ...)]: define a value as empty, so the corresponding field of the patch will not be wrapped byOption, and the patch is applied when the field differs from the empty value.#[patch(skip_wrap)]: keep the field type as-is in the patch struct (no extraOptionwrapping). Useful when the field is alreadyOption<...>(for exampleOption<Vec<_>>) and you do not want a double-Optionin the patch. Withskip_wrap,Nonein the patch means "no change" andSome(v)sets the field toSome(v)(includingSome(vec![])to clear the vector). Cannot be combined withempty_value.#[patch(nesting)]: treat the field as a nested patchable struct. The inner struct must also derivePatch. Requires thenestingfeature.#[patch(addable)]: allow conflicting patches to add their values together with the+operator instead of panicking. Requires theopfeature.#[patch(add = fn)]: likeaddable, but use the specified function to combine values. Requires theopfeature.#[filler(extendable)]: use the field as an extendable collection for the filler. The field type needs to implementDefault,Extend,IntoIterator, and have anis_emptymethod.#[filler(empty_value = ...)]: define a value as empty, so the corresponding field of the filler will be applied even when the field is notOptionorextendable.#[filler(addable)]: allow conflicting fillers to add/extend their values together with the+operator instead of panicking. Requires theopfeature.#[complex(attribute(...))]: add attributes to the field in the generated complex struct. (catalyst feature)
Please check the traits documentation to learn more.
The examples demonstrate the following scenarios:
- diff two instances for a patch (
diff.rs) - create a patch from a JSON string (
json.rs) - rename the patch structure (
rename-patch-struct.rs) - check whether a patch is empty (
status.rs) - add attributes to a patch struct (
patch-attr.rs) - show option field behavior (
option.rs) - show operators on patches (
op.rs) - show example with serde crates, e.g.
humantime_serdefor durations (time.rs) - show a patch nesting another patch (
nesting.rs) - show filler with all possible types (
filler.rs) - show operators on fillers (
filler-op.rs) - show
skip_wrapfield behavior (instance.rs) - use
Patchwithclapfor command-line config (clap.rs)
This crate includes the following optional features:
status(default): implements theStatustrait for the patch struct, which provides theis_emptymethod.op(default): provides the<<operator between an instance and a patch/filler, and the+operator for patches/fillers.- By default, when there is a field conflict between patches/fillers,
+will add them together if#[patch(addable)],#[patch(add = fn)], or#[filler(addable)]is provided; otherwise it will panic.
- By default, when there is a field conflict between patches/fillers,
merge(optional): implements theMergetrait for the patch struct, which provides themergemethod, and<<(ifopis enabled) between patches.alloc(optional): enablesallocsupport forno_std+ alloc environments.std(optional): enablesstd-dependent features (impliesboxandoption).box(optional): implements thePatch<Box<P>>trait forTwhereTimplementsPatch<P>. This lets you patch a boxed (or unboxed) struct with a boxed patch.option(optional): implements thePatch<Option<P>>trait forOption<T>whereTimplementsPatch<P>. Please take a look at the example to learn more.- default behavior:
Tneeds to implementFrom<P>. When patching onNone, it converts the patch intoTviaFrom<P>, letting you patch structs containing fields with optional values. none_as_default(optional):Tneeds to implementDefault. When patching onNone, it patches on a default instance. Mutually exclusive withkeep_none.keep_none(optional): when patching onNone, it staysNone. Mutually exclusive withnone_as_default.
- default behavior:
nesting(optional): allows a field to usePatchderive with the#[patch(nesting)]attribute.catalyst(optional): enables theSubstrate,Catalyst, andComplexderive macros for extending a struct with fields from another crate.unsafe(optional): usesManuallyDrop+ptr::read/MaybeUninit+ptr::writein the generatedbind,decouple,__substrate_new, and__substrate_unpackto avoid memory moves. Only meaningful with thecatalystfeature.