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
master
Paul Woolcock 6 years ago
parent 2e8ee7e840
commit 49eee2313f
  1. 23
      src/entities/attachment.rs

@ -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.

Loading…
Cancel
Save