macros: Move utility things into util module

This commit is contained in:
Jonas Platte 2022-03-04 17:37:06 +01:00
parent 1192bd1099
commit 493b9a08c7
No known key found for this signature in database
GPG Key ID: BBA95679259D342F
3 changed files with 45 additions and 40 deletions

View File

@ -3,15 +3,15 @@
use proc_macro2::{Span, TokenStream}; use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote}; use quote::{format_ident, quote};
use syn::{ use syn::{
parse_quote, Data, DataStruct, DeriveInput, Field, Fields, FieldsNamed, GenericParam, Ident, parse_quote, Data, DataStruct, DeriveInput, Field, Fields, FieldsNamed, GenericParam, Meta,
Meta, MetaList, NestedMeta, MetaList, NestedMeta,
}; };
use super::{ use super::{
event_parse::{to_kind_variation, EventKind, EventKindVariation}, event_parse::{to_kind_variation, EventKind, EventKindVariation},
util::is_non_stripped_room_event, util::is_non_stripped_room_event,
}; };
use crate::import_ruma_common; use crate::{import_ruma_common, util::to_camel_case};
/// Derive `Event` macro code generation. /// Derive `Event` macro code generation.
pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> { pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
@ -556,15 +556,3 @@ fn expand_eq_ord_event(input: &DeriveInput) -> TokenStream {
} }
} }
} }
/// CamelCase's a field ident like "foo_bar" to "FooBar".
fn to_camel_case(name: &Ident) -> Ident {
let span = name.span();
let name = name.to_string();
let s: String = name
.split('_')
.map(|s| s.chars().next().unwrap().to_uppercase().to_string() + &s[1..])
.collect();
Ident::new(&s, span)
}

View File

@ -8,14 +8,19 @@
use proc_macro::TokenStream; use proc_macro::TokenStream;
use proc_macro2 as pm2; use proc_macro2 as pm2;
use proc_macro_crate::{crate_name, FoundCrate}; use quote::quote;
use quote::{format_ident, quote};
use ruma_identifiers_validation::{ use ruma_identifiers_validation::{
device_key_id, event_id, key_id, mxc_uri, room_alias_id, room_id, room_version_id, server_name, device_key_id, event_id, key_id, mxc_uri, room_alias_id, room_id, room_version_id, server_name,
user_id, user_id,
}; };
use syn::{parse_macro_input, DeriveInput, ItemEnum}; use syn::{parse_macro_input, DeriveInput, ItemEnum};
mod api;
mod events;
mod identifiers;
mod serde;
mod util;
use self::{ use self::{
api::{request::expand_derive_request, response::expand_derive_response, Api}, api::{request::expand_derive_request, response::expand_derive_response, Api},
events::{ events::{
@ -36,13 +41,9 @@ use self::{
outgoing::expand_derive_outgoing, outgoing::expand_derive_outgoing,
serialize_as_ref_str::expand_serialize_as_ref_str, serialize_as_ref_str::expand_serialize_as_ref_str,
}, },
util::import_ruma_common,
}; };
mod api;
mod events;
mod identifiers;
mod serde;
/// Generates an enum to represent the various Matrix event types. /// Generates an enum to represent the various Matrix event types.
/// ///
/// This macro also implements the necessary traits for the type to serialize and deserialize /// This macro also implements the necessary traits for the type to serialize and deserialize
@ -107,24 +108,6 @@ pub fn derive_event(input: TokenStream) -> TokenStream {
expand_event(input).unwrap_or_else(syn::Error::into_compile_error).into() expand_event(input).unwrap_or_else(syn::Error::into_compile_error).into()
} }
pub(crate) fn import_ruma_common() -> pm2::TokenStream {
if let Ok(FoundCrate::Name(name)) = crate_name("ruma-common") {
let import = format_ident!("{}", name);
quote! { ::#import }
} else if let Ok(FoundCrate::Name(name)) = crate_name("ruma") {
let import = format_ident!("{}", name);
quote! { ::#import }
} else if let Ok(FoundCrate::Name(name)) = crate_name("matrix-sdk") {
let import = format_ident!("{}", name);
quote! { ::#import::ruma }
} else if let Ok(FoundCrate::Name(name)) = crate_name("matrix-sdk-appservice") {
let import = format_ident!("{}", name);
quote! { ::#import::ruma }
} else {
quote! { ::ruma_common }
}
}
/// Generates `From` implementations for event enums. /// Generates `From` implementations for event enums.
#[proc_macro_derive(EventEnumFromEvent)] #[proc_macro_derive(EventEnumFromEvent)]
pub fn derive_from_event_to_enum(input: TokenStream) -> TokenStream { pub fn derive_from_event_to_enum(input: TokenStream) -> TokenStream {

View File

@ -0,0 +1,34 @@
use proc_macro2::TokenStream;
use proc_macro_crate::{crate_name, FoundCrate};
use quote::{format_ident, quote};
use syn::Ident;
pub(crate) fn import_ruma_common() -> TokenStream {
if let Ok(FoundCrate::Name(name)) = crate_name("ruma-common") {
let import = format_ident!("{}", name);
quote! { ::#import }
} else if let Ok(FoundCrate::Name(name)) = crate_name("ruma") {
let import = format_ident!("{}", name);
quote! { ::#import }
} else if let Ok(FoundCrate::Name(name)) = crate_name("matrix-sdk") {
let import = format_ident!("{}", name);
quote! { ::#import::ruma }
} else if let Ok(FoundCrate::Name(name)) = crate_name("matrix-sdk-appservice") {
let import = format_ident!("{}", name);
quote! { ::#import::ruma }
} else {
quote! { ::ruma_common }
}
}
/// CamelCase's a field ident like "foo_bar" to "FooBar".
pub(crate) fn to_camel_case(name: &Ident) -> Ident {
let span = name.span();
let name = name.to_string();
let s: String = name
.split('_')
.map(|s| s.chars().next().unwrap().to_uppercase().to_string() + &s[1..])
.collect();
Ident::new(&s, span)
}