From 49eee2313fe2a64917ec39976eae5e2db153e4df Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Wed, 22 Aug 2018 14:35:37 -0400 Subject: [PATCH] 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 --- src/entities/attachment.rs | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/src/entities/attachment.rs b/src/entities/attachment.rs index 2784987..dbc560f 100644 --- a/src/entities/attachment.rs +++ b/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, /// Meta information about the attachment. - #[serde(deserialize_with="empty_as_none")] pub meta: Option, /// Noop will be removed. pub description: Option, } -fn empty_as_none<'de, D: Deserializer<'de>>(val: D) - -> Result, 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, /// Smaller version. - small: ImageDetails, + pub small: Option, } /// Dimensions of an attachement.