events-macros: Simplify redaction-related code

This commit is contained in:
Jonas Platte 2021-10-02 00:42:40 +02:00
parent ec92349496
commit 3b786d8f78
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 42 additions and 65 deletions

View File

@ -464,25 +464,10 @@ fn expand_redact(
) -> Option<TokenStream> { ) -> Option<TokenStream> {
let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers }; let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers };
if let EventKindVariation::Full | EventKindVariation::Sync | EventKindVariation::Stripped = var let redacted_var = var.to_redacted()?;
{ let struct_id = kind.to_event_ident(&redacted_var)?;
let (redacted_type, redacted_enum) = match var { let redacted_enum = kind.to_event_enum_ident(&redacted_var)?;
EventKindVariation::Full => { let redacted_type = quote! { #ruma_events::#struct_id };
let struct_id = kind.to_event_ident(&EventKindVariation::Redacted)?;
(
quote! { #ruma_events::#struct_id },
kind.to_event_enum_ident(&EventKindVariation::Redacted)?,
)
}
EventKindVariation::Sync => {
let struct_id = kind.to_event_ident(&EventKindVariation::RedactedSync)?;
(
quote! { #ruma_events::#struct_id },
kind.to_event_enum_ident(&EventKindVariation::RedactedSync)?,
)
}
_ => return None,
};
let self_variants = variants.iter().map(|v| v.match_arm(quote! { Self })); let self_variants = variants.iter().map(|v| v.match_arm(quote! { Self }));
let redaction_variants = variants.iter().map(|v| v.ctor(&redacted_enum)); let redaction_variants = variants.iter().map(|v| v.ctor(&redacted_enum));
@ -524,9 +509,6 @@ fn expand_redact(
} }
} }
}) })
} else {
None
}
} }
fn expand_redacted_enum( fn expand_redacted_enum(
@ -539,7 +521,8 @@ fn expand_redacted_enum(
if let EventKind::State | EventKind::Message = kind { if let EventKind::State | EventKind::Message = kind {
let ident = format_ident!("AnyPossiblyRedacted{}", kind.to_event_ident(var)?); let ident = format_ident!("AnyPossiblyRedacted{}", kind.to_event_ident(var)?);
let (regular_enum_ident, redacted_enum_ident) = inner_enum_idents(kind, var)?; let regular_enum_ident = kind.to_event_enum_ident(var)?;
let redacted_enum_ident = kind.to_event_enum_ident(&var.to_redacted()?)?;
Some(quote! { Some(quote! {
/// An enum that holds either regular un-redacted events or redacted events. /// An enum that holds either regular un-redacted events or redacted events.
@ -751,20 +734,6 @@ fn accessor_methods(
}) })
} }
fn inner_enum_idents(kind: &EventKind, var: &EventKindVariation) -> Option<(Ident, Ident)> {
Some(match var {
EventKindVariation::Full => (
kind.to_event_enum_ident(var)?,
kind.to_event_enum_ident(&EventKindVariation::Redacted)?,
),
EventKindVariation::Sync => (
kind.to_event_enum_ident(var)?,
kind.to_event_enum_ident(&EventKindVariation::RedactedSync)?,
),
_ => return None,
})
}
/// Redacted events do NOT generate `content` or `prev_content` methods like /// Redacted events do NOT generate `content` or `prev_content` methods like
/// un-redacted events; otherwise, they are the same. /// un-redacted events; otherwise, they are the same.
fn redacted_accessor_methods( fn redacted_accessor_methods(

View File

@ -45,6 +45,14 @@ impl EventKindVariation {
matches!(self, Self::Redacted | Self::RedactedSync) matches!(self, Self::Redacted | Self::RedactedSync)
} }
pub fn to_redacted(self) -> Option<Self> {
match self {
EventKindVariation::Full => Some(EventKindVariation::Redacted),
EventKindVariation::Sync => Some(EventKindVariation::RedactedSync),
_ => None,
}
}
pub fn to_sync(self) -> Option<Self> { pub fn to_sync(self) -> Option<Self> {
match self { match self {
EventKindVariation::Full => Some(EventKindVariation::Sync), EventKindVariation::Full => Some(EventKindVariation::Sync),