events: Replace derived Deserialize for MessageEventContent with a manual impl

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
This commit is contained in:
Akshay 2021-02-12 13:33:09 +01:00 committed by Jonas Platte
parent 317b2055a8
commit 526542c246
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
2 changed files with 40 additions and 1 deletions

View File

@ -18,6 +18,7 @@ use super::{relationships::RelatesToJsonRepr, EncryptedFile, ImageInfo, Thumbnai
// FIXME: Do we want to keep re-exporting this? // FIXME: Do we want to keep re-exporting this?
pub use super::relationships::InReplyTo; pub use super::relationships::InReplyTo;
mod content_serde;
pub mod feedback; pub mod feedback;
use crate::MessageEvent as OuterMessageEvent; use crate::MessageEvent as OuterMessageEvent;
@ -28,7 +29,7 @@ use crate::MessageEvent as OuterMessageEvent;
pub type MessageEvent = OuterMessageEvent<MessageEventContent>; pub type MessageEvent = OuterMessageEvent<MessageEventContent>;
/// The payload for `MessageEvent`. /// The payload for `MessageEvent`.
#[derive(Clone, Debug, Deserialize, Serialize, MessageEventContent)] #[derive(Clone, Debug, Serialize, MessageEventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.room.message")] #[ruma_event(type = "m.room.message")]
#[serde(tag = "msgtype")] #[serde(tag = "msgtype")]

View File

@ -0,0 +1,38 @@
//! `Deserialize` implementation for MessageEventContent
use serde::{de, Deserialize};
use serde_json::value::RawValue as RawJsonValue;
use crate::{from_raw_json_value, room::message::MessageEventContent};
/// Helper struct to determine the msgtype from a `serde_json::value::RawValue`
#[derive(Debug, Deserialize)]
struct MessageDeHelper {
/// The message type field
msgtype: String,
}
impl<'de> de::Deserialize<'de> for MessageEventContent {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
let json = Box::<RawJsonValue>::deserialize(deserializer)?;
let MessageDeHelper { msgtype } = from_raw_json_value(&json)?;
Ok(match msgtype.as_ref() {
"m.audio" => Self::Audio(from_raw_json_value(&json)?),
"m.emote" => Self::Emote(from_raw_json_value(&json)?),
"m.file" => Self::File(from_raw_json_value(&json)?),
"m.image" => Self::Image(from_raw_json_value(&json)?),
"m.location" => Self::Location(from_raw_json_value(&json)?),
"m.notice" => Self::Notice(from_raw_json_value(&json)?),
"m.server_notice" => Self::ServerNotice(from_raw_json_value(&json)?),
"m.text" => Self::Text(from_raw_json_value(&json)?),
"m.video" => Self::Video(from_raw_json_value(&json)?),
#[cfg(feature = "unstable-pre-spec")]
"m.key.verification.request" => Self::VerificationRequest(from_raw_json_value(&json)?),
_ => return Err(de::Error::custom("unknown msgtype")),
})
}
}