1
0
Fork 0
mirror of https://gitlab.com/futo-org/fcast.git synced 2025-06-24 21:25:23 +00:00

rs-terminal: make custom property optional for generic metadata

This commit is contained in:
Marcus Hanestad 2025-06-24 08:27:54 +02:00
parent a955097989
commit 156b4ff624

View file

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