Skip to content

Instantly share code, notes, and snippets.

@MaikKlein
Last active March 26, 2018 19:39
Show Gist options
  • Save MaikKlein/08d9359ac81ebbd04e09dac999ab1e05 to your computer and use it in GitHub Desktop.
Save MaikKlein/08d9359ac81ebbd04e09dac999ab1e05 to your computer and use it in GitHub Desktop.
pub fn serde_from_str_opt<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error>
where
D: Deserializer<'de>,
T: FromStr,
T::Err: Display,
{
String::deserialize(deserializer)
.map(|s| T::from_str(&s).ok())
}
#[serde(deserialize_with = "serde_from_str_opt")]
pub average_price: Option<f32>,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment