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 {
/// 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)]
pub account_data: Raw<AnyGlobalAccountDataEventContent>,
}

View File

@ -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<AnyRoomAccountDataEventContent>,
}

View File

@ -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<AnyStateEventContent>,
}

View File

@ -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::<T>()` or `.cast_ref::<T>().deserialize_with_type()`
Improvements:

View File

@ -29,12 +29,12 @@ pub trait EventContent: Sized + Serialize {
impl<T> Raw<T>
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> {
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<T> {
<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 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::<Raw<AnyMessageLikeEventContent>>(json_data)
.unwrap()
.deserialize_content(MessageLikeEventType::CallAnswer)
.unwrap(),
AnyMessageLikeEventContent::CallAnswer(content) => content
);
let content = from_json_value::<CallAnswerEventContent>(json_data).unwrap();
assert_eq!(content.answer.sdp, "Hello");
assert_eq!(content.call_id, "foofoo");

View File

@ -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::<Raw<AnyStateEventContent>>(json_data)
.unwrap()
.deserialize_content(StateEventType::RoomAliases),
Ok(AnyStateEventContent::RoomAliases(content)) => content
);
let content = from_json_value::<RoomAliasesEventContent>(json_data).unwrap();
assert_eq!(content.aliases, vec![room_alias_id!("#somewhere:localhost")]);
}