#macro-derive #patch #struct #overlay

macro struct-patch-derive

A library that helps you implement partial updates for your structs

15 unstable releases (3 breaking)

0.4.1 Oct 2, 2023
0.3.2 Aug 8, 2023
0.3.1 Jul 24, 2023
0.2.3 Mar 4, 2023

#29 in #overlay

Download history 275/week @ 2023-11-20 802/week @ 2023-11-27 1055/week @ 2023-12-04 1789/week @ 2023-12-11 1709/week @ 2023-12-18 1252/week @ 2023-12-25 1196/week @ 2024-01-01 2406/week @ 2024-01-08 1791/week @ 2024-01-15 1752/week @ 2024-01-22 1151/week @ 2024-01-29 1343/week @ 2024-02-05 1901/week @ 2024-02-12 2498/week @ 2024-02-19 1558/week @ 2024-02-26 1377/week @ 2024-03-04

7,525 downloads per month
Used in 2 crates (via struct-patch)

MIT license

13KB
252 lines

Struct Patch

Crates.io MIT licensed Docs

A lib help you patch Rust instance, and easy to partial update configures.

Introduction

This crate provides the Patch trait and an accompanying derive macro.

Deriving Patch on a struct will generate 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.

Quick Example

use struct_patch::Patch;
use serde::{Deserialize, Serialize};

#[derive(Default, Debug, PartialEq, Patch)]
#[patch_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);

    assert_eq!(
        item,
        Item {
            field_bool: true,
            field_int: 7,
            field_string: String::from("hello")
        }
    );
}

Dependencies

~320–770KB
~18K SLoC