serde_apply

Crates.ioserde_apply
lib.rsserde_apply
version0.1.0
sourcesrc
created_at2022-05-10 21:32:50.490344
updated_at2022-05-10 21:32:50.490344
descriptionPartially deserialize and update your struct
homepagehttps://github.com/Zagitta/serde_apply
repositoryhttps://github.com/Zagitta/serde_apply
max_upload_size
id584201
size6,898
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