Closed mike-lloyd03 closed 6 months ago
@mike-lloyd03 you need add default keyword in serde marco.
See example: https://github.com/baoyachi/duration-str/blob/3c8d5eed4647ad991f847f5d1b4a597ddba346f6/src/lib.rs#L975-L1000.
#[derive(Default, Deserialize)]
pub struct Config {
pub default_shell: Option<ShellType>,
+ #[serde(default,deserialize_with = "deserialize_option_duration_time")]
pub session_lifetime: Option<time::Duration>,
}
Ahh that'll do it. Thanks for the quick response.
When using
deserialize_option_duration_time
and deserializing a yaml file, if the field is missing from the yaml file,serde_yaml::from_reader
will raise an error that the field is missing. This should not happen for fields of typeOption<T>
.For these two tests, the first one passes and the second one fails:
Furthermore, if I remove
deserialize_option_duration_time
and changesession_lifetime
toOption<String>
, the test does not raise an error for a missing field.