From 674137c41df348f0d4e64fb3f532c4db775f12b2 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 12 Sep 2022 11:44:05 +0200 Subject: [PATCH] events: Use CanBeEmpty trait for skipping unsigned serialization --- crates/ruma-common/src/events/room/member.rs | 4 ++-- crates/ruma-common/src/events/unsigned.rs | 17 +++++++++++++---- crates/ruma-common/tests/events/audio.rs | 2 +- crates/ruma-common/tests/events/event_enums.rs | 1 + crates/ruma-common/tests/events/file.rs | 2 +- crates/ruma-common/tests/events/image.rs | 2 +- crates/ruma-common/tests/events/location.rs | 4 +++- crates/ruma-common/tests/events/message.rs | 4 +++- .../ruma-common/tests/events/message_event.rs | 2 +- crates/ruma-common/tests/events/redaction.rs | 4 +++- crates/ruma-common/tests/events/state_event.rs | 2 +- crates/ruma-common/tests/events/video.rs | 2 +- crates/ruma-common/tests/events/voice.rs | 4 +++- crates/ruma-macros/src/events/event.rs | 2 +- 14 files changed, 35 insertions(+), 17 deletions(-) diff --git a/crates/ruma-common/src/events/room/member.rs b/crates/ruma-common/src/events/room/member.rs index b6f9fe25..0c7b9bcd 100644 --- a/crates/ruma-common/src/events/room/member.rs +++ b/crates/ruma-common/src/events/room/member.rs @@ -460,8 +460,8 @@ mod tests { use super::{MembershipState, RoomMemberEventContent}; use crate::{ - events::OriginalStateEvent, mxc_uri, server_name, server_signing_key_id, user_id, - MilliSecondsSinceUnixEpoch, + events::OriginalStateEvent, mxc_uri, serde::CanBeEmpty, server_name, server_signing_key_id, + user_id, MilliSecondsSinceUnixEpoch, }; #[test] diff --git a/crates/ruma-common/src/events/unsigned.rs b/crates/ruma-common/src/events/unsigned.rs index f66f6080..29b76453 100644 --- a/crates/ruma-common/src/events/unsigned.rs +++ b/crates/ruma-common/src/events/unsigned.rs @@ -3,7 +3,10 @@ use serde::{Deserialize, Serialize}; use serde_json::{from_str as from_json_str, value::RawValue as RawJsonValue}; use super::{relation::Relations, room::redaction::SyncRoomRedactionEvent, StateEventContent}; -use crate::{serde::Raw, OwnedTransactionId}; +use crate::{ + serde::{CanBeEmpty, Raw}, + OwnedTransactionId, +}; /// Extra information about a message event that is not incorporated into the event's hash. #[derive(Clone, Debug, Default, Deserialize, Serialize)] @@ -34,13 +37,15 @@ impl MessageLikeUnsigned { pub fn new() -> Self { Self::default() } +} +impl CanBeEmpty for MessageLikeUnsigned { /// Whether this unsigned data is empty (all fields are `None`). /// /// This method is used to determine whether to skip serializing the `unsigned` field in room /// events. Do not use it to determine whether an incoming `unsigned` field was present - it /// could still have been present but contained none of the known fields. - pub fn is_empty(&self) -> bool { + fn is_empty(&self) -> bool { self.age.is_none() && self.transaction_id.is_none() && self.relations.is_none() } } @@ -78,13 +83,15 @@ impl StateUnsigned { pub fn new() -> Self { Self { age: None, transaction_id: None, prev_content: None, relations: None } } +} +impl CanBeEmpty for StateUnsigned { /// Whether this unsigned data is empty (all fields are `None`). /// /// This method is used to determine whether to skip serializing the `unsigned` field in room /// events. Do not use it to determine whether an incoming `unsigned` field was present - it /// could still have been present but contained none of the known fields. - pub fn is_empty(&self) -> bool { + fn is_empty(&self) -> bool { self.age.is_none() && self.transaction_id.is_none() && self.prev_content.is_none() @@ -160,13 +167,15 @@ impl RedactedUnsigned { pub fn new_because(redacted_because: Box) -> Self { Self { redacted_because: Some(redacted_because) } } +} +impl CanBeEmpty for RedactedUnsigned { /// Whether this unsigned data is empty (`redacted_because` is `None`). /// /// This method is used to determine whether to skip serializing the `unsigned` field in /// redacted room events. Do not use it to determine whether an incoming `unsigned` field /// was present - it could still have been present but contained none of the known fields. - pub fn is_empty(&self) -> bool { + fn is_empty(&self) -> bool { self.redacted_because.is_none() } } diff --git a/crates/ruma-common/tests/events/audio.rs b/crates/ruma-common/tests/events/audio.rs index 24b3c035..8e01da7a 100644 --- a/crates/ruma-common/tests/events/audio.rs +++ b/crates/ruma-common/tests/events/audio.rs @@ -20,7 +20,7 @@ use ruma_common::{ AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent, }, mxc_uri, room_id, - serde::Base64, + serde::{Base64, CanBeEmpty}, user_id, MilliSecondsSinceUnixEpoch, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; diff --git a/crates/ruma-common/tests/events/event_enums.rs b/crates/ruma-common/tests/events/event_enums.rs index eeeff059..bca430b3 100644 --- a/crates/ruma-common/tests/events/event_enums.rs +++ b/crates/ruma-common/tests/events/event_enums.rs @@ -2,6 +2,7 @@ use assert_matches::assert_matches; use js_int::uint; use ruma_common::{ events::{AnyMessageLikeEvent, MessageLikeEvent}, + serde::CanBeEmpty, MilliSecondsSinceUnixEpoch, VoipVersionId, }; use serde_json::{from_value as from_json_value, json}; diff --git a/crates/ruma-common/tests/events/file.rs b/crates/ruma-common/tests/events/file.rs index e55cf818..01652e1c 100644 --- a/crates/ruma-common/tests/events/file.rs +++ b/crates/ruma-common/tests/events/file.rs @@ -17,7 +17,7 @@ use ruma_common::{ AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent, }, mxc_uri, room_id, - serde::Base64, + serde::{Base64, CanBeEmpty}, user_id, MilliSecondsSinceUnixEpoch, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; diff --git a/crates/ruma-common/tests/events/image.rs b/crates/ruma-common/tests/events/image.rs index b5a5b01f..8dc5d40f 100644 --- a/crates/ruma-common/tests/events/image.rs +++ b/crates/ruma-common/tests/events/image.rs @@ -21,7 +21,7 @@ use ruma_common::{ AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent, }, mxc_uri, room_id, - serde::Base64, + serde::{Base64, CanBeEmpty}, user_id, MilliSecondsSinceUnixEpoch, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; diff --git a/crates/ruma-common/tests/events/location.rs b/crates/ruma-common/tests/events/location.rs index fd6677a7..9677a063 100644 --- a/crates/ruma-common/tests/events/location.rs +++ b/crates/ruma-common/tests/events/location.rs @@ -13,7 +13,9 @@ use ruma_common::{ }, AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent, }, - room_id, user_id, MilliSecondsSinceUnixEpoch, + room_id, + serde::CanBeEmpty, + user_id, MilliSecondsSinceUnixEpoch, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; diff --git a/crates/ruma-common/tests/events/message.rs b/crates/ruma-common/tests/events/message.rs index c1a76160..29d249af 100644 --- a/crates/ruma-common/tests/events/message.rs +++ b/crates/ruma-common/tests/events/message.rs @@ -14,7 +14,9 @@ use ruma_common::{ }, AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent, }, - room_id, user_id, MilliSecondsSinceUnixEpoch, + room_id, + serde::CanBeEmpty, + user_id, MilliSecondsSinceUnixEpoch, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; diff --git a/crates/ruma-common/tests/events/message_event.rs b/crates/ruma-common/tests/events/message_event.rs index 3b1d3111..0f1a99d3 100644 --- a/crates/ruma-common/tests/events/message_event.rs +++ b/crates/ruma-common/tests/events/message_event.rs @@ -10,7 +10,7 @@ use ruma_common::{ MessageLikeEventType, MessageLikeUnsigned, OriginalMessageLikeEvent, }, mxc_uri, room_id, - serde::Raw, + serde::{CanBeEmpty, Raw}, user_id, MilliSecondsSinceUnixEpoch, VoipVersionId, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; diff --git a/crates/ruma-common/tests/events/redaction.rs b/crates/ruma-common/tests/events/redaction.rs index d2323a35..d607871c 100644 --- a/crates/ruma-common/tests/events/redaction.rs +++ b/crates/ruma-common/tests/events/redaction.rs @@ -8,7 +8,9 @@ use ruma_common::{ }, AnyMessageLikeEvent, MessageLikeUnsigned, }, - room_id, user_id, MilliSecondsSinceUnixEpoch, + room_id, + serde::CanBeEmpty, + user_id, MilliSecondsSinceUnixEpoch, }; use serde_json::{ from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue, diff --git a/crates/ruma-common/tests/events/state_event.rs b/crates/ruma-common/tests/events/state_event.rs index 8b0221f9..413dc043 100644 --- a/crates/ruma-common/tests/events/state_event.rs +++ b/crates/ruma-common/tests/events/state_event.rs @@ -9,7 +9,7 @@ use ruma_common::{ StateUnsigned, SyncStateEvent, }, mxc_uri, room_alias_id, room_id, - serde::Raw, + serde::{CanBeEmpty, Raw}, server_name, user_id, MilliSecondsSinceUnixEpoch, }; use serde_json::{ diff --git a/crates/ruma-common/tests/events/video.rs b/crates/ruma-common/tests/events/video.rs index b266f9a8..68cc1ec1 100644 --- a/crates/ruma-common/tests/events/video.rs +++ b/crates/ruma-common/tests/events/video.rs @@ -21,7 +21,7 @@ use ruma_common::{ AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent, }, mxc_uri, room_id, - serde::Base64, + serde::{Base64, CanBeEmpty}, user_id, MilliSecondsSinceUnixEpoch, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; diff --git a/crates/ruma-common/tests/events/voice.rs b/crates/ruma-common/tests/events/voice.rs index 3452a94c..3cfad370 100644 --- a/crates/ruma-common/tests/events/voice.rs +++ b/crates/ruma-common/tests/events/voice.rs @@ -19,7 +19,9 @@ use ruma_common::{ voice::{VoiceContent, VoiceEventContent}, AnyMessageLikeEvent, MessageLikeEvent, MessageLikeUnsigned, OriginalMessageLikeEvent, }, - mxc_uri, room_id, user_id, MilliSecondsSinceUnixEpoch, + mxc_uri, room_id, + serde::CanBeEmpty, + user_id, MilliSecondsSinceUnixEpoch, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; diff --git a/crates/ruma-macros/src/events/event.rs b/crates/ruma-macros/src/events/event.rs index fbf93688..1d165556 100644 --- a/crates/ruma-macros/src/events/event.rs +++ b/crates/ruma-macros/src/events/event.rs @@ -86,7 +86,7 @@ fn expand_serialize_event( } } else if name == "unsigned" { quote! { - if !self.unsigned.is_empty() { + if !#ruma_common::serde::is_empty(&self.unsigned) { state.serialize_field("unsigned", &self.unsigned)?; } }