DeepMerge
A flexible deep merge library for Rust with policy-driven merging and derive macros featuring typed attributes.
Features
- Policy-driven merging: Configure how different types should be merged
- Derive macro support: Automatically implement
DeepMergefor your structs - Typed attributes: Use identifiers instead of string literals for better compile-time checking
- Flexible attribute syntax: Mix string literals, identifiers, and path expressions
- Precedence rules: Field-level > struct-level > caller-provided policies
- Multiple merge strategies: Append, prepend, union, concatenation, replacement, and more
- No-std compatible: Works without the standard library (with
alloc)
Quick Start
Add this to your Cargo.toml:
[]
= { = "0.1", = ["derive"] }
Usage Examples
Basic Usage with Typed Attributes
use *;
// Use clean identifier syntax instead of string literals
let mut config = Config ;
let update = Config ;
config.merge_with_policy;
// Results:
// title: "My App v2" (concatenated)
// tags: ["web", "api"] (appended)
// enabled: true (true wins)
// version: "1.0" (kept original due to field-level override)
Supported Attribute Syntax
use *;
Available Merge Policies
String Policies: concat, keep, replace
Sequence Policies: append, prepend, union, extend, intersect
Boolean Policies: true_wins, false_wins, replace, keep
Number Policies: sum, max, min, replace, keep
Map Policies: overlay, union, left, right
Option Policies: take, preserve, or_left
Policy Precedence
Policies are applied in this order of precedence:
- Field-level attributes (highest priority)
- Struct-level attributes
- Caller-provided policies (lowest priority)
Advanced Features
Future Path Expression Support
The infrastructure is ready for advanced path expressions:
// Coming soon:
// #[merge(policy(string = StringMerge::ConcatWithSep(", ")))]
Condition Policies with Key Extractors
// Coming soon:
// #[merge(policy(condition = changed, changed_by = "key_extractor_fn"))]
Feature Flags
derive(default): Enable the derive macrostd(default): Enable standard library supportalloc(default): Enable allocation supportindexmap: Add support forIndexMaptypes
License
Licensed under either of:
- Apache License, Version 2.0
- MIT License
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.