Make the Meta fields optional
using `deserialize_with` causes an error if the `"meta"` key is missing completely, which is not very useful. This isn't ideal, but it will make it so all the various states that "meta" can be in will deser correctly
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
//! Module containing everything related to media attachements.
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use super::Empty;
|
||||
|
||||
/// A struct representing a media attachment.
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
@@ -20,35 +18,18 @@ pub struct Attachment {
|
||||
/// (only present on local images)
|
||||
pub text_url: Option<String>,
|
||||
/// Meta information about the attachment.
|
||||
#[serde(deserialize_with="empty_as_none")]
|
||||
pub meta: Option<Meta>,
|
||||
/// Noop will be removed.
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
fn empty_as_none<'de, D: Deserializer<'de>>(val: D)
|
||||
-> Result<Option<Meta>, D::Error>
|
||||
{
|
||||
#[derive(Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum EmptyOrMeta {
|
||||
Empty(Empty),
|
||||
Meta(Meta),
|
||||
}
|
||||
|
||||
Ok(match EmptyOrMeta::deserialize(val)? {
|
||||
EmptyOrMeta::Empty(_) => None,
|
||||
EmptyOrMeta::Meta(m) => Some(m),
|
||||
})
|
||||
}
|
||||
|
||||
/// Information about the attachment itself.
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Meta {
|
||||
/// Original version.
|
||||
original: ImageDetails,
|
||||
pub original: Option<ImageDetails>,
|
||||
/// Smaller version.
|
||||
small: ImageDetails,
|
||||
pub small: Option<ImageDetails>,
|
||||
}
|
||||
|
||||
/// Dimensions of an attachement.
|
||||
|
||||
Reference in New Issue
Block a user