Skip to content

Commit

Permalink
Deserialize maybe-empty strings as Option<String>
Browse files Browse the repository at this point in the history
  • Loading branch information
d-k-bo committed Oct 8, 2023
1 parent 5258e7a commit 5641b8e
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,20 @@ pub struct Item {
pub channel: String,
pub topic: String,
pub title: String,
pub description: String,
#[serde(deserialize_with = "empty_string_as_none")]
pub description: Option<String>,
pub timestamp: i64,
#[serde(with = "duration_secs")]
pub duration: Duration,
pub size: Option<usize>,
pub url_website: String,
pub url_subtitle: String,
#[serde(deserialize_with = "empty_string_as_none")]
pub url_subtitle: Option<String>,
pub url_video: String,
pub url_video_low: String,
pub url_video_hd: String,
#[serde(deserialize_with = "empty_string_as_none")]
pub url_video_low: Option<String>,
#[serde(deserialize_with = "empty_string_as_none")]
pub url_video_hd: Option<String>,
#[serde(with = "timestamp", rename = "filmlisteTimestamp")]
pub filmliste_timestamp: i64,
pub id: String,
Expand Down Expand Up @@ -203,3 +207,15 @@ mod timestamp {
deserializer.deserialize_any(TimestampVisitor)
}
}

pub fn empty_string_as_none<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
if s.is_empty() {
Ok(None)
} else {
Ok(Some(s))
}
}

0 comments on commit 5641b8e

Please sign in to comment.