events: Refactor marker trait code generation
This commit is contained in:
parent
ab850d8237
commit
ed9138796a
@ -289,39 +289,26 @@ fn generate_event_content_derives(
|
||||
) -> syn::Result<TokenStream> {
|
||||
let msg = "valid event kinds are GlobalAccountData, RoomAccountData, \
|
||||
EphemeralRoom, Message, State, ToDevice";
|
||||
content_attr
|
||||
let marker_traits: Vec<_> = content_attr
|
||||
.iter()
|
||||
.map(|kind| {
|
||||
Ok(match kind {
|
||||
EventKind::GlobalAccountData => quote! {
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::GlobalAccountDataEventContent for #ident {}
|
||||
},
|
||||
EventKind::RoomAccountData => quote! {
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::RoomAccountDataEventContent for #ident {}
|
||||
},
|
||||
EventKind::Ephemeral => quote! {
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::EphemeralRoomEventContent for #ident {}
|
||||
},
|
||||
EventKind::Message => quote! {
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::MessageEventContent for #ident {}
|
||||
},
|
||||
EventKind::State => quote! {
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::StateEventContent for #ident {}
|
||||
},
|
||||
EventKind::ToDevice => quote! {
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::ToDeviceEventContent for #ident {}
|
||||
},
|
||||
EventKind::Redaction => return Err(syn::Error::new_spanned(ident, msg)),
|
||||
EventKind::Presence => return Err(syn::Error::new_spanned(ident, msg)),
|
||||
})
|
||||
.map(|kind| match kind {
|
||||
EventKind::GlobalAccountData => Ok(quote! { GlobalAccountDataEventContent }),
|
||||
EventKind::RoomAccountData => Ok(quote! { RoomAccountDataEventContent }),
|
||||
EventKind::Ephemeral => Ok(quote! { EphemeralRoomEventContent }),
|
||||
EventKind::Message => Ok(quote! { MessageEventContent }),
|
||||
EventKind::State => Ok(quote! { StateEventContent }),
|
||||
EventKind::ToDevice => Ok(quote! { ToDeviceEventContent }),
|
||||
EventKind::Redaction => Err(syn::Error::new_spanned(ident, msg)),
|
||||
EventKind::Presence => Err(syn::Error::new_spanned(ident, msg)),
|
||||
})
|
||||
.collect()
|
||||
.collect::<syn::Result<_>>()?;
|
||||
|
||||
Ok(quote! {
|
||||
#(
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::#marker_traits for #ident {}
|
||||
)*
|
||||
})
|
||||
}
|
||||
|
||||
fn generate_event_content_impl(
|
||||
|
@ -430,7 +430,7 @@ fn expand_content_enum(
|
||||
}
|
||||
};
|
||||
|
||||
let marker_trait_impls = marker_traits(kind, ruma_events);
|
||||
let marker_trait_impl = marker_trait(kind, ruma_events);
|
||||
|
||||
let redacted_content_enum = if kind.is_state() || kind.is_message() {
|
||||
let redacted_ident = kind.to_redacted_content_enum();
|
||||
@ -488,7 +488,7 @@ fn expand_content_enum(
|
||||
|
||||
#event_content_impl
|
||||
|
||||
#marker_trait_impls
|
||||
#marker_trait_impl
|
||||
|
||||
#redacted_content_enum
|
||||
}
|
||||
@ -708,34 +708,21 @@ fn generate_custom_variant(
|
||||
}
|
||||
}
|
||||
|
||||
fn marker_traits(kind: &EventKind, ruma_events: &TokenStream) -> TokenStream {
|
||||
fn marker_trait(kind: &EventKind, ruma_events: &TokenStream) -> TokenStream {
|
||||
let marker_trait = match kind {
|
||||
EventKind::State => quote! { StateEventContent },
|
||||
EventKind::Message => quote! { MessageEventContent },
|
||||
EventKind::Ephemeral => quote! { EphemeralRoomEventContent },
|
||||
EventKind::GlobalAccountData => quote! { GlobalAccountDataEventContent },
|
||||
EventKind::RoomAccountData => quote! { RoomAccountDataEventContent },
|
||||
EventKind::ToDevice => quote! { ToDeviceEventContent },
|
||||
_ => return TokenStream::new(),
|
||||
};
|
||||
|
||||
let ident = kind.to_content_enum();
|
||||
match kind {
|
||||
EventKind::State => quote! {
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::StateEventContent for #ident {}
|
||||
},
|
||||
EventKind::Message => quote! {
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::MessageEventContent for #ident {}
|
||||
},
|
||||
EventKind::Ephemeral => quote! {
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::EphemeralRoomEventContent for #ident {}
|
||||
},
|
||||
EventKind::GlobalAccountData => quote! {
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::GlobalAccountDataEventContent for #ident {}
|
||||
},
|
||||
EventKind::RoomAccountData => quote! {
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::RoomAccountDataEventContent for #ident {}
|
||||
},
|
||||
EventKind::ToDevice => quote! {
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::ToDeviceEventContent for #ident {}
|
||||
},
|
||||
_ => TokenStream::new(),
|
||||
quote! {
|
||||
#[automatically_derived]
|
||||
impl #ruma_events::#marker_trait for #ident {}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user