events: Make ImageInfo and ThumbnailInfo non-exhaustive

This commit is contained in:
Jonas Platte 2021-04-18 14:44:37 +02:00
parent dc57620ae0
commit 486c9245b3
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
4 changed files with 32 additions and 18 deletions

View File

@ -30,7 +30,8 @@ pub mod tombstone;
pub mod topic; pub mod topic;
/// Metadata about an image. /// 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 { pub struct ImageInfo {
/// The height of the image in pixels. /// The height of the image in pixels.
#[serde(rename = "h", skip_serializing_if = "Option::is_none")] #[serde(rename = "h", skip_serializing_if = "Option::is_none")]
@ -71,8 +72,16 @@ pub struct ImageInfo {
pub blurhash: Option<String>, pub blurhash: Option<String>,
} }
impl ImageInfo {
/// Creates an empty `ImageInfo`.
pub fn new() -> Self {
Self::default()
}
}
/// Metadata about a thumbnail. /// 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 { pub struct ThumbnailInfo {
/// The height of the thumbnail in pixels. /// The height of the thumbnail in pixels.
#[serde(rename = "h", skip_serializing_if = "Option::is_none")] #[serde(rename = "h", skip_serializing_if = "Option::is_none")]
@ -91,6 +100,13 @@ pub struct ThumbnailInfo {
pub size: Option<UInt>, pub size: Option<UInt>,
} }
impl ThumbnailInfo {
/// Creates an empty `ThumbnailInfo`.
pub fn new() -> Self {
Self::default()
}
}
/// A file sent to a room with end-to-end encryption enabled. /// A file sent to a room with end-to-end encryption enabled.
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EncryptedFile { pub struct EncryptedFile {

View File

@ -1,5 +1,6 @@
use std::time::{Duration, UNIX_EPOCH}; use std::time::{Duration, UNIX_EPOCH};
use assign::assign;
use js_int::UInt; use js_int::UInt;
use matches::assert_matches; use matches::assert_matches;
use ruma_identifiers::{event_id, mxc_uri, room_id, user_id}; 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 { let aliases_event = AnyMessageEvent::Sticker(MessageEvent {
content: StickerEventContent::new( content: StickerEventContent::new(
"Hello".into(), "Hello".into(),
ImageInfo { assign!(ImageInfo::new(), {
height: UInt::new(423), height: UInt::new(423),
width: UInt::new(1011), width: UInt::new(1011),
mimetype: Some("image/png".into()), mimetype: Some("image/png".into()),
size: UInt::new(84242), size: UInt::new(84242),
thumbnail_info: Some(Box::new(ThumbnailInfo { thumbnail_info: Some(Box::new(assign!(ThumbnailInfo::new(), {
width: UInt::new(800), width: UInt::new(800),
height: UInt::new(334), height: UInt::new(334),
mimetype: Some("image/png".into()), mimetype: Some("image/png".into()),
size: UInt::new(82595), size: UInt::new(82595),
})), }))),
thumbnail_url: Some(mxc_uri!("mxc://matrix.org/mnrsnsRRS787TSts")), 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"), mxc_uri!("mxc://matrix.org/arsrns98rsRSR"),
), ),
event_id: event_id!("$h29iv0s8:example.com"), event_id: event_id!("$h29iv0s8:example.com"),

View File

@ -1,5 +1,6 @@
use std::time::{Duration, UNIX_EPOCH}; use std::time::{Duration, UNIX_EPOCH};
use assign::assign;
use js_int::{uint, UInt}; use js_int::{uint, UInt};
use matches::assert_matches; use matches::assert_matches;
use ruma_events::{ use ruma_events::{
@ -17,22 +18,19 @@ fn message_serialize_sticker() {
let aliases_event = MessageEvent { let aliases_event = MessageEvent {
content: AnyMessageEventContent::Sticker(StickerEventContent::new( content: AnyMessageEventContent::Sticker(StickerEventContent::new(
"Hello".into(), "Hello".into(),
ImageInfo { assign!(ImageInfo::new(), {
height: UInt::new(423), height: UInt::new(423),
width: UInt::new(1011), width: UInt::new(1011),
mimetype: Some("image/png".into()), mimetype: Some("image/png".into()),
size: UInt::new(84242), size: UInt::new(84242),
thumbnail_info: Some(Box::new(ThumbnailInfo { thumbnail_info: Some(Box::new(assign!(ThumbnailInfo::new(), {
width: UInt::new(800), width: UInt::new(800),
height: UInt::new(334), height: UInt::new(334),
mimetype: Some("image/png".into()), mimetype: Some("image/png".into()),
size: UInt::new(82595), size: UInt::new(82595),
})), }))),
thumbnail_url: Some(mxc_uri!("mxc://matrix.org/irsns989Rrsn")), 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"), mxc_uri!("mxc://matrix.org/rnsldl8srs98IRrs"),
)), )),
event_id: event_id!("$h29iv0s8:example.com"), event_id: event_id!("$h29iv0s8:example.com"),
@ -193,6 +191,7 @@ fn deserialize_message_sticker() {
thumbnail_file: None, thumbnail_file: None,
#[cfg(feature = "unstable-pre-spec")] #[cfg(feature = "unstable-pre-spec")]
blurhash: None, blurhash: None,
..
}, },
url, url,
.. ..
@ -219,6 +218,7 @@ fn deserialize_message_sticker() {
height: thumb_height, height: thumb_height,
mimetype: thumb_mimetype, mimetype: thumb_mimetype,
size: thumb_size, size: thumb_size,
..
} if *thumb_width == UInt::new(800) } if *thumb_width == UInt::new(800)
&& *thumb_height == UInt::new(334) && *thumb_height == UInt::new(334)
&& *thumb_mimetype == Some("image/png".into()) && *thumb_mimetype == Some("image/png".into())

View File

@ -223,8 +223,7 @@ fn deserialize_avatar_without_prev_content() {
size, size,
thumbnail_info: Some(thumbnail_info), thumbnail_info: Some(thumbnail_info),
thumbnail_url: Some(thumbnail_url), thumbnail_url: Some(thumbnail_url),
#[cfg(feature = "unstable-pre-spec")] ..
blurhash: None,
} if *height == UInt::new(423) } if *height == UInt::new(423)
&& *width == UInt::new(1011) && *width == UInt::new(1011)
&& *mimetype == "image/png" && *mimetype == "image/png"
@ -236,6 +235,7 @@ fn deserialize_avatar_without_prev_content() {
height: thumb_height, height: thumb_height,
mimetype: thumb_mimetype, mimetype: thumb_mimetype,
size: thumb_size, size: thumb_size,
..
} if *thumb_width == UInt::new(800) } if *thumb_width == UInt::new(800)
&& *thumb_height == UInt::new(334) && *thumb_height == UInt::new(334)
&& *thumb_mimetype == Some("image/png".into()) && *thumb_mimetype == Some("image/png".into())