events: Replace derived Deserialize for MessageEventContent with a manual impl
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
This commit is contained in:
parent
317b2055a8
commit
526542c246
@ -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")]
|
||||||
|
38
ruma-events/src/room/message/content_serde.rs
Normal file
38
ruma-events/src/room/message/content_serde.rs
Normal 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")),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user