#merge #serde #magic #sugar

serde_merge

Simple utility to merge some serializable data types

4 releases

0.1.3 Dec 30, 2021
0.1.2 Jul 4, 2021
0.1.1 Jul 4, 2021
0.1.0 Jul 3, 2021

#720 in Encoding

Download history 4366/week @ 2024-07-20 4706/week @ 2024-07-27 3629/week @ 2024-08-03 4877/week @ 2024-08-10 6116/week @ 2024-08-17 5403/week @ 2024-08-24 4895/week @ 2024-08-31 5466/week @ 2024-09-07 5314/week @ 2024-09-14 5353/week @ 2024-09-21 6830/week @ 2024-09-28 7152/week @ 2024-10-05 5896/week @ 2024-10-12 6919/week @ 2024-10-19 6297/week @ 2024-10-26 5998/week @ 2024-11-02

26,264 downloads per month
Used in 4 crates (3 directly)

Apache-2.0

8KB
60 lines

SERDE MERGE

Simple utility to merge some serializable data types based on serde.

Quick Start

HashMap with HashMap

use serde_merge::mmerge;
use std::collections::HashMap;

fn main() {
    let mut map1: HashMap<&str, &str> = HashMap::new();
    map1.insert("key1", "value1");
    map1.insert("key2", "value2");
    map1.insert("key3", "value3");
    let mut map2: HashMap<&str, &str> = HashMap::new();
    map2.insert("key4", "value4");
    map2.insert("key5", "value5");
    map2.insert("key6", "value6");
    let result = mmerge(map1, map2).unwrap();
    println!("{:#?}", result);
    // --Output--
    // {
    //     "key1": String(
    //         "value1",
    //     ),
    //     "key2": String(
    //         "value2",
    //     ),
    //     "key3": String(
    //         "value3",
    //     ),
    //     "key4": String(
    //         "value4",
    //     ),
    //     "key5": String(
    //         "value5",
    //     ),
    //     "key6": String(
    //         "value6",
    //     ),
    // }
}

Struct with struct

use serde::{Deserialize, Serialize};
use serde_merge::tmerge;

#[derive(Serialize, Deserialize)]
struct Foo {
    pub field1: String,
    pub field2: i32,
}

#[derive(Serialize, Deserialize)]
struct Bar {
    pub field3: Vec<String>,
    pub field4: u32,
}

#[derive(Debug, Serialize, Deserialize)]
struct Result {
    pub field1: String,
    pub field2: i32,
    pub field3: Vec<String>,
    pub field4: u32,
}

fn main() {
    let foo = Foo {
        field1: "field1".to_string(),
        field2: 15,
    };
    let bar = Bar {
        field3: Vec::from([
            "elem1".to_string(),
            "elem2".to_string(),
            "elem3".to_string(),
        ]),
        field4: 5,
    };
    // unwrap is not recommended, handle Result in your app
    let result: Result = tmerge(foo, bar).unwrap();
    println!("{:#?}", result);
    // --Output--
    // Result {
    //     field1: "field1",
    //     field2: 15,
    //     field3: [
    //         "elem1",
    //         "elem2",
    //         "elem3",
    //     ],
    //     field4: 5,
    // }
}

Note

Merging structs require derive feature for serde.

Struct with optional fields

use serde::{Deserialize, Serialize};
use serde_merge::omerge;

#[derive(Serialize, Deserialize, PartialEq)]
struct HasOptional {
    field1: i32,
    field2: Option<bool>,
    field3: Option<String>,
}

pub fn main() {
    let s1 = HasOptional {
        field1: 1,
        field2: None,
        field3: Some("3".to_string()),
    };
    let s2 = HasOptional {
        field1: 2,
        field2: Some(true),
        field3: None,
    };
    let result: HasOptional = omerge(s1, s2).unwrap();
    let target = HasOptional {
        field1: 2,
        field2: Some(true),
        field3: Some("3".to_string()),
    };
    assert_eq!(result, target); // true
}

Dependencies

~0.7–1.7MB
~36K SLoC