From 1d8ea75f6f69a61afb336f142955462e60870d1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Tue, 10 Jan 2023 11:32:54 +0100 Subject: [PATCH] events: Remove EventContent::from_parts --- crates/ruma-common/CHANGELOG.md | 2 + crates/ruma-common/src/events/_custom.rs | 4 -- crates/ruma-common/src/events/content.rs | 6 +-- crates/ruma-common/src/events/kinds.rs | 6 +-- .../src/events/policy/rule/room.rs | 11 ----- .../src/events/policy/rule/server.rs | 11 ----- .../src/events/policy/rule/user.rs | 11 ----- crates/ruma-common/src/events/room/aliases.rs | 11 ----- crates/ruma-common/src/events/room/member.rs | 11 ----- .../src/events/secret_storage/key.rs | 21 +++------ crates/ruma-common/tests/events/redacted.rs | 4 +- crates/ruma-macros/src/events/event.rs | 45 ++++++++---------- .../ruma-macros/src/events/event_content.rs | 28 ----------- crates/ruma-macros/src/events/event_enum.rs | 47 +------------------ 14 files changed, 34 insertions(+), 184 deletions(-) diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index 26dfc4b4..ead76bbf 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -55,6 +55,8 @@ Breaking changes: * Replace it with a bound on `DeserializeOwned` * Remove `Raw::deserialize_content` * Instead, use `.deserialize_as::()` or `.cast_ref::().deserialize_with_type()` +* Remove `EventContent::from_parts` + * Replace it with `EventContentFromType::from_parts` Improvements: diff --git a/crates/ruma-common/src/events/_custom.rs b/crates/ruma-common/src/events/_custom.rs index 7221ffde..4023e48f 100644 --- a/crates/ruma-common/src/events/_custom.rs +++ b/crates/ruma-common/src/events/_custom.rs @@ -29,10 +29,6 @@ macro_rules! custom_event_content { fn event_type(&self) -> Self::EventType { self.event_type[..].into() } - - fn from_parts(event_type: &str, _content: &RawJsonValue) -> serde_json::Result { - Ok(Self { event_type: event_type.into() }) - } } impl EventContentFromType for $i { diff --git a/crates/ruma-common/src/events/content.rs b/crates/ruma-common/src/events/content.rs index 3059569a..18e19e11 100644 --- a/crates/ruma-common/src/events/content.rs +++ b/crates/ruma-common/src/events/content.rs @@ -21,10 +21,6 @@ pub trait EventContent: Sized + Serialize { /// Get the event's type, like `m.room.message`. fn event_type(&self) -> Self::EventType; - - /// Constructs the given event content. - #[doc(hidden)] - fn from_parts(event_type: &str, content: &RawJsonValue) -> serde_json::Result; } impl Raw @@ -34,7 +30,7 @@ where { /// 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 { - ::from_parts(&event_type.to_string(), self.json()) + T::from_parts(&event_type.to_string(), self.json()) } } diff --git a/crates/ruma-common/src/events/kinds.rs b/crates/ruma-common/src/events/kinds.rs index a71be062..024f71bf 100644 --- a/crates/ruma-common/src/events/kinds.rs +++ b/crates/ruma-common/src/events/kinds.rs @@ -5,7 +5,7 @@ use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize}; use serde_json::value::RawValue as RawJsonValue; use super::{ - EphemeralRoomEventContent, EventContent, GlobalAccountDataEventContent, + EphemeralRoomEventContent, EventContent, EventContentFromType, GlobalAccountDataEventContent, MessageLikeEventContent, MessageLikeEventType, MessageLikeUnsigned, OriginalStateEventContent, RedactContent, RedactedMessageLikeEventContent, RedactedStateEventContent, RedactedUnsigned, RedactionDeHelper, RoomAccountDataEventContent, StateEventContent, StateEventType, @@ -551,8 +551,8 @@ macro_rules! impl_possibly_redacted_event { impl<'de, C> Deserialize<'de> for $ty where - C: $content_trait + RedactContent, - C::Redacted: $redacted_content_trait, + C: $content_trait + EventContentFromType + RedactContent, + C::Redacted: $redacted_content_trait + EventContentFromType, $( C::Redacted: $trait, )? { fn deserialize(deserializer: D) -> Result diff --git a/crates/ruma-common/src/events/policy/rule/room.rs b/crates/ruma-common/src/events/policy/rule/room.rs index 6e017025..3ecbfc04 100644 --- a/crates/ruma-common/src/events/policy/rule/room.rs +++ b/crates/ruma-common/src/events/policy/rule/room.rs @@ -4,7 +4,6 @@ use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; -use serde_json::value::RawValue as RawJsonValue; use super::{PolicyRuleEventContent, PossiblyRedactedPolicyRuleEventContent}; use crate::events::{EventContent, StateEventContent, StateEventType}; @@ -30,16 +29,6 @@ impl EventContent for PossiblyRedactedPolicyRuleRoomEventContent { fn event_type(&self) -> Self::EventType { StateEventType::PolicyRuleRoom } - - fn from_parts(event_type: &str, content: &RawJsonValue) -> serde_json::Result { - if event_type != "m.policy.rule.room" { - return Err(::serde::de::Error::custom(format!( - "expected event type `m.policy.rule.room`, found `{event_type}`", - ))); - } - - serde_json::from_str(content.get()) - } } impl StateEventContent for PossiblyRedactedPolicyRuleRoomEventContent { diff --git a/crates/ruma-common/src/events/policy/rule/server.rs b/crates/ruma-common/src/events/policy/rule/server.rs index ce6084e9..54ce0d7a 100644 --- a/crates/ruma-common/src/events/policy/rule/server.rs +++ b/crates/ruma-common/src/events/policy/rule/server.rs @@ -4,7 +4,6 @@ use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; -use serde_json::value::RawValue as RawJsonValue; use super::{PolicyRuleEventContent, PossiblyRedactedPolicyRuleEventContent}; use crate::events::{EventContent, StateEventContent, StateEventType}; @@ -30,16 +29,6 @@ impl EventContent for PossiblyRedactedPolicyRuleServerEventContent { fn event_type(&self) -> Self::EventType { StateEventType::PolicyRuleServer } - - fn from_parts(event_type: &str, content: &RawJsonValue) -> serde_json::Result { - if event_type != "m.policy.rule.server" { - return Err(::serde::de::Error::custom(format!( - "expected event type `m.policy.rule.server`, found `{event_type}`", - ))); - } - - serde_json::from_str(content.get()) - } } impl StateEventContent for PossiblyRedactedPolicyRuleServerEventContent { diff --git a/crates/ruma-common/src/events/policy/rule/user.rs b/crates/ruma-common/src/events/policy/rule/user.rs index dfcd267c..bf9cd468 100644 --- a/crates/ruma-common/src/events/policy/rule/user.rs +++ b/crates/ruma-common/src/events/policy/rule/user.rs @@ -4,7 +4,6 @@ use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; -use serde_json::value::RawValue as RawJsonValue; use super::{PolicyRuleEventContent, PossiblyRedactedPolicyRuleEventContent}; use crate::events::{EventContent, StateEventContent, StateEventType}; @@ -30,16 +29,6 @@ impl EventContent for PossiblyRedactedPolicyRuleUserEventContent { fn event_type(&self) -> Self::EventType { StateEventType::PolicyRuleUser } - - fn from_parts(event_type: &str, content: &RawJsonValue) -> serde_json::Result { - if event_type != "m.policy.rule.user" { - return Err(::serde::de::Error::custom(format!( - "expected event type `m.policy.rule.user`, found `{event_type}`", - ))); - } - - serde_json::from_str(content.get()) - } } impl StateEventContent for PossiblyRedactedPolicyRuleUserEventContent { diff --git a/crates/ruma-common/src/events/room/aliases.rs b/crates/ruma-common/src/events/room/aliases.rs index 7dfe5da5..d31c9539 100644 --- a/crates/ruma-common/src/events/room/aliases.rs +++ b/crates/ruma-common/src/events/room/aliases.rs @@ -2,7 +2,6 @@ use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; -use serde_json::value::RawValue as RawJsonValue; use crate::{ events::{ @@ -83,16 +82,6 @@ impl EventContent for RedactedRoomAliasesEventContent { fn event_type(&self) -> StateEventType { StateEventType::RoomAliases } - - fn from_parts(event_type: &str, content: &RawJsonValue) -> serde_json::Result { - if event_type != "m.room.aliases" { - return Err(::serde::de::Error::custom(format!( - "expected event type `m.room.aliases`, found `{event_type}`", - ))); - } - - serde_json::from_str(content.get()) - } } impl StateEventContent for RedactedRoomAliasesEventContent { diff --git a/crates/ruma-common/src/events/room/member.rs b/crates/ruma-common/src/events/room/member.rs index b1f2c6ac..33569452 100644 --- a/crates/ruma-common/src/events/room/member.rs +++ b/crates/ruma-common/src/events/room/member.rs @@ -7,7 +7,6 @@ use std::collections::BTreeMap; use js_int::Int; use ruma_macros::EventContent; use serde::{Deserialize, Serialize}; -use serde_json::value::RawValue as RawJsonValue; use crate::{ events::{ @@ -243,16 +242,6 @@ impl EventContent for RedactedRoomMemberEventContent { fn event_type(&self) -> StateEventType { StateEventType::RoomMember } - - fn from_parts(event_type: &str, content: &RawJsonValue) -> serde_json::Result { - if event_type != "m.room.member" { - return Err(::serde::de::Error::custom(format!( - "expected event type `m.room.member`, found `{event_type}`", - ))); - } - - serde_json::from_str(content.get()) - } } impl StateEventContent for RedactedRoomMemberEventContent { diff --git a/crates/ruma-common/src/events/secret_storage/key.rs b/crates/ruma-common/src/events/secret_storage/key.rs index 61131aa3..88a933d2 100644 --- a/crates/ruma-common/src/events/secret_storage/key.rs +++ b/crates/ruma-common/src/events/secret_storage/key.rs @@ -141,11 +141,8 @@ mod tests { })) .unwrap(); - let content = ::from_parts( - "m.secret_storage.key.test", - &json, - ) - .unwrap(); + let content = + SecretStorageKeyEventContent::from_parts("m.secret_storage.key.test", &json).unwrap(); assert_eq!(content.name.unwrap(), "my_key"); assert_matches!(content.passphrase, None); @@ -169,11 +166,8 @@ mod tests { })) .unwrap(); - let content = ::from_parts( - "m.secret_storage.key.test", - &json, - ) - .unwrap(); + let content = + SecretStorageKeyEventContent::from_parts("m.secret_storage.key.test", &json).unwrap(); assert!(content.name.is_none()); assert_matches!(content.passphrase, None); @@ -233,11 +227,8 @@ mod tests { })) .unwrap(); - let content = ::from_parts( - "m.secret_storage.key.test", - &json, - ) - .unwrap(); + let content = + SecretStorageKeyEventContent::from_parts("m.secret_storage.key.test", &json).unwrap(); assert_eq!(content.name.unwrap(), "my_key"); let passphrase = content.passphrase.unwrap(); diff --git a/crates/ruma-common/tests/events/redacted.rs b/crates/ruma-common/tests/events/redacted.rs index 20fa4533..19df30cd 100644 --- a/crates/ruma-common/tests/events/redacted.rs +++ b/crates/ruma-common/tests/events/redacted.rs @@ -8,8 +8,8 @@ use ruma_common::{ redaction::RoomRedactionEventContent, }, AnyMessageLikeEvent, AnySyncMessageLikeEvent, AnySyncStateEvent, AnySyncTimelineEvent, - AnyTimelineEvent, EventContent, MessageLikeEvent, RedactContent, SyncMessageLikeEvent, - SyncStateEvent, + AnyTimelineEvent, EventContentFromType, MessageLikeEvent, RedactContent, + SyncMessageLikeEvent, SyncStateEvent, }, RoomVersionId, }; diff --git a/crates/ruma-macros/src/events/event.rs b/crates/ruma-macros/src/events/event.rs index 696cda96..d999ebc1 100644 --- a/crates/ruma-macros/src/events/event.rs +++ b/crates/ruma-macros/src/events/event.rs @@ -2,7 +2,7 @@ use proc_macro2::{Span, TokenStream}; use quote::quote; -use syn::{Data, DataStruct, DeriveInput, Field, Fields, FieldsNamed}; +use syn::{parse_quote, Data, DataStruct, DeriveInput, Field, Fields, FieldsNamed}; use super::{ event_parse::{to_kind_variation, EventKind, EventKindVariation}, @@ -109,31 +109,13 @@ fn expand_deserialize_event( .iter() .map(|field| { let name = field.ident.as_ref().unwrap(); - Ok(if name == "content" { - if is_generic && var.is_redacted() { - quote! { - let content = { - let json = content.ok_or_else( - || #serde::de::Error::missing_field("content"), - )?; - C::from_parts(&event_type, &json) - .map_err(#serde::de::Error::custom)? - }; - } - } else if is_generic { - quote! { - let content = { - let json = content - .ok_or_else(|| #serde::de::Error::missing_field("content"))?; - C::from_parts(&event_type, &json).map_err(#serde::de::Error::custom)? - }; - } - } else { - quote! { - let content = content.ok_or_else( - || #serde::de::Error::missing_field("content"), - )?; - } + Ok(if name == "content" && is_generic { + quote! { + let content = { + let json = content + .ok_or_else(|| #serde::de::Error::missing_field("content"))?; + C::from_parts(&event_type, &json).map_err(#serde::de::Error::custom)? + }; } } else if name == "unsigned" && !var.is_redacted() { quote! { @@ -170,6 +152,17 @@ fn expand_deserialize_event( } else { quote! {} }; + let where_clause = if is_generic { + let predicate = parse_quote! { C: #ruma_common::events::EventContentFromType }; + if let Some(mut where_clause) = where_clause.cloned() { + where_clause.predicates.push(predicate); + Some(where_clause) + } else { + Some(parse_quote! { where #predicate }) + } + } else { + where_clause.cloned() + }; Ok(quote! { #[automatically_derived] diff --git a/crates/ruma-macros/src/events/event_content.rs b/crates/ruma-macros/src/events/event_content.rs index 28e996ac..583c05d3 100644 --- a/crates/ruma-macros/src/events/event_content.rs +++ b/crates/ruma-macros/src/events/event_content.rs @@ -894,27 +894,6 @@ fn generate_event_content_impl<'a>( let event_types = aliases.iter().chain([event_type]); - let from_parts_fn_impl = if type_suffix_data.is_some() { - quote! { - ::from_parts(ev_type, content) - } - } else { - let event_types = event_types.clone(); - let event_types = quote! { - [#(#event_types,)*] - }; - - quote! { - if !#event_types.contains(&ev_type) { - return ::std::result::Result::Err(#serde::de::Error::custom( - ::std::format!("expected event type as one of `{:?}`, found `{}`", #event_types, ev_type) - )); - } - - #serde_json::from_str(content.get()) - } - }; - let event_content_from_type_impl = type_suffix_data.map(|(_, type_fragment_field)| { let type_prefixes = event_types.map(|ev_type| { ev_type @@ -971,13 +950,6 @@ fn generate_event_content_impl<'a>( fn event_type(&self) -> Self::EventType { #event_type_fn_impl } - - fn from_parts( - ev_type: &::std::primitive::str, - content: &#serde_json::value::RawValue, - ) -> #serde_json::Result { - #from_parts_fn_impl - } } #event_content_from_type_impl diff --git a/crates/ruma-macros/src/events/event_enum.rs b/crates/ruma-macros/src/events/event_enum.rs index a52c06b4..28337eb8 100644 --- a/crates/ruma-macros/src/events/event_enum.rs +++ b/crates/ruma-macros/src/events/event_enum.rs @@ -1,6 +1,6 @@ //! Implementation of event enum and event content enum macros. -use std::{fmt, iter::zip}; +use std::fmt; use proc_macro2::{Span, TokenStream}; use quote::{format_ident, quote, IdentFragment, ToTokens}; @@ -307,7 +307,6 @@ fn expand_content_enum( ruma_common: &TokenStream, ) -> syn::Result { let serde = quote! { #ruma_common::exports::serde }; - let serde_json = quote! { #ruma_common::exports::serde_json }; let ident = kind.to_content_enum(); @@ -320,36 +319,6 @@ fn expand_content_enum( Ok(to_event_content_path(kind, stable_name, &event.ev_path, None)) }) .collect::>()?; - let event_type_match_arms: TokenStream = zip(zip(events, variants), &content) - .map(|((event, variant), ev_content)| { - let variant_attrs = { - let attrs = &variant.attrs; - quote! { #(#attrs)* } - }; - let variant_ctor = variant.ctor(quote! { Self }); - - let ev_types = event.aliases.iter().chain([&event.ev_type]); - let ev_types = if event.ev_type.value().ends_with(".*") { - let ev_types = ev_types.map(|ev_type| { - ev_type - .value() - .strip_suffix(".*") - .expect("aliases have already been checked to have the same suffix") - .to_owned() - }); - quote! { _s if #(_s.starts_with(#ev_types))||* } - } else { - quote! { #(#ev_types)|* } - }; - - Ok(quote! { - #variant_attrs #ev_types => { - let content = #ev_content::from_parts(event_type, input)?; - ::std::result::Result::Ok(#variant_ctor(content)) - }, - }) - }) - .collect::>()?; let variant_decls = variants.iter().map(|v| v.decl()).collect::>(); let variant_arms = variants.iter().map(|v| v.match_arm(quote! { Self })).collect::>(); @@ -394,20 +363,6 @@ fn expand_content_enum( Self::_Custom { event_type } => ::std::convert::From::from(&event_type.0[..]), } } - - fn from_parts( - event_type: &::std::primitive::str, - input: &#serde_json::value::RawValue, - ) -> #serde_json::Result { - match event_type { - #event_type_match_arms - ty => { - ::std::result::Result::Ok(Self::_Custom { - event_type: crate::PrivOwnedStr(ty.into()), - }) - } - } - } } #[automatically_derived]