Crates.io | serde_piecewise_default |
lib.rs | serde_piecewise_default |
version | 0.2.0 |
source | src |
created_at | 2019-06-12 05:05:22.144503 |
updated_at | 2019-06-12 05:24:29.166037 |
description | Uses serde's Option |
homepage | |
repository | https://github.com/boringcactus/serde-piecewise-default |
max_upload_size | |
id | 140534 |
size | 10,317 |
Uses serde's Option<T>
handling to let you easily specify defaults for all fields at once
by implementing Default
.
use serde::Deserialize;
use serde_piecewise_default::DeserializePiecewiseDefault;
#[derive(DeserializePiecewiseDefault, PartialEq, Eq, Debug)]
struct Example {
value: u8,
enabled: bool,
}
impl Default for Example {
fn default() -> Self {
Example {
value: 20,
enabled: true,
}
}
}
let data: Example = serde_json::from_str(r#"{"value": 8}"#).unwrap();
assert_eq!(data, Example { value: 8, enabled: true });
let data: Example = serde_json::from_str(r#"{"enabled": false}"#).unwrap();
assert_eq!(data, Example { value: 20, enabled: false });
let data: Example = serde_json::from_str("{}").unwrap();
assert_eq!(data, Example { value: 20, enabled: true });
#[derive(DeserializePiecewiseDefault)]
struct Example {
item1: i8,
item2: String,
}
will expand to
struct Example {
item1: i8,
item2: String,
}
#[derive(Deserialize)]
struct OptionExample {
item1: Option<i8>,
item2: Option<String>,
}
impl<'de> Deserialize<'de> for Example {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
<OptionExample as Deserialize>::deserialize(deserializer)
.map(|raw_result| {
let OptionExample { item1, item2 } = raw_result;
let default = <Example as Default>::default();
let item1 = item1.unwrap_or(default.item1);
let item2 = item2.unwrap_or(default.item2);
Example { item1, item2 }
})
}
}