From 4a91932ea8492195405dc3d488869c440e9f9c76 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 3 Aug 2017 22:10:11 +0200 Subject: [PATCH 1/3] Fix a bunch more deserialization bugs --- src/call/candidates.rs | 2 ++ src/call/mod.rs | 1 + src/presence.rs | 1 + src/receipt.rs | 1 + src/room/avatar.rs | 9 ++++++--- src/room/mod.rs | 4 ++-- src/stripped.rs | 20 +++++++++++--------- 7 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/call/candidates.rs b/src/call/candidates.rs index a9642073..352ca046 100644 --- a/src/call/candidates.rs +++ b/src/call/candidates.rs @@ -24,7 +24,9 @@ pub struct Candidate { /// The SDP "a" line of the candidate. pub candidate: String, /// The SDP media type this candidate is intended for. + #[serde(rename = "sdpMid")] pub sdp_mid: String, /// The index of the SDP "m" line this candidate is intended for. + #[serde(rename = "sdpMLineIndex")] pub sdp_m_line_index: u64, } diff --git a/src/call/mod.rs b/src/call/mod.rs index d94786fe..6097f86f 100644 --- a/src/call/mod.rs +++ b/src/call/mod.rs @@ -11,6 +11,7 @@ pub mod invite; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct SessionDescription { /// The type of session description. + #[serde(rename="type")] pub session_type: SessionDescriptionType, /// The SDP text of the session description. pub sdp: String, diff --git a/src/presence.rs b/src/presence.rs index e86ee38c..5ed4001f 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -18,6 +18,7 @@ pub struct PresenceEventContent { pub avatar_url: Option, /// Whether or not the user is currently active. + #[serde(skip_serializing_if="Option::is_none")] pub currently_active: Option, /// The current display name for this user. diff --git a/src/receipt.rs b/src/receipt.rs index 42b6cabe..2d5b08cc 100644 --- a/src/receipt.rs +++ b/src/receipt.rs @@ -23,6 +23,7 @@ pub type ReceiptEventContent = HashMap; pub struct Receipts { /// A collection of users who have sent *m.read* receipts for this event. #[serde(rename="m.read")] + #[serde(default)] pub m_read: UserReceipts, } diff --git a/src/room/avatar.rs b/src/room/avatar.rs index d900fdbf..741a0f36 100644 --- a/src/room/avatar.rs +++ b/src/room/avatar.rs @@ -13,11 +13,14 @@ state_event! { #[derive(Clone, Debug, Deserialize, Serialize)] pub struct AvatarEventContent { /// Information about the avatar image. - pub info: ImageInfo, + #[serde(skip_serializing_if="Option::is_none")] + pub info: Option, /// Information about the avatar thumbnail image. - pub thumbnail_info: ImageInfo, + #[serde(skip_serializing_if="Option::is_none")] + pub thumbnail_info: Option, /// URL of the avatar thumbnail image. - pub thumbnail_url: String, + #[serde(skip_serializing_if="Option::is_none")] + pub thumbnail_url: Option, /// URL of the avatar image. pub url: String, } diff --git a/src/room/mod.rs b/src/room/mod.rs index 95b5a186..b4f73d11 100644 --- a/src/room/mod.rs +++ b/src/room/mod.rs @@ -21,11 +21,11 @@ pub mod topic; #[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] pub struct ImageInfo { /// The height of the image in pixels. - pub height: u64, + pub h: u64, /// The MIME type of the image, e.g. "image/png." pub mimetype: String, /// The file size of the image in bytes. pub size: u64, /// The width of the image in pixels. - pub width: u64, + pub w: u64, } diff --git a/src/stripped.rs b/src/stripped.rs index 04ed37e4..a760e456 100644 --- a/src/stripped.rs +++ b/src/stripped.rs @@ -291,14 +291,14 @@ mod tests { "state_key": "", "content": { "info": { - "height": 128, - "width": 128, + "h": 128, + "w": 128, "mimetype": "image/jpeg", "size": 1024 }, "thumbnail_info": { - "height": 16, - "width": 16, + "h": 16, + "w": 16, "mimetype": "image/jpeg", "size": 32 }, @@ -331,11 +331,13 @@ mod tests { match from_str::(avatar_event).unwrap() { StrippedState::RoomAvatar(event) => { - assert_eq!(event.content.info.height, 128); - assert_eq!(event.content.info.width, 128); - assert_eq!(event.content.info.mimetype, "image/jpeg"); - assert_eq!(event.content.info.size, 1024); - assert_eq!(event.content.thumbnail_info.size, 32); + let image_info = event.content.info.unwrap(); + + assert_eq!(image_info.h, 128); + assert_eq!(image_info.w, 128); + assert_eq!(image_info.mimetype, "image/jpeg"); + assert_eq!(image_info.size, 1024); + assert_eq!(event.content.thumbnail_info.unwrap().size, 32); assert_eq!(event.content.url, "https://domain.com/image.jpg"); assert_eq!(event.event_type, EventType::RoomAvatar); assert_eq!(event.state_key, ""); From b852b15134c0ebdadfc5e5b7f9d2a85b1758533c Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 3 Aug 2017 22:10:56 +0200 Subject: [PATCH 2/3] Fix grammar in doc comments --- src/room/message.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/room/message.rs b/src/room/message.rs index 32d50631..e0bbcc15 100644 --- a/src/room/message.rs +++ b/src/room/message.rs @@ -56,22 +56,22 @@ pub enum MessageEventContent { /// An emote message. Emote(EmoteMessageEventContent), - /// An file message. + /// A file message. File(FileMessageEventContent), /// An image message. Image(ImageMessageEventContent), - /// An location message. + /// A location message. Location(LocationMessageEventContent), - /// An notice message. + /// A notice message. Notice(NoticeMessageEventContent), /// An text message. Text(TextMessageEventContent), - /// An video message. + /// A video message. Video(VideoMessageEventContent), } From b741ec302872ab4a12a1cc49f070b02b8b28ea1a Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 4 Aug 2017 10:35:56 +0200 Subject: [PATCH 3/3] Use more descriptive names for properties with one-character-names in the spec --- src/room/message.rs | 6 ++++-- src/room/mod.rs | 6 ++++-- src/stripped.rs | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/room/message.rs b/src/room/message.rs index e0bbcc15..487ff2f3 100644 --- a/src/room/message.rs +++ b/src/room/message.rs @@ -221,8 +221,9 @@ pub struct VideoInfo { #[serde(skip_serializing_if="Option::is_none")] pub duration: Option, /// The height of the video in pixels. + #[serde(rename = "h")] #[serde(skip_serializing_if="Option::is_none")] - pub h: Option, + pub height: Option, /// The mimetype of the video, e.g. "video/mp4." #[serde(skip_serializing_if="Option::is_none")] pub mimetype: Option, @@ -236,8 +237,9 @@ pub struct VideoInfo { #[serde(skip_serializing_if="Option::is_none")] pub thumbnail_url: Option, /// The width of the video in pixels. + #[serde(rename = "w")] #[serde(skip_serializing_if="Option::is_none")] - pub w: Option, + pub width: Option, } impl_enum! { diff --git a/src/room/mod.rs b/src/room/mod.rs index b4f73d11..232f7556 100644 --- a/src/room/mod.rs +++ b/src/room/mod.rs @@ -21,11 +21,13 @@ pub mod topic; #[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] pub struct ImageInfo { /// The height of the image in pixels. - pub h: u64, + #[serde(rename = "h")] + pub height: u64, /// The MIME type of the image, e.g. "image/png." pub mimetype: String, /// The file size of the image in bytes. pub size: u64, /// The width of the image in pixels. - pub w: u64, + #[serde(rename = "w")] + pub width: u64, } diff --git a/src/stripped.rs b/src/stripped.rs index a760e456..28d50500 100644 --- a/src/stripped.rs +++ b/src/stripped.rs @@ -333,8 +333,8 @@ mod tests { StrippedState::RoomAvatar(event) => { let image_info = event.content.info.unwrap(); - assert_eq!(image_info.h, 128); - assert_eq!(image_info.w, 128); + assert_eq!(image_info.height, 128); + assert_eq!(image_info.width, 128); assert_eq!(image_info.mimetype, "image/jpeg"); assert_eq!(image_info.size, 1024); assert_eq!(event.content.thumbnail_info.unwrap().size, 32);