serde_apply_macros

Crates.ioserde_apply_macros
lib.rsserde_apply_macros
version0.1.1
sourcesrc
created_at2022-05-10 21:24:52.251939
updated_at2022-05-10 21:32:23.171141
descriptionDerive macro for serde_apply
homepagehttps://github.com/Zagitta/serde_apply
repositoryhttps://github.com/Zagitta/serde_apply
max_upload_size
id584196
size3,842
Simon Rasmussen (Zagitta)

documentation

https://docs.rs/serde_apply

README

Deserialize partial structs and update existing state

#[derive(Default, Debug, Deserialize, serde_apply_macros::SerdeApply)]
struct Foobar {
    a: String,
    b: Option<String>,
}

#[derive(Default, Debug, Deserialize, serde_apply_macros::SerdeApply)]
struct Bar {
    foo: Foobar,
    baz: i32,
}

fn main() {
    let mut my_bar = Bar::default();
    println!("Before update: {:#?}", my_bar);
    serde_apply::apply(
        &mut my_bar,
        &mut serde_json::Deserializer::from_str(r#"{}"#),
    )
    .unwrap();
    println!("After first (noop) update: {:#?}", my_bar);
    serde_apply::apply(
        &mut my_bar,
        &mut serde_json::Deserializer::from_str(r#"{"foo": {"b": "Hello World!"}}"#),
    )
    .unwrap();
    println!("After second update: {:#?}", my_bar);
}
Before update: Bar {
    foo: Foobar {
        a: "",
        b: None,
    },
    baz: 0,
}
After first (noop) update: Bar {
    foo: Foobar {
        a: "",
        b: None,
    },
    baz: 0,
}
After second update: Bar {
    foo: Foobar {
        a: "",
        b: Some(
            "Hello World!",
        ),
    },
    baz: 0,
}
Commit count: 6

cargo fmt