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:
parent
a955097989
commit
156b4ff624
1 changed files with 26 additions and 8 deletions
|
@ -15,7 +15,7 @@ pub enum MetadataObject {
|
|||
Generic {
|
||||
title: Option<String>,
|
||||
thumbnail_url: Option<String>,
|
||||
custom: Value,
|
||||
custom: Option<Value>,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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::<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());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue