events: Don't store extra fields for unknown events in content enums

This commit is contained in:
Jonas Platte 2021-08-22 13:54:20 +02:00
parent 1c361c2895
commit 77cf085ab9
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 19 additions and 23 deletions

View File

@ -396,16 +396,15 @@ fn expand_content_enum(
#( #attrs )* #( #attrs )*
#[derive(Clone, Debug, #serde::Serialize)] #[derive(Clone, Debug, #serde::Serialize)]
#[serde(untagged)] #[serde(untagged)]
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant, clippy::manual_non_exhaustive)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub enum #ident { pub enum #ident {
#( #(
#[doc = #event_type_str] #[doc = #event_type_str]
#variant_decls(#content), #variant_decls(#content),
)* )*
/// Content of an event not defined by the Matrix specification.
#[doc(hidden)] #[doc(hidden)]
_Custom(#ruma_events::custom::CustomEventContent), _Custom { event_type: ::std::string::String },
} }
}; };
@ -422,7 +421,7 @@ fn expand_content_enum(
fn event_type(&self) -> &::std::primitive::str { fn event_type(&self) -> &::std::primitive::str {
match self { match self {
#( #variant_arms(content) => content.event_type(), )* #( #variant_arms(content) => content.event_type(), )*
Self::_Custom(content) => content.event_type(), Self::_Custom { event_type } => &event_type,
} }
} }
@ -435,13 +434,11 @@ fn expand_content_enum(
#variant_attrs #event_type_str => { #variant_attrs #event_type_str => {
let content = #content::from_parts(event_type, input)?; let content = #content::from_parts(event_type, input)?;
::std::result::Result::Ok(#variant_ctors(content)) ::std::result::Result::Ok(#variant_ctors(content))
}, }
)* )*
ev_type => { ty => {
let content = ::std::result::Result::Ok(Self::_Custom { event_type: ty.to_owned() })
#ruma_events::custom::CustomEventContent::from_parts(ev_type, input)?; }
::std::result::Result::Ok(Self::_Custom(content))
},
} }
} }
} }
@ -461,16 +458,15 @@ fn expand_content_enum(
#( #attrs )* #( #attrs )*
#[derive(Clone, Debug, #serde::Serialize)] #[derive(Clone, Debug, #serde::Serialize)]
#[serde(untagged)] #[serde(untagged)]
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant, clippy::manual_non_exhaustive)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub enum #redacted_ident { pub enum #redacted_ident {
#( #(
#[doc = #event_type_str] #[doc = #event_type_str]
#variant_decls(#redacted_content), #variant_decls(#redacted_content),
)* )*
/// Content of a redacted event not defined by the Matrix specification.
#[doc(hidden)] #[doc(hidden)]
_Custom(#ruma_events::custom::RedactedCustomEventContent), _Custom { event_type: ::std::string::String },
} }
impl #ruma_events::RedactContent for #ident { impl #ruma_events::RedactContent for #ident {
@ -485,15 +481,11 @@ fn expand_content_enum(
#( #(
#variant_arms(content) => { #variant_arms(content) => {
#redaction_variants( #redaction_variants(
#ruma_events::RedactContent::redact(content, version) #ruma_events::RedactContent::redact(content, version),
) )
}, }
)* )*
Self::_Custom(content) => { Self::_Custom { event_type } => #redacted_ident::_Custom { event_type },
#redacted_ident::_Custom(
#ruma_events::RedactContent::redact(content, version)
)
},
} }
} }
} }
@ -776,7 +768,9 @@ fn accessor_methods(
pub fn content(&self) -> #content_enum { pub fn content(&self) -> #content_enum {
match self { match self {
#( #self_variants(event) => #content_variants(event.content.clone()), )* #( #self_variants(event) => #content_variants(event.content.clone()), )*
Self::_Custom(event) => #content_enum::_Custom(event.content.clone()), Self::_Custom(event) => #content_enum::_Custom {
event_type: #ruma_events::EventContent::event_type(&event.content).to_owned(),
},
} }
} }
}; };
@ -792,7 +786,9 @@ fn accessor_methods(
}, },
)* )*
Self::_Custom(event) => { Self::_Custom(event) => {
event.prev_content.as_ref().map(|c| #content_enum::_Custom(c.clone())) event.prev_content.as_ref().map(|c| #content_enum::_Custom {
event_type: #ruma_events::EventContent::event_type(c).to_owned(),
})
}, },
} }
} }

View File

@ -370,7 +370,7 @@ impl AnyMessageEventContent {
| AnyMessageEventContent::RoomMessageFeedback(_) | AnyMessageEventContent::RoomMessageFeedback(_)
| AnyMessageEventContent::RoomRedaction(_) | AnyMessageEventContent::RoomRedaction(_)
| AnyMessageEventContent::Sticker(_) | AnyMessageEventContent::Sticker(_)
| AnyMessageEventContent::_Custom(_) => None, | AnyMessageEventContent::_Custom { .. } => None,
} }
} }
} }