#struct #patch #macro #derive #overlay

macro struct-patch-derive

A library that helps you implement partial updates for your structs

10 releases

0.2.3 Mar 4, 2023
0.2.2 Mar 2, 2023
0.2.1 Feb 23, 2023
0.2.0 Jan 21, 2023
0.1.5 Jan 21, 2023

#10 in #overlay

Download history 155/week @ 2023-01-13 177/week @ 2023-01-20 34/week @ 2023-01-27 57/week @ 2023-02-03 243/week @ 2023-02-10 402/week @ 2023-02-17 789/week @ 2023-02-24 622/week @ 2023-03-03 504/week @ 2023-03-10 465/week @ 2023-03-17 351/week @ 2023-03-24

2,177 downloads per month
Used in struct-patch

MIT license

9KB
164 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

~0.8–1.2MB
~28K SLoC