From 486c9245b3fbdebfbe9a0ffd86db0c698acc4fd0 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 18 Apr 2021 14:44:37 +0200 Subject: [PATCH] events: Make ImageInfo and ThumbnailInfo non-exhaustive --- ruma-events/src/room.rs | 20 ++++++++++++++++++-- ruma-events/tests/event_enums.rs | 12 +++++------- ruma-events/tests/message_event.rs | 14 +++++++------- ruma-events/tests/state_event.rs | 4 ++-- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/ruma-events/src/room.rs b/ruma-events/src/room.rs index 29de2b8d..ed4f1810 100644 --- a/ruma-events/src/room.rs +++ b/ruma-events/src/room.rs @@ -30,7 +30,8 @@ pub mod tombstone; pub mod topic; /// Metadata about an image. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct ImageInfo { /// The height of the image in pixels. #[serde(rename = "h", skip_serializing_if = "Option::is_none")] @@ -71,8 +72,16 @@ pub struct ImageInfo { pub blurhash: Option, } +impl ImageInfo { + /// Creates an empty `ImageInfo`. + pub fn new() -> Self { + Self::default() + } +} + /// Metadata about a thumbnail. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct ThumbnailInfo { /// The height of the thumbnail in pixels. #[serde(rename = "h", skip_serializing_if = "Option::is_none")] @@ -91,6 +100,13 @@ pub struct ThumbnailInfo { pub size: Option, } +impl ThumbnailInfo { + /// Creates an empty `ThumbnailInfo`. + pub fn new() -> Self { + Self::default() + } +} + /// A file sent to a room with end-to-end encryption enabled. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct EncryptedFile { diff --git a/ruma-events/tests/event_enums.rs b/ruma-events/tests/event_enums.rs index e0b62067..b4f6fa12 100644 --- a/ruma-events/tests/event_enums.rs +++ b/ruma-events/tests/event_enums.rs @@ -1,5 +1,6 @@ use std::time::{Duration, UNIX_EPOCH}; +use assign::assign; use js_int::UInt; use matches::assert_matches; use ruma_identifiers::{event_id, mxc_uri, room_id, user_id}; @@ -71,22 +72,19 @@ fn serialize_message_event() { let aliases_event = AnyMessageEvent::Sticker(MessageEvent { content: StickerEventContent::new( "Hello".into(), - ImageInfo { + assign!(ImageInfo::new(), { height: UInt::new(423), width: UInt::new(1011), mimetype: Some("image/png".into()), size: UInt::new(84242), - thumbnail_info: Some(Box::new(ThumbnailInfo { + thumbnail_info: Some(Box::new(assign!(ThumbnailInfo::new(), { width: UInt::new(800), height: UInt::new(334), mimetype: Some("image/png".into()), size: UInt::new(82595), - })), + }))), thumbnail_url: Some(mxc_uri!("mxc://matrix.org/mnrsnsRRS787TSts")), - thumbnail_file: None, - #[cfg(feature = "unstable-pre-spec")] - blurhash: None, - }, + }), mxc_uri!("mxc://matrix.org/arsrns98rsRSR"), ), event_id: event_id!("$h29iv0s8:example.com"), diff --git a/ruma-events/tests/message_event.rs b/ruma-events/tests/message_event.rs index bd0345d7..3e581bcd 100644 --- a/ruma-events/tests/message_event.rs +++ b/ruma-events/tests/message_event.rs @@ -1,5 +1,6 @@ use std::time::{Duration, UNIX_EPOCH}; +use assign::assign; use js_int::{uint, UInt}; use matches::assert_matches; use ruma_events::{ @@ -17,22 +18,19 @@ fn message_serialize_sticker() { let aliases_event = MessageEvent { content: AnyMessageEventContent::Sticker(StickerEventContent::new( "Hello".into(), - ImageInfo { + assign!(ImageInfo::new(), { height: UInt::new(423), width: UInt::new(1011), mimetype: Some("image/png".into()), size: UInt::new(84242), - thumbnail_info: Some(Box::new(ThumbnailInfo { + thumbnail_info: Some(Box::new(assign!(ThumbnailInfo::new(), { width: UInt::new(800), height: UInt::new(334), mimetype: Some("image/png".into()), size: UInt::new(82595), - })), + }))), thumbnail_url: Some(mxc_uri!("mxc://matrix.org/irsns989Rrsn")), - thumbnail_file: None, - #[cfg(feature = "unstable-pre-spec")] - blurhash: None, - }, + }), mxc_uri!("mxc://matrix.org/rnsldl8srs98IRrs"), )), event_id: event_id!("$h29iv0s8:example.com"), @@ -193,6 +191,7 @@ fn deserialize_message_sticker() { thumbnail_file: None, #[cfg(feature = "unstable-pre-spec")] blurhash: None, + .. }, url, .. @@ -219,6 +218,7 @@ fn deserialize_message_sticker() { height: thumb_height, mimetype: thumb_mimetype, size: thumb_size, + .. } if *thumb_width == UInt::new(800) && *thumb_height == UInt::new(334) && *thumb_mimetype == Some("image/png".into()) diff --git a/ruma-events/tests/state_event.rs b/ruma-events/tests/state_event.rs index 32b3cded..68f42900 100644 --- a/ruma-events/tests/state_event.rs +++ b/ruma-events/tests/state_event.rs @@ -223,8 +223,7 @@ fn deserialize_avatar_without_prev_content() { size, thumbnail_info: Some(thumbnail_info), thumbnail_url: Some(thumbnail_url), - #[cfg(feature = "unstable-pre-spec")] - blurhash: None, + .. } if *height == UInt::new(423) && *width == UInt::new(1011) && *mimetype == "image/png" @@ -236,6 +235,7 @@ fn deserialize_avatar_without_prev_content() { height: thumb_height, mimetype: thumb_mimetype, size: thumb_size, + .. } if *thumb_width == UInt::new(800) && *thumb_height == UInt::new(334) && *thumb_mimetype == Some("image/png".into())