diff --git a/crates/ruma-client-api/src/config/get_global_account_data.rs b/crates/ruma-client-api/src/config/get_global_account_data.rs index 2e0940e7..db66df25 100644 --- a/crates/ruma-client-api/src/config/get_global_account_data.rs +++ b/crates/ruma-client-api/src/config/get_global_account_data.rs @@ -42,7 +42,12 @@ pub mod v3 { pub struct Response { /// Account data content for the given type. /// - /// Use [`Raw::deserialize_content`] for deserialization. + /// Since the inner type of the `Raw` does not implement `Deserialize`, you need to use + /// `.deserialize_as::()` or `.cast_ref::().deserialize_with_type()` for event + /// types with a variable suffix (like [`SecretStorageKeyEventContent`]) to + /// deserialize it. + /// + /// [`SecretStorageKeyEventContent`]: ruma_common::events::secret_storage::key::SecretStorageKeyEventContent #[ruma_api(body)] pub account_data: Raw, } diff --git a/crates/ruma-client-api/src/config/get_room_account_data.rs b/crates/ruma-client-api/src/config/get_room_account_data.rs index b7d42a1e..8e62cd60 100644 --- a/crates/ruma-client-api/src/config/get_room_account_data.rs +++ b/crates/ruma-client-api/src/config/get_room_account_data.rs @@ -46,7 +46,8 @@ pub mod v3 { pub struct Response { /// Account data content for the given type. /// - /// Use [`Raw::deserialize_content`] for deserialization. + /// Since the inner type of the `Raw` does not implement `Deserialize`, you need to use + /// [`Raw::deserialize_as`] to deserialize it. #[ruma_api(body)] pub account_data: Raw, } diff --git a/crates/ruma-client-api/src/state/get_state_events_for_key.rs b/crates/ruma-client-api/src/state/get_state_events_for_key.rs index 1e4cd406..f319a574 100644 --- a/crates/ruma-client-api/src/state/get_state_events_for_key.rs +++ b/crates/ruma-client-api/src/state/get_state_events_for_key.rs @@ -52,7 +52,7 @@ pub mod v3 { /// The content of the state event. /// /// Since the inner type of the `Raw` does not implement `Deserialize`, you need to use - /// [`Raw::deserialize_content`] to deserialize it. + /// [`Raw::deserialize_as`] to deserialize it. #[ruma_api(body)] pub content: Raw, } diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index d25a019f..26dfc4b4 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -53,6 +53,8 @@ Breaking changes: * Use `EventContentFromType::from_parts` instead * Remove `StateUnsignedFromParts` * Replace it with a bound on `DeserializeOwned` +* Remove `Raw::deserialize_content` + * Instead, use `.deserialize_as::()` or `.cast_ref::().deserialize_with_type()` Improvements: diff --git a/crates/ruma-common/src/events/content.rs b/crates/ruma-common/src/events/content.rs index c600dd21..3059569a 100644 --- a/crates/ruma-common/src/events/content.rs +++ b/crates/ruma-common/src/events/content.rs @@ -29,12 +29,12 @@ pub trait EventContent: Sized + Serialize { impl Raw where - T: EventContent, + T: EventContentFromType, T::EventType: fmt::Display, { - /// Try to deserialize the JSON as an event's content. - pub fn deserialize_content(&self, event_type: T::EventType) -> serde_json::Result { - T::from_parts(&event_type.to_string(), self.json()) + /// Try to deserialize the JSON as an event's content with the given event type. + pub fn deserialize_with_type(&self, event_type: T::EventType) -> serde_json::Result { + ::from_parts(&event_type.to_string(), self.json()) } } diff --git a/crates/ruma-common/tests/events/message_event.rs b/crates/ruma-common/tests/events/message_event.rs index 4c14461e..ebfa08d3 100644 --- a/crates/ruma-common/tests/events/message_event.rs +++ b/crates/ruma-common/tests/events/message_event.rs @@ -3,13 +3,13 @@ use assign::assign; use js_int::{uint, UInt}; use ruma_common::{ events::{ + call::answer::CallAnswerEventContent, room::{ImageInfo, MediaSource, ThumbnailInfo}, sticker::StickerEventContent, - AnyMessageLikeEvent, AnyMessageLikeEventContent, AnySyncMessageLikeEvent, MessageLikeEvent, - MessageLikeEventType, + AnyMessageLikeEvent, AnySyncMessageLikeEvent, MessageLikeEvent, }, mxc_uri, room_id, - serde::{CanBeEmpty, Raw}, + serde::CanBeEmpty, MilliSecondsSinceUnixEpoch, VoipVersionId, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; @@ -107,13 +107,7 @@ fn deserialize_message_call_answer_content() { "version": 0 }); - let content = assert_matches!( - from_json_value::>(json_data) - .unwrap() - .deserialize_content(MessageLikeEventType::CallAnswer) - .unwrap(), - AnyMessageLikeEventContent::CallAnswer(content) => content - ); + let content = from_json_value::(json_data).unwrap(); assert_eq!(content.answer.sdp, "Hello"); assert_eq!(content.call_id, "foofoo"); diff --git a/crates/ruma-common/tests/events/state_event.rs b/crates/ruma-common/tests/events/state_event.rs index 6b58a398..9865a16b 100644 --- a/crates/ruma-common/tests/events/state_event.rs +++ b/crates/ruma-common/tests/events/state_event.rs @@ -2,11 +2,11 @@ use assert_matches::assert_matches; use js_int::uint; use ruma_common::{ events::{ - AnyStateEvent, AnyStateEventContent, AnySyncStateEvent, AnyTimelineEvent, StateEvent, - StateEventType, SyncStateEvent, + room::aliases::RoomAliasesEventContent, AnyStateEvent, AnySyncStateEvent, AnyTimelineEvent, + StateEvent, SyncStateEvent, }, mxc_uri, room_alias_id, - serde::{CanBeEmpty, Raw}, + serde::CanBeEmpty, MilliSecondsSinceUnixEpoch, }; use serde_json::{from_value as from_json_value, json, Value as JsonValue}; @@ -36,12 +36,7 @@ fn deserialize_aliases_content() { "aliases": ["#somewhere:localhost"], }); - let content = assert_matches!( - from_json_value::>(json_data) - .unwrap() - .deserialize_content(StateEventType::RoomAliases), - Ok(AnyStateEventContent::RoomAliases(content)) => content - ); + let content = from_json_value::(json_data).unwrap(); assert_eq!(content.aliases, vec![room_alias_id!("#somewhere:localhost")]); }