macros: Remove some unnecessary short-circuiting in event proc-macros

This commit is contained in:
Jonas Platte 2022-04-01 14:32:06 +02:00 committed by Jonas Platte
parent 634f93c985
commit 1415e32a1d
5 changed files with 118 additions and 52 deletions

View File

@ -45,7 +45,10 @@ pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
let mut res = TokenStream::new();
res.extend(expand_serialize_event(&input, var, &fields, &ruma_common));
res.extend(expand_deserialize_event(&input, kind, var, &fields, &ruma_common)?);
res.extend(
expand_deserialize_event(&input, kind, var, &fields, &ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
if var.is_sync() {
res.extend(expand_sync_from_into_full(&input, kind, var, &fields, &ruma_common));

View File

@ -184,25 +184,20 @@ pub fn expand_event_content(
}
// We only generate redacted content structs for state and message-like events
let redacted_event_content = needs_redacted(&content_attr, event_kind)
.then(|| {
generate_redacted_event_content(
ident,
fields.clone(),
event_type,
event_kind,
ruma_common,
)
})
.transpose()?;
let redacted_event_content = needs_redacted(&content_attr, event_kind).then(|| {
generate_redacted_event_content(ident, fields.clone(), event_type, event_kind, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error)
});
let event_content_impl =
generate_event_content_impl(ident, fields, event_type, event_kind, ruma_common)?;
generate_event_content_impl(ident, fields, event_type, event_kind, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error);
let static_event_content_impl = event_kind
.map(|k| generate_static_event_content_impl(ident, k, false, event_type, ruma_common));
let type_aliases = event_kind
.map(|k| generate_event_type_aliases(k, ident, &event_type.value(), ruma_common))
.transpose()?;
let type_aliases = event_kind.map(|k| {
generate_event_type_aliases(k, ident, &event_type.value(), ruma_common)
.unwrap_or_else(syn::Error::into_compile_error)
});
Ok(quote! {
#redacted_event_content
@ -301,7 +296,8 @@ fn generate_redacted_event_content<'a>(
event_type,
event_kind,
ruma_common,
)?;
)
.unwrap_or_else(syn::Error::into_compile_error);
let static_event_content_impl = event_kind.map(|k| {
generate_static_event_content_impl(&redacted_ident, k, true, event_type, ruma_common)

View File

@ -48,29 +48,71 @@ pub fn expand_event_enums(input: &EventEnumDecl) -> syn::Result<TokenStream> {
let variants = &variants;
let ruma_common = &ruma_common;
res.extend(expand_event_enum(kind, V::Full, events, attrs, variants, ruma_common));
res.extend(
expand_event_enum(kind, V::Full, events, attrs, variants, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(expand_content_enum(kind, events, attrs, variants, ruma_common));
if matches!(kind, EventKind::Ephemeral | EventKind::MessageLike | EventKind::State) {
res.extend(expand_event_enum(kind, V::Sync, events, attrs, variants, ruma_common));
res.extend(expand_from_full_event(kind, V::Full, variants));
res.extend(expand_into_full_event(kind, V::Sync, variants, ruma_common));
res.extend(
expand_event_enum(kind, V::Sync, events, attrs, variants, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
expand_from_full_event(kind, V::Full, variants)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
expand_into_full_event(kind, V::Sync, variants, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
}
if matches!(kind, EventKind::State) {
res.extend(expand_event_enum(kind, V::Stripped, events, attrs, variants, ruma_common));
res.extend(expand_event_enum(kind, V::Initial, events, attrs, variants, ruma_common));
res.extend(
expand_event_enum(kind, V::Stripped, events, attrs, variants, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
expand_event_enum(kind, V::Initial, events, attrs, variants, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
}
if matches!(kind, EventKind::MessageLike | EventKind::State) {
res.extend(expand_event_enum(kind, V::Redacted, events, attrs, variants, ruma_common));
res.extend(expand_event_enum(kind, V::RedactedSync, events, attrs, variants, ruma_common));
res.extend(expand_redact(kind, V::Full, variants, ruma_common));
res.extend(expand_redact(kind, V::Sync, variants, ruma_common));
res.extend(expand_possibly_redacted_enum(kind, V::Full, ruma_common));
res.extend(expand_possibly_redacted_enum(kind, V::Sync, ruma_common));
res.extend(expand_from_full_event(kind, V::Redacted, variants));
res.extend(expand_into_full_event(kind, V::RedactedSync, variants, ruma_common));
res.extend(
expand_event_enum(kind, V::Redacted, events, attrs, variants, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
expand_event_enum(kind, V::RedactedSync, events, attrs, variants, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
expand_redact(kind, V::Full, 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_possibly_redacted_enum(kind, V::Full, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
expand_possibly_redacted_enum(kind, V::Sync, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
expand_from_full_event(kind, V::Redacted, variants)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
expand_into_full_event(kind, V::RedactedSync, variants, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
}
Ok(res)

View File

@ -44,14 +44,38 @@ pub fn expand_event_type_enum(
let mut res = TokenStream::new();
res.extend(generate_enum("EventType", &all, &ruma_common)?);
res.extend(generate_enum("RoomEventType", &room, &ruma_common)?);
res.extend(generate_enum("StateEventType", &state, &ruma_common)?);
res.extend(generate_enum("MessageLikeEventType", &message, &ruma_common)?);
res.extend(generate_enum("EphemeralRoomEventType", &ephemeral, &ruma_common)?);
res.extend(generate_enum("RoomAccountDataEventType", &room_account, &ruma_common)?);
res.extend(generate_enum("GlobalAccountDataEventType", &global_account, &ruma_common)?);
res.extend(generate_enum("ToDeviceEventType", &to_device, &ruma_common)?);
res.extend(
generate_enum("EventType", &all, &ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
generate_enum("RoomEventType", &room, &ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
generate_enum("StateEventType", &state, &ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
generate_enum("MessageLikeEventType", &message, &ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
generate_enum("EphemeralRoomEventType", &ephemeral, &ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
generate_enum("RoomAccountDataEventType", &room_account, &ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
generate_enum("GlobalAccountDataEventType", &global_account, &ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
res.extend(
generate_enum("ToDeviceEventType", &to_device, &ruma_common)
.unwrap_or_else(syn::Error::into_compile_error),
);
Ok(res)
}

View File

@ -72,24 +72,25 @@ use self::{
//// supported: https://github.com/rust-lang/rust/issues/74563
#[proc_macro]
pub fn event_enum(input: TokenStream) -> TokenStream {
let event_enum_input = syn::parse_macro_input!(input as EventEnumInput);
let ruma_common = import_ruma_common();
let event_enum_input = syn::parse_macro_input!(input as EventEnumInput);
let enums = event_enum_input
.enums
.iter()
.map(expand_event_enums)
.collect::<syn::Result<pm2::TokenStream>>();
let event_types = expand_event_type_enum(event_enum_input, ruma_common);
event_types
.and_then(|types| {
enums.map(|mut enums| {
enums.extend(types);
enums
})
})
.unwrap_or_else(syn::Error::into_compile_error)
.into()
.map(|e| expand_event_enums(e).unwrap_or_else(syn::Error::into_compile_error))
.collect::<pm2::TokenStream>();
let event_types = expand_event_type_enum(event_enum_input, ruma_common)
.unwrap_or_else(syn::Error::into_compile_error);
let tokens = quote! {
#enums
#event_types
};
tokens.into()
}
/// Generates an implementation of `ruma_common::events::EventContent`.