#merge #serde #magic #sugar

serde_merge

一个简单的工具,用于合并一些可序列化的数据类型

4个版本

0.1.3 2021年12月30日
0.1.2 2021年7月4日
0.1.1 2021年7月4日
0.1.0 2021年7月3日

#608 in 编码

Download history 2161/week @ 2024-03-14 1876/week @ 2024-03-21 1989/week @ 2024-03-28 2132/week @ 2024-04-04 2006/week @ 2024-04-11 2275/week @ 2024-04-18 2741/week @ 2024-04-25 1942/week @ 2024-05-02 2550/week @ 2024-05-09 2716/week @ 2024-05-16 2605/week @ 2024-05-23 2627/week @ 2024-05-30 2931/week @ 2024-06-06 4605/week @ 2024-06-13 4607/week @ 2024-06-20 3303/week @ 2024-06-27

15,961 每月下载量
用于 3 个crate(其中2个直接使用)

Apache-2.0

8KB
60

SERDE MERGE

基于serde的简单工具,用于合并一些可序列化的数据类型。

快速入门

HashMap与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",
    //     ),
    // }
}

结构体与结构体

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,
    // }
}

注意

合并结构体需要为serde启用derive功能。

结构体与可选字段

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
}

依赖项

~0.7–1.4MB
~33K SLoC