events: Remove Raw::deserialize_content

Replace it with deserialize_as or deserialize_with_type.
This commit is contained in:
Kévin Commaille 2023-01-06 16:53:08 +01:00 committed by Kévin Commaille
parent dc591647f8
commit 9a9bd2c933
7 changed files with 23 additions and 26 deletions

View File

@ -42,7 +42,12 @@ pub mod v3 {
pub struct Response { pub struct Response {
/// Account data content for the given type. /// 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::<T>()` or `.cast_ref::<T>().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)] #[ruma_api(body)]
pub account_data: Raw<AnyGlobalAccountDataEventContent>, pub account_data: Raw<AnyGlobalAccountDataEventContent>,
} }

View File

@ -46,7 +46,8 @@ pub mod v3 {
pub struct Response { pub struct Response {
/// Account data content for the given type. /// 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)] #[ruma_api(body)]
pub account_data: Raw<AnyRoomAccountDataEventContent>, pub account_data: Raw<AnyRoomAccountDataEventContent>,
} }

View File

@ -52,7 +52,7 @@ pub mod v3 {
/// The content of the state event. /// The content of the state event.
/// ///
/// Since the inner type of the `Raw` does not implement `Deserialize`, you need to use /// 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)] #[ruma_api(body)]
pub content: Raw<AnyStateEventContent>, pub content: Raw<AnyStateEventContent>,
} }

View File

@ -53,6 +53,8 @@ Breaking changes:
* Use `EventContentFromType::from_parts` instead * Use `EventContentFromType::from_parts` instead
* Remove `StateUnsignedFromParts` * Remove `StateUnsignedFromParts`
* Replace it with a bound on `DeserializeOwned` * Replace it with a bound on `DeserializeOwned`
* Remove `Raw::deserialize_content`
* Instead, use `.deserialize_as::<T>()` or `.cast_ref::<T>().deserialize_with_type()`
Improvements: Improvements:

View File

@ -29,12 +29,12 @@ pub trait EventContent: Sized + Serialize {
impl<T> Raw<T> impl<T> Raw<T>
where where
T: EventContent, T: EventContentFromType,
T::EventType: fmt::Display, T::EventType: fmt::Display,
{ {
/// Try to deserialize the JSON as an event's content. /// Try to deserialize the JSON as an event's content with the given event type.
pub fn deserialize_content(&self, event_type: T::EventType) -> serde_json::Result<T> { pub fn deserialize_with_type(&self, event_type: T::EventType) -> serde_json::Result<T> {
T::from_parts(&event_type.to_string(), self.json()) <T as EventContentFromType>::from_parts(&event_type.to_string(), self.json())
} }
} }

View File

@ -3,13 +3,13 @@ use assign::assign;
use js_int::{uint, UInt}; use js_int::{uint, UInt};
use ruma_common::{ use ruma_common::{
events::{ events::{
call::answer::CallAnswerEventContent,
room::{ImageInfo, MediaSource, ThumbnailInfo}, room::{ImageInfo, MediaSource, ThumbnailInfo},
sticker::StickerEventContent, sticker::StickerEventContent,
AnyMessageLikeEvent, AnyMessageLikeEventContent, AnySyncMessageLikeEvent, MessageLikeEvent, AnyMessageLikeEvent, AnySyncMessageLikeEvent, MessageLikeEvent,
MessageLikeEventType,
}, },
mxc_uri, room_id, mxc_uri, room_id,
serde::{CanBeEmpty, Raw}, serde::CanBeEmpty,
MilliSecondsSinceUnixEpoch, VoipVersionId, MilliSecondsSinceUnixEpoch, VoipVersionId,
}; };
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; 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 "version": 0
}); });
let content = assert_matches!( let content = from_json_value::<CallAnswerEventContent>(json_data).unwrap();
from_json_value::<Raw<AnyMessageLikeEventContent>>(json_data)
.unwrap()
.deserialize_content(MessageLikeEventType::CallAnswer)
.unwrap(),
AnyMessageLikeEventContent::CallAnswer(content) => content
);
assert_eq!(content.answer.sdp, "Hello"); assert_eq!(content.answer.sdp, "Hello");
assert_eq!(content.call_id, "foofoo"); assert_eq!(content.call_id, "foofoo");

View File

@ -2,11 +2,11 @@ use assert_matches::assert_matches;
use js_int::uint; use js_int::uint;
use ruma_common::{ use ruma_common::{
events::{ events::{
AnyStateEvent, AnyStateEventContent, AnySyncStateEvent, AnyTimelineEvent, StateEvent, room::aliases::RoomAliasesEventContent, AnyStateEvent, AnySyncStateEvent, AnyTimelineEvent,
StateEventType, SyncStateEvent, StateEvent, SyncStateEvent,
}, },
mxc_uri, room_alias_id, mxc_uri, room_alias_id,
serde::{CanBeEmpty, Raw}, serde::CanBeEmpty,
MilliSecondsSinceUnixEpoch, MilliSecondsSinceUnixEpoch,
}; };
use serde_json::{from_value as from_json_value, json, Value as JsonValue}; use serde_json::{from_value as from_json_value, json, Value as JsonValue};
@ -36,12 +36,7 @@ fn deserialize_aliases_content() {
"aliases": ["#somewhere:localhost"], "aliases": ["#somewhere:localhost"],
}); });
let content = assert_matches!( let content = from_json_value::<RoomAliasesEventContent>(json_data).unwrap();
from_json_value::<Raw<AnyStateEventContent>>(json_data)
.unwrap()
.deserialize_content(StateEventType::RoomAliases),
Ok(AnyStateEventContent::RoomAliases(content)) => content
);
assert_eq!(content.aliases, vec![room_alias_id!("#somewhere:localhost")]); assert_eq!(content.aliases, vec![room_alias_id!("#somewhere:localhost")]);
} }