diff --git a/crates/ruma-events-macros/src/event_enum.rs b/crates/ruma-events-macros/src/event_enum.rs index 6c0cff1c..e0fc0e17 100644 --- a/crates/ruma-events-macros/src/event_enum.rs +++ b/crates/ruma-events-macros/src/event_enum.rs @@ -161,10 +161,10 @@ fn expand_any_with_deser( use #serde::de::Error as _; let json = Box::<#serde_json::value::RawValue>::deserialize(deserializer)?; - let #ruma_events::EventDeHelper { ev_type, .. } = + let #ruma_events::EventTypeDeHelper { ev_type, .. } = #ruma_events::from_raw_json_value(&json)?; - match ev_type.as_str() { + match &*ev_type { #( #variant_attrs #events => { let event = #serde_json::from_str::<#content>(json.get()) @@ -609,7 +609,7 @@ fn expand_redacted_enum( D: #serde::de::Deserializer<'de>, { let json = Box::<#serde_json::value::RawValue>::deserialize(deserializer)?; - let #ruma_events::EventDeHelper { unsigned, .. } = + let #ruma_events::RedactionDeHelper { unsigned } = #ruma_events::from_raw_json_value(&json)?; Ok(match unsigned { diff --git a/crates/ruma-events/src/enums.rs b/crates/ruma-events/src/enums.rs index 3ba2f038..367a4af4 100644 --- a/crates/ruma-events/src/enums.rs +++ b/crates/ruma-events/src/enums.rs @@ -1,10 +1,10 @@ use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events_macros::event_enum; use ruma_identifiers::{EventId, RoomId, RoomVersionId, UserId}; -use serde::{de, Serialize}; +use serde::{de, Deserialize, Serialize}; use serde_json::value::RawValue as RawJsonValue; -use crate::{from_raw_json_value, room::redaction::SyncRedactionEvent, EventDeHelper, Redact}; +use crate::{from_raw_json_value, room::redaction::SyncRedactionEvent, Redact, UnsignedDeHelper}; event_enum! { /// Any global account data event. @@ -197,13 +197,20 @@ impl AnySyncRoomEvent { } } -impl<'de> de::Deserialize<'de> for AnyRoomEvent { +#[derive(Deserialize)] +#[allow(clippy::exhaustive_structs)] +struct EventDeHelper { + pub state_key: Option, + pub unsigned: Option, +} + +impl<'de> Deserialize<'de> for AnyRoomEvent { fn deserialize(deserializer: D) -> Result where D: de::Deserializer<'de>, { let json = Box::::deserialize(deserializer)?; - let EventDeHelper { state_key, unsigned, .. } = from_raw_json_value(&json)?; + let EventDeHelper { state_key, unsigned } = from_raw_json_value(&json)?; if state_key.is_some() { Ok(match unsigned { @@ -223,13 +230,13 @@ impl<'de> de::Deserialize<'de> for AnyRoomEvent { } } -impl<'de> de::Deserialize<'de> for AnySyncRoomEvent { +impl<'de> Deserialize<'de> for AnySyncRoomEvent { fn deserialize(deserializer: D) -> Result where D: de::Deserializer<'de>, { let json = Box::::deserialize(deserializer)?; - let EventDeHelper { state_key, unsigned, .. } = from_raw_json_value(&json)?; + let EventDeHelper { state_key, unsigned } = from_raw_json_value(&json)?; if state_key.is_some() { Ok(match unsigned { diff --git a/crates/ruma-events/src/lib.rs b/crates/ruma-events/src/lib.rs index 5f81e9bd..e5288fdf 100644 --- a/crates/ruma-events/src/lib.rs +++ b/crates/ruma-events/src/lib.rs @@ -430,45 +430,36 @@ pub enum HasDeserializeFields { Optional, } -/// Helper struct to determine if the event has been redacted. +/// Helper struct to determine the event kind from a `serde_json::value::RawValue`. #[doc(hidden)] -#[derive(Debug, Deserialize)] +#[derive(Deserialize)] +#[allow(clippy::exhaustive_structs)] +pub struct EventTypeDeHelper<'a> { + #[serde(borrow, rename = "type")] + pub ev_type: std::borrow::Cow<'a, str>, +} + +/// Helper struct to determine if an event has been redacted. +#[doc(hidden)] +#[derive(Deserialize)] +pub struct RedactionDeHelper { + /// Used to check whether redacted_because exists. + pub unsigned: Option, +} + +#[doc(hidden)] +#[derive(Deserialize)] #[allow(clippy::exhaustive_structs)] pub struct UnsignedDeHelper { /// This is the field that signals an event has been redacted. pub redacted_because: Option, } -/// Helper struct to determine the event kind from a `serde_json::value::RawValue`. -#[doc(hidden)] -#[derive(Debug, Deserialize)] -#[allow(clippy::exhaustive_structs)] -pub struct EventDeHelper { - /// the Matrix event type string "m.room.whatever". - #[serde(rename = "type")] - pub ev_type: String, - - /// If `state_key` is present the event will be deserialized as a state event. - pub state_key: Option, - - /// If no `state_key` is found but an `event_id` is present the event - /// will be deserialized as a message event. - pub event_id: Option, - - /// If no `event_id` or `state_key` are found but a `room_id` is present - /// the event will be deserialized as an ephemeral event. - pub room_id: Option, - - /// If this `UnsignedData` contains a `redacted_because` key the event is - /// immediately deserialized as a redacted event. - pub unsigned: Option, -} - /// Helper function for `serde_json::value::RawValue` deserialization. #[doc(hidden)] -pub fn from_raw_json_value(val: &RawJsonValue) -> Result +pub fn from_raw_json_value<'a, T, E>(val: &'a RawJsonValue) -> Result where - T: de::DeserializeOwned, + T: de::Deserialize<'a>, E: de::Error, { serde_json::from_str(val.get()).map_err(E::custom)