Rename ruma_content_collection to ruma_content_enum

This commit is contained in:
Jonas Platte 2020-06-07 15:54:23 +02:00
parent f531dce754
commit aa7a54015c
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
4 changed files with 24 additions and 23 deletions

View File

@ -1,10 +1,10 @@
//! Implementation of the collection type macro.
//! Implementation of the content_enum type macro.
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Ident, LitStr};
use parse::RumaCollectionInput;
use parse::ContentEnumInput;
fn marker_traits(ident: &Ident) -> TokenStream {
match ident.to_string().as_str() {
@ -20,8 +20,8 @@ fn marker_traits(ident: &Ident) -> TokenStream {
}
}
/// Create a collection from `RumaCollectionInput.
pub fn expand_collection(input: RumaCollectionInput) -> syn::Result<TokenStream> {
/// Create a content enum from `ContentEnumInput`.
pub fn expand_content_enum(input: ContentEnumInput) -> syn::Result<TokenStream> {
let attrs = &input.attrs;
let ident = &input.name;
let event_type_str = &input.events;
@ -33,7 +33,7 @@ pub fn expand_collection(input: RumaCollectionInput) -> syn::Result<TokenStream>
.map(to_event_content_path)
.collect::<Vec<_>>();
let collection = quote! {
let content_enum = quote! {
#( #attrs )*
#[derive(Clone, Debug, ::serde::Serialize)]
#[serde(untagged)]
@ -71,7 +71,7 @@ pub fn expand_collection(input: RumaCollectionInput) -> syn::Result<TokenStream>
let marker_trait_impls = marker_traits(ident);
Ok(quote! {
#collection
#content_enum
#event_content_impl
@ -115,21 +115,21 @@ pub(crate) fn to_camel_case(name: &LitStr) -> Ident {
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 {
use syn::{
parse::{self, Parse, ParseStream},
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 {
syn::custom_keyword!(name);
syn::custom_keyword!(events);
}
/// The entire `event_content_collection!` macro structure directly as it appears in the source code..
pub struct RumaCollectionInput {
/// The entire `event_content_content_enum!` macro structure directly as it appears in the source code..
pub struct ContentEnumInput {
/// Outer attributes on the field, such as a docstring.
pub attrs: Vec<Attribute>,
@ -143,13 +143,13 @@ pub mod parse {
pub events: Vec<LitStr>,
}
impl Parse for RumaCollectionInput {
impl Parse for ContentEnumInput {
fn parse(input: ParseStream<'_>) -> parse::Result<Self> {
let attrs = input.call(Attribute::parse_outer)?;
// name field
input.parse::<kw::name>()?;
input.parse::<Token![:]>()?;
// the name of our collection enum
// the name of our content_enum enum
let name: Ident = input.parse()?;
input.parse::<Token![,]>()?;

View File

@ -15,7 +15,7 @@ use quote::ToTokens;
use syn::{parse_macro_input, DeriveInput};
use self::{
collection::{expand_collection, parse::RumaCollectionInput},
content_enum::{expand_content_enum, parse::ContentEnumInput},
event::expand_event,
event_content::{expand_message_event_content, expand_state_event_content},
from_raw::expand_from_raw,
@ -23,7 +23,7 @@ use self::{
parse::RumaEventInput,
};
mod collection;
mod content_enum;
mod event;
mod event_content;
mod from_raw;
@ -123,14 +123,15 @@ pub fn ruma_event(input: TokenStream) -> TokenStream {
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
#[proc_macro]
pub fn event_content_collection(input: TokenStream) -> TokenStream {
let ruma_collection_input = syn::parse_macro_input!(input as RumaCollectionInput);
expand_collection(ruma_collection_input)
pub fn event_content_enum(input: TokenStream) -> TokenStream {
let content_enum_input = syn::parse_macro_input!(input as ContentEnumInput);
expand_content_enum(content_enum_input)
.unwrap_or_else(|err| err.to_compile_error())
.into()
}

View File

@ -14,9 +14,9 @@ use serde::{
};
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.
name: AnyMessageEventContent,
events: [

View File

@ -14,9 +14,9 @@ use serde::{
};
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.
name: AnyStateEventContent,
events: [