diff --git a/senders/terminal/src/models/v3.rs b/senders/terminal/src/models/v3.rs index c2432a6..74aca0e 100644 --- a/senders/terminal/src/models/v3.rs +++ b/senders/terminal/src/models/v3.rs @@ -15,7 +15,7 @@ pub enum MetadataObject { Generic { title: Option, thumbnail_url: Option, - custom: Value, + custom: Option, }, } @@ -46,7 +46,9 @@ impl Serialize for MetadataObject { None => Value::Null, }, ); - map.insert("custom".to_owned(), custom.clone()); + if let Some(custom) = custom { + map.insert("custom".to_owned(), custom.clone()); + } map.serialize(serializer) } } @@ -90,8 +92,7 @@ impl<'de> Deserialize<'de> for MetadataObject { thumbnail_url, custom: rest .get("custom") - .ok_or(de::Error::missing_field("custom"))? - .clone(), + .cloned(), }) } _ => Err(de::Error::custom(format!("Unknown metadata type {type_}"))), @@ -476,7 +477,7 @@ mod tests { &serde_json::to_string(&MetadataObject::Generic { title: Some(s!("abc")), thumbnail_url: Some(s!("def")), - custom: serde_json::Value::Null, + custom: Some(serde_json::Value::Null), }) .unwrap(), r#"{"custom":null,"thumbnailUrl":"def","title":"abc","type":0}"# @@ -485,11 +486,20 @@ mod tests { &serde_json::to_string(&MetadataObject::Generic { title: None, thumbnail_url: None, - custom: serde_json::Value::Null, + custom: Some(serde_json::Value::Null), }) .unwrap(), r#"{"custom":null,"thumbnailUrl":null,"title":null,"type":0}"# ); + assert_eq!( + &serde_json::to_string(&MetadataObject::Generic { + title: Some(s!("abc")), + thumbnail_url: Some(s!("def")), + custom: None, + }) + .unwrap(), + r#"{"thumbnailUrl":"def","title":"abc","type":0}"# + ); } #[test] @@ -502,7 +512,7 @@ mod tests { MetadataObject::Generic { title: Some(s!("abc")), thumbnail_url: Some(s!("def")), - custom: serde_json::Value::Null, + custom: Some(serde_json::Value::Null), } ); assert_eq!( @@ -510,7 +520,15 @@ mod tests { MetadataObject::Generic { title: None, thumbnail_url: None, - custom: serde_json::Value::Null, + custom: Some(serde_json::Value::Null), + } + ); + assert_eq!( + serde_json::from_str::(r#"{"type":0}"#).unwrap(), + MetadataObject::Generic { + title: None, + thumbnail_url: None, + custom: None, } ); assert!(serde_json::from_str::(r#"{"type":1"#).is_err());