From 675e6f43cda2f882de010f538024d84f9918d3d5 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 14 Dec 2022 12:37:15 +0100 Subject: [PATCH] events: Remove the Redact trait It has not proven to be useful, being used by no downstream project. --- crates/ruma-common/src/events.rs | 15 ---- crates/ruma-common/src/events/enums.rs | 35 +------- crates/ruma-common/src/events/kinds.rs | 28 ++---- .../ruma-common/src/events/room/redaction.rs | 63 +------------- crates/ruma-macros/src/events/event.rs | 86 +------------------ crates/ruma-macros/src/events/event_enum.rs | 44 ---------- crates/ruma-macros/src/events/event_parse.rs | 8 -- 7 files changed, 12 insertions(+), 267 deletions(-) diff --git a/crates/ruma-common/src/events.rs b/crates/ruma-common/src/events.rs index a3b20341..e84b523d 100644 --- a/crates/ruma-common/src/events.rs +++ b/crates/ruma-common/src/events.rs @@ -104,7 +104,6 @@ use serde::{de::IgnoredAny, Deserialize, Serializer}; -use self::room::redaction::SyncRoomRedactionEvent; use crate::{EventEncryptionAlgorithm, RoomVersionId}; // Needs to be public for trybuild tests @@ -177,18 +176,6 @@ pub use self::{ unsigned::{MessageLikeUnsigned, RedactedUnsigned, StateUnsigned, StateUnsignedFromParts}, }; -/// Trait to define the behavior of redacting an event. -pub trait Redact { - /// The redacted form of the event. - type Redacted; - - /// Transforms `self` into a redacted form (removing most fields) according to the spec. - /// - /// A small number of events have room-version specific redaction behavior, so a version has to - /// be specified. - fn redact(self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) -> Self::Redacted; -} - /// Trait to define the behavior of redact an event's content object. pub trait RedactContent { /// The redacted form of the event's content. @@ -198,8 +185,6 @@ pub trait RedactContent { /// /// A small number of events have room-version specific redaction behavior, so a version has to /// be specified. - /// - /// Where applicable, it is preferred to use [`Redact::redact`] on the outer event. fn redact(self, version: &RoomVersionId) -> Self::Redacted; } diff --git a/crates/ruma-common/src/events/enums.rs b/crates/ruma-common/src/events/enums.rs index f465106d..0d82416f 100644 --- a/crates/ruma-common/src/events/enums.rs +++ b/crates/ruma-common/src/events/enums.rs @@ -2,13 +2,10 @@ use ruma_macros::{event_enum, EventEnumFromEvent}; use serde::{de, Deserialize}; use serde_json::value::RawValue as RawJsonValue; -use super::{ - room::{encrypted, redaction::SyncRoomRedactionEvent}, - BundledRelations, Redact, -}; +use super::{room::encrypted, BundledRelations}; use crate::{ serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId, - RoomVersionId, TransactionId, UserId, + TransactionId, UserId, }; event_enum! { @@ -272,34 +269,6 @@ impl<'de> Deserialize<'de> for AnySyncTimelineEvent { } } -impl Redact for AnyTimelineEvent { - type Redacted = Self; - - /// Redacts `self`, referencing the given event in `unsigned.redacted_because`. - /// - /// Does nothing for events that are already redacted. - fn redact(self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) -> Self { - match self { - Self::MessageLike(ev) => Self::MessageLike(ev.redact(redaction, version)), - Self::State(ev) => Self::State(ev.redact(redaction, version)), - } - } -} - -impl Redact for AnySyncTimelineEvent { - type Redacted = Self; - - /// Redacts `self`, referencing the given event in `unsigned.redacted_because`. - /// - /// Does nothing for events that are already redacted. - fn redact(self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) -> Self { - match self { - Self::MessageLike(ev) => Self::MessageLike(ev.redact(redaction, version)), - Self::State(ev) => Self::State(ev.redact(redaction, version)), - } - } -} - impl AnyMessageLikeEventContent { /// Get a copy of the event's `m.relates_to` field, if any. /// diff --git a/crates/ruma-common/src/events/kinds.rs b/crates/ruma-common/src/events/kinds.rs index 01cc8d13..bb0f39d7 100644 --- a/crates/ruma-common/src/events/kinds.rs +++ b/crates/ruma-common/src/events/kinds.rs @@ -5,15 +5,15 @@ use serde::{Deserialize, Deserializer, Serialize}; use serde_json::value::RawValue as RawJsonValue; use super::{ - room::redaction::SyncRoomRedactionEvent, EphemeralRoomEventContent, EventContent, - GlobalAccountDataEventContent, MessageLikeEventContent, MessageLikeEventType, - MessageLikeUnsigned, Redact, RedactContent, RedactedMessageLikeEventContent, - RedactedStateEventContent, RedactedUnsigned, RedactionDeHelper, RoomAccountDataEventContent, - StateEventContent, StateEventType, ToDeviceEventContent, + EphemeralRoomEventContent, EventContent, GlobalAccountDataEventContent, + MessageLikeEventContent, MessageLikeEventType, MessageLikeUnsigned, RedactContent, + RedactedMessageLikeEventContent, RedactedStateEventContent, RedactedUnsigned, + RedactionDeHelper, RoomAccountDataEventContent, StateEventContent, StateEventType, + ToDeviceEventContent, }; use crate::{ serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, - OwnedUserId, RoomId, RoomVersionId, UserId, + OwnedUserId, RoomId, UserId, }; /// A global account data event. @@ -456,22 +456,6 @@ macro_rules! impl_possibly_redacted_event { $($extra)* } - impl Redact for $ty - where - C: $content_trait + RedactContent, - C::Redacted: $redacted_content_trait, - $( C::Redacted: $trait, )? - { - type Redacted = Self; - - fn redact(self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) -> Self { - match self { - Self::Original(ev) => Self::Redacted(ev.redact(redaction, version)), - Self::Redacted(ev) => Self::Redacted(ev), - } - } - } - impl<'de, C> Deserialize<'de> for $ty where C: $content_trait + RedactContent, diff --git a/crates/ruma-common/src/events/room/redaction.rs b/crates/ruma-common/src/events/room/redaction.rs index 0905ec9d..3b39b49d 100644 --- a/crates/ruma-common/src/events/room/redaction.rs +++ b/crates/ruma-common/src/events/room/redaction.rs @@ -8,8 +8,8 @@ use serde_json::value::RawValue as RawJsonValue; use crate::{ events::{ - EventContent, MessageLikeEventType, MessageLikeUnsigned, Redact, RedactContent, - RedactedUnsigned, RedactionDeHelper, + EventContent, MessageLikeEventType, MessageLikeUnsigned, RedactedUnsigned, + RedactionDeHelper, }, serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UserId, @@ -65,25 +65,6 @@ pub struct OriginalRoomRedactionEvent { pub unsigned: MessageLikeUnsigned, } -impl Redact for OriginalRoomRedactionEvent { - type Redacted = RedactedRoomRedactionEvent; - - fn redact( - self, - redaction: SyncRoomRedactionEvent, - version: &crate::RoomVersionId, - ) -> Self::Redacted { - RedactedRoomRedactionEvent { - content: self.content.redact(version), - event_id: self.event_id, - sender: self.sender, - origin_server_ts: self.origin_server_ts, - room_id: self.room_id, - unsigned: RedactedUnsigned::new_because(Box::new(redaction)), - } - } -} - /// Redacted redaction event. #[derive(Clone, Debug, Event)] #[allow(clippy::exhaustive_structs)] @@ -130,24 +111,6 @@ pub struct OriginalSyncRoomRedactionEvent { pub unsigned: MessageLikeUnsigned, } -impl Redact for OriginalSyncRoomRedactionEvent { - type Redacted = RedactedSyncRoomRedactionEvent; - - fn redact( - self, - redaction: SyncRoomRedactionEvent, - version: &crate::RoomVersionId, - ) -> Self::Redacted { - RedactedSyncRoomRedactionEvent { - content: self.content.redact(version), - event_id: self.event_id, - sender: self.sender, - origin_server_ts: self.origin_server_ts, - unsigned: RedactedUnsigned::new_because(Box::new(redaction)), - } - } -} - /// Redacted redaction event without a `room_id`. #[derive(Clone, Debug, Event)] #[allow(clippy::exhaustive_structs)] @@ -240,17 +203,6 @@ impl RoomRedactionEvent { } } -impl Redact for RoomRedactionEvent { - type Redacted = Self; - - fn redact(self, redaction: SyncRoomRedactionEvent, version: &crate::RoomVersionId) -> Self { - match self { - Self::Original(ev) => Self::Redacted(ev.redact(redaction, version)), - Self::Redacted(ev) => Self::Redacted(ev), - } - } -} - impl<'de> Deserialize<'de> for RoomRedactionEvent { fn deserialize(deserializer: D) -> Result where @@ -326,17 +278,6 @@ impl From for SyncRoomRedactionEvent { } } -impl Redact for SyncRoomRedactionEvent { - type Redacted = Self; - - fn redact(self, redaction: SyncRoomRedactionEvent, version: &crate::RoomVersionId) -> Self { - match self { - Self::Original(ev) => Self::Redacted(ev.redact(redaction, version)), - Self::Redacted(ev) => Self::Redacted(ev), - } - } -} - impl<'de> Deserialize<'de> for SyncRoomRedactionEvent { fn deserialize(deserializer: D) -> Result where diff --git a/crates/ruma-macros/src/events/event.rs b/crates/ruma-macros/src/events/event.rs index 98e89d7c..569a33bb 100644 --- a/crates/ruma-macros/src/events/event.rs +++ b/crates/ruma-macros/src/events/event.rs @@ -1,8 +1,8 @@ //! Implementation of the top level `*Event` derive macro. use proc_macro2::{Span, TokenStream}; -use quote::{format_ident, quote}; -use syn::{parse_quote, Data, DataStruct, DeriveInput, Field, Fields, FieldsNamed, GenericParam}; +use quote::quote; +use syn::{Data, DataStruct, DeriveInput, Field, Fields, FieldsNamed}; use super::{ event_parse::{to_kind_variation, EventKind, EventKindVariation}, @@ -51,12 +51,6 @@ pub fn expand_event(input: DeriveInput) -> syn::Result { res.extend(expand_sync_from_into_full(&input, kind, var, &fields, &ruma_common)); } - if matches!(kind, EventKind::MessageLike | EventKind::State) - && matches!(var, EventKindVariation::Original | EventKindVariation::OriginalSync) - { - res.extend(expand_redact_event(&input, kind, var, &fields, &ruma_common)); - } - if is_non_stripped_room_event(kind, var) { res.extend(expand_eq_ord_event(&input)); } @@ -338,82 +332,6 @@ fn expand_deserialize_event( }) } -fn expand_redact_event( - input: &DeriveInput, - kind: EventKind, - var: EventKindVariation, - fields: &[Field], - ruma_common: &TokenStream, -) -> syn::Result { - let redacted_type = kind.to_event_ident(var.to_redacted())?; - let ident = &input.ident; - - let mut generics = input.generics.clone(); - if generics.params.is_empty() { - return Ok(TokenStream::new()); - } - - assert_eq!(generics.params.len(), 1, "expected one generic parameter"); - let ty_param = match &generics.params[0] { - GenericParam::Type(ty) => ty.ident.clone(), - _ => panic!("expected a type parameter"), - }; - - let where_clause = generics.make_where_clause(); - where_clause.predicates.push(parse_quote! { #ty_param: #ruma_common::events::RedactContent }); - - let assoc_type_bounds = - (kind == EventKind::State).then(|| quote! { StateKey = #ty_param::StateKey }); - let trait_name = format_ident!("Redacted{kind}Content"); - let redacted_event_content_bound = quote! { - #ruma_common::events::#trait_name<#assoc_type_bounds> - }; - where_clause.predicates.push(parse_quote! { - <#ty_param as #ruma_common::events::RedactContent>::Redacted: #redacted_event_content_bound - }); - - let (impl_generics, ty_gen, where_clause) = generics.split_for_impl(); - - let fields = fields.iter().filter_map(|field| { - let ident = field.ident.as_ref().unwrap(); - - if ident == "content" || ident == "prev_content" { - None - } else if ident == "unsigned" { - Some(quote! { - unsigned: #ruma_common::events::RedactedUnsigned::new_because( - ::std::boxed::Box::new(redaction), - ) - }) - } else { - Some(quote! { - #ident: self.#ident - }) - } - }); - - Ok(quote! { - #[automatically_derived] - impl #impl_generics #ruma_common::events::Redact for #ident #ty_gen #where_clause { - type Redacted = #ruma_common::events::#redacted_type< - <#ty_param as #ruma_common::events::RedactContent>::Redacted, - >; - - fn redact( - self, - redaction: #ruma_common::events::room::redaction::SyncRoomRedactionEvent, - version: &#ruma_common::RoomVersionId, - ) -> Self::Redacted { - let content = #ruma_common::events::RedactContent::redact(self.content, version); - #ruma_common::events::#redacted_type { - content, - #(#fields),* - } - } - } - }) -} - fn expand_sync_from_into_full( input: &DeriveInput, kind: EventKind, diff --git a/crates/ruma-macros/src/events/event_enum.rs b/crates/ruma-macros/src/events/event_enum.rs index 11d5fc58..be91e378 100644 --- a/crates/ruma-macros/src/events/event_enum.rs +++ b/crates/ruma-macros/src/events/event_enum.rs @@ -69,14 +69,6 @@ pub fn expand_event_enums(input: &EventEnumDecl) -> syn::Result { expand_event_enum(kind, V::Sync, events, docs, attrs, variants, ruma_common) .unwrap_or_else(syn::Error::into_compile_error), ); - res.extend( - expand_redact(kind, V::None, variants, ruma_common) - .unwrap_or_else(syn::Error::into_compile_error), - ); - res.extend( - expand_redact(kind, V::Sync, variants, ruma_common) - .unwrap_or_else(syn::Error::into_compile_error), - ); res.extend( expand_from_full_event(kind, V::None, variants) .unwrap_or_else(syn::Error::into_compile_error), @@ -427,42 +419,6 @@ fn expand_content_enum( }) } -fn expand_redact( - kind: EventKind, - var: EventEnumVariation, - variants: &[EventEnumVariant], - ruma_common: &TokenStream, -) -> syn::Result { - let ident = kind.to_event_enum_ident(var.into())?; - - let self_variants = variants.iter().map(|v| v.match_arm(quote! { Self })); - let redacted_variants = variants.iter().map(|v| v.ctor(&ident)); - - Ok(quote! { - #[automatically_derived] - impl #ruma_common::events::Redact for #ident { - type Redacted = Self; - - fn redact( - self, - redaction: #ruma_common::events::room::redaction::SyncRoomRedactionEvent, - version: &#ruma_common::RoomVersionId, - ) -> Self { - match self { - #( - #self_variants(event) => #redacted_variants( - #ruma_common::events::Redact::redact(event, redaction, version), - ), - )* - Self::_Custom(event) => Self::_Custom( - #ruma_common::events::Redact::redact(event, redaction, version), - ) - } - } - } - }) -} - fn expand_accessor_methods( kind: EventKind, var: EventEnumVariation, diff --git a/crates/ruma-macros/src/events/event_parse.rs b/crates/ruma-macros/src/events/event_parse.rs index 43cba615..b8e382d0 100644 --- a/crates/ruma-macros/src/events/event_parse.rs +++ b/crates/ruma-macros/src/events/event_parse.rs @@ -55,14 +55,6 @@ impl EventKindVariation { matches!(self, Self::OriginalSync | Self::RedactedSync) } - pub fn to_redacted(self) -> Self { - match self { - EventKindVariation::Original => EventKindVariation::Redacted, - EventKindVariation::OriginalSync => EventKindVariation::RedactedSync, - _ => panic!("No redacted form of {self:?}"), - } - } - pub fn to_full(self) -> Self { match self { EventKindVariation::OriginalSync => EventKindVariation::Original,