empty_string_as_none improved
use crate::types::resource::{MetaBehaviorHints, PosterShape};
use chrono::{DateTime, Utc};
-use serde::de::IntoDeserializer;
+use serde::de::{Deserializer, IntoDeserializer};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
@@ -58,15 +58,15 @@ impl LibItem {
}
}
-fn empty_string_as_none<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
+fn empty_string_as_none<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error>
where
- D: serde::Deserializer<'de>,
+ D: Deserializer<'de>,
T: serde::Deserialize<'de>,
{
- let opt = Option::<String>::deserialize(de)?;
- let opt = opt.as_deref();
- match opt {
- None | Some("") => Ok(None),
- Some(s) => T::deserialize(s.into_deserializer()).map(Some),
+ match Option::<String>::deserialize(deserializer) {
+ Ok(Some(value)) if value.is_empty() => Ok(None),
+ Ok(Some(value)) => T::deserialize(value.into_deserializer()).map(Some),
+ Ok(None) => Ok(None),
+ Err(error) => Err(error),
}
}
