Move event content derives into one place and share code
This commit is contained in:
parent
cc3d0af40c
commit
0c0dafa2c5
@ -1,4 +1,4 @@
|
||||
//! Implementation of the `StateEventContent` derive macro
|
||||
//! Implementations of the MessageEventContent and StateEventContent derive macro.
|
||||
|
||||
use proc_macro2::{Span, TokenStream};
|
||||
use quote::quote;
|
||||
@ -11,7 +11,7 @@ use syn::{
|
||||
///
|
||||
/// `#[ruma_event(type = "m.room.alias")]`
|
||||
enum EventMeta {
|
||||
/// Variant holds the "m.room.whatever" event type.
|
||||
/// Variant holds the "m.whatever" event type.
|
||||
Type(LitStr),
|
||||
}
|
||||
|
||||
@ -23,8 +23,10 @@ impl Parse for EventMeta {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a `StateEventContent` implementation for a struct
|
||||
pub fn expand_state_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||
/// Create a `RoomEventContent` implementation for a struct.
|
||||
///
|
||||
/// This is used internally for code sharing as `RoomEventContent` is not derivable.
|
||||
fn expand_room_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||
let ident = &input.ident;
|
||||
|
||||
let event_type_attr = input
|
||||
@ -72,6 +74,28 @@ pub fn expand_state_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||
#event_content_impl
|
||||
|
||||
impl ::ruma_events::RoomEventContent for #ident { }
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a `MessageEventContent` implementation for a struct
|
||||
pub fn expand_message_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||
let ident = input.ident.clone();
|
||||
let room_ev_content = expand_room_event(input)?;
|
||||
|
||||
Ok(quote! {
|
||||
#room_ev_content
|
||||
|
||||
impl ::ruma_events::MessageEventContent for #ident { }
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a `MessageEventContent` implementation for a struct
|
||||
pub fn expand_state_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||
let ident = input.ident.clone();
|
||||
let room_ev_content = expand_room_event(input)?;
|
||||
|
||||
Ok(quote! {
|
||||
#room_ev_content
|
||||
|
||||
impl ::ruma_events::StateEventContent for #ident { }
|
||||
})
|
@ -37,13 +37,16 @@ use quote::ToTokens;
|
||||
use syn::{parse_macro_input, DeriveInput};
|
||||
|
||||
use self::{
|
||||
from_raw::expand_from_raw, gen::RumaEvent, parse::RumaEventInput, state::expand_state_event,
|
||||
event_content::{expand_message_event, expand_state_event},
|
||||
from_raw::expand_from_raw,
|
||||
gen::RumaEvent,
|
||||
parse::RumaEventInput,
|
||||
};
|
||||
|
||||
mod event_content;
|
||||
mod from_raw;
|
||||
mod gen;
|
||||
mod parse;
|
||||
mod state;
|
||||
|
||||
// A note about the `example` modules that appears in doctests:
|
||||
//
|
||||
@ -149,6 +152,15 @@ pub fn derive_from_raw(input: TokenStream) -> TokenStream {
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Generates an implementation of `ruma_events::MessageEventContent` and it's super traits.
|
||||
#[proc_macro_derive(MessageEventContent, attributes(ruma_event))]
|
||||
pub fn derive_message_event_content(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
expand_message_event(input)
|
||||
.unwrap_or_else(|err| err.to_compile_error())
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Generates an implementation of `ruma_events::StateEventContent` and it's super traits.
|
||||
#[proc_macro_derive(StateEventContent, attributes(ruma_event))]
|
||||
pub fn derive_state_event_content(input: TokenStream) -> TokenStream {
|
||||
|
Loading…
x
Reference in New Issue
Block a user