Rename ruma_content_collection to ruma_content_enum
This commit is contained in:
parent
f531dce754
commit
aa7a54015c
@ -1,10 +1,10 @@
|
|||||||
//! Implementation of the collection type macro.
|
//! Implementation of the content_enum type macro.
|
||||||
|
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::{Ident, LitStr};
|
use syn::{Ident, LitStr};
|
||||||
|
|
||||||
use parse::RumaCollectionInput;
|
use parse::ContentEnumInput;
|
||||||
|
|
||||||
fn marker_traits(ident: &Ident) -> TokenStream {
|
fn marker_traits(ident: &Ident) -> TokenStream {
|
||||||
match ident.to_string().as_str() {
|
match ident.to_string().as_str() {
|
||||||
@ -20,8 +20,8 @@ fn marker_traits(ident: &Ident) -> TokenStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a collection from `RumaCollectionInput.
|
/// Create a content enum from `ContentEnumInput`.
|
||||||
pub fn expand_collection(input: RumaCollectionInput) -> syn::Result<TokenStream> {
|
pub fn expand_content_enum(input: ContentEnumInput) -> syn::Result<TokenStream> {
|
||||||
let attrs = &input.attrs;
|
let attrs = &input.attrs;
|
||||||
let ident = &input.name;
|
let ident = &input.name;
|
||||||
let event_type_str = &input.events;
|
let event_type_str = &input.events;
|
||||||
@ -33,7 +33,7 @@ pub fn expand_collection(input: RumaCollectionInput) -> syn::Result<TokenStream>
|
|||||||
.map(to_event_content_path)
|
.map(to_event_content_path)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let collection = quote! {
|
let content_enum = quote! {
|
||||||
#( #attrs )*
|
#( #attrs )*
|
||||||
#[derive(Clone, Debug, ::serde::Serialize)]
|
#[derive(Clone, Debug, ::serde::Serialize)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
@ -71,7 +71,7 @@ pub fn expand_collection(input: RumaCollectionInput) -> syn::Result<TokenStream>
|
|||||||
let marker_trait_impls = marker_traits(ident);
|
let marker_trait_impls = marker_traits(ident);
|
||||||
|
|
||||||
Ok(quote! {
|
Ok(quote! {
|
||||||
#collection
|
#content_enum
|
||||||
|
|
||||||
#event_content_impl
|
#event_content_impl
|
||||||
|
|
||||||
@ -115,21 +115,21 @@ pub(crate) fn to_camel_case(name: &LitStr) -> Ident {
|
|||||||
Ident::new(&s, span)
|
Ident::new(&s, span)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Details of parsing input for the `event_content_collection` procedural macro.
|
/// Details of parsing input for the `event_content_content_enum` procedural macro.
|
||||||
pub mod parse {
|
pub mod parse {
|
||||||
use syn::{
|
use syn::{
|
||||||
parse::{self, Parse, ParseStream},
|
parse::{self, Parse, ParseStream},
|
||||||
Attribute, Expr, ExprLit, Ident, Lit, LitStr, Token,
|
Attribute, Expr, ExprLit, Ident, Lit, LitStr, Token,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Custom keywords for the `event_content_collection!` macro
|
/// Custom keywords for the `event_content_content_enum!` macro
|
||||||
mod kw {
|
mod kw {
|
||||||
syn::custom_keyword!(name);
|
syn::custom_keyword!(name);
|
||||||
syn::custom_keyword!(events);
|
syn::custom_keyword!(events);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The entire `event_content_collection!` macro structure directly as it appears in the source code..
|
/// The entire `event_content_content_enum!` macro structure directly as it appears in the source code..
|
||||||
pub struct RumaCollectionInput {
|
pub struct ContentEnumInput {
|
||||||
/// Outer attributes on the field, such as a docstring.
|
/// Outer attributes on the field, such as a docstring.
|
||||||
pub attrs: Vec<Attribute>,
|
pub attrs: Vec<Attribute>,
|
||||||
|
|
||||||
@ -143,13 +143,13 @@ pub mod parse {
|
|||||||
pub events: Vec<LitStr>,
|
pub events: Vec<LitStr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for RumaCollectionInput {
|
impl Parse for ContentEnumInput {
|
||||||
fn parse(input: ParseStream<'_>) -> parse::Result<Self> {
|
fn parse(input: ParseStream<'_>) -> parse::Result<Self> {
|
||||||
let attrs = input.call(Attribute::parse_outer)?;
|
let attrs = input.call(Attribute::parse_outer)?;
|
||||||
// name field
|
// name field
|
||||||
input.parse::<kw::name>()?;
|
input.parse::<kw::name>()?;
|
||||||
input.parse::<Token![:]>()?;
|
input.parse::<Token![:]>()?;
|
||||||
// the name of our collection enum
|
// the name of our content_enum enum
|
||||||
let name: Ident = input.parse()?;
|
let name: Ident = input.parse()?;
|
||||||
input.parse::<Token![,]>()?;
|
input.parse::<Token![,]>()?;
|
||||||
|
|
@ -15,7 +15,7 @@ use quote::ToTokens;
|
|||||||
use syn::{parse_macro_input, DeriveInput};
|
use syn::{parse_macro_input, DeriveInput};
|
||||||
|
|
||||||
use self::{
|
use self::{
|
||||||
collection::{expand_collection, parse::RumaCollectionInput},
|
content_enum::{expand_content_enum, parse::ContentEnumInput},
|
||||||
event::expand_event,
|
event::expand_event,
|
||||||
event_content::{expand_message_event_content, expand_state_event_content},
|
event_content::{expand_message_event_content, expand_state_event_content},
|
||||||
from_raw::expand_from_raw,
|
from_raw::expand_from_raw,
|
||||||
@ -23,7 +23,7 @@ use self::{
|
|||||||
parse::RumaEventInput,
|
parse::RumaEventInput,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod collection;
|
mod content_enum;
|
||||||
mod event;
|
mod event;
|
||||||
mod event_content;
|
mod event_content;
|
||||||
mod from_raw;
|
mod from_raw;
|
||||||
@ -123,14 +123,15 @@ pub fn ruma_event(input: TokenStream) -> TokenStream {
|
|||||||
ruma_event.into_token_stream().into()
|
ruma_event.into_token_stream().into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a collection type to represent the various Matrix event types.
|
/// Generates a content enum to represent the various Matrix event types.
|
||||||
///
|
///
|
||||||
/// This macro also implements the necessary traits for the type to serialize and deserialize itself.
|
/// This macro also implements the necessary traits for the type to serialize and deserialize
|
||||||
|
/// itself.
|
||||||
// TODO more docs/example
|
// TODO more docs/example
|
||||||
#[proc_macro]
|
#[proc_macro]
|
||||||
pub fn event_content_collection(input: TokenStream) -> TokenStream {
|
pub fn event_content_enum(input: TokenStream) -> TokenStream {
|
||||||
let ruma_collection_input = syn::parse_macro_input!(input as RumaCollectionInput);
|
let content_enum_input = syn::parse_macro_input!(input as ContentEnumInput);
|
||||||
expand_collection(ruma_collection_input)
|
expand_content_enum(content_enum_input)
|
||||||
.unwrap_or_else(|err| err.to_compile_error())
|
.unwrap_or_else(|err| err.to_compile_error())
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
@ -14,9 +14,9 @@ use serde::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{MessageEventContent, RoomEventContent, UnsignedData};
|
use crate::{MessageEventContent, RoomEventContent, UnsignedData};
|
||||||
use ruma_events_macros::{event_content_collection, Event};
|
use ruma_events_macros::{event_content_enum, Event};
|
||||||
|
|
||||||
event_content_collection! {
|
event_content_enum! {
|
||||||
/// A message event.
|
/// A message event.
|
||||||
name: AnyMessageEventContent,
|
name: AnyMessageEventContent,
|
||||||
events: [
|
events: [
|
||||||
|
@ -14,9 +14,9 @@ use serde::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{RoomEventContent, StateEventContent, TryFromRaw, UnsignedData};
|
use crate::{RoomEventContent, StateEventContent, TryFromRaw, UnsignedData};
|
||||||
use ruma_events_macros::{event_content_collection, Event};
|
use ruma_events_macros::{event_content_enum, Event};
|
||||||
|
|
||||||
event_content_collection! {
|
event_content_enum! {
|
||||||
/// A state event.
|
/// A state event.
|
||||||
name: AnyStateEventContent,
|
name: AnyStateEventContent,
|
||||||
events: [
|
events: [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user