From 0faa00ca95f48c6c2fbda0488f06be9598abfee5 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 7 Mar 2022 15:17:28 +0100 Subject: [PATCH] macros: Move additional utility function into util module --- crates/ruma-macros/src/events/event_enum.rs | 24 ++------------------- crates/ruma-macros/src/util.rs | 23 +++++++++++++++++++- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/crates/ruma-macros/src/events/event_enum.rs b/crates/ruma-macros/src/events/event_enum.rs index b8593e6a..c662e87a 100644 --- a/crates/ruma-macros/src/events/event_enum.rs +++ b/crates/ruma-macros/src/events/event_enum.rs @@ -8,6 +8,7 @@ use super::{ event_parse::{EventEnumDecl, EventEnumEntry, EventKind, EventKindVariation}, util::{has_prev_content_field, EVENT_FIELDS}, }; +use crate::util::m_prefix_name_to_type_name; /// Create a content enum from `EventEnumInput`. pub fn expand_event_enums(input: &EventEnumDecl) -> syn::Result { @@ -574,27 +575,6 @@ fn to_event_content_path( } } -/// Splits the given `event_type` string on `.` and `_` removing the `m.room.` then -/// camel casing to give the `Event` struct name. -fn to_camel_case(name: &LitStr) -> syn::Result { - let span = name.span(); - let name = name.value(); - - if &name[..2] != "m." { - return Err(syn::Error::new( - span, - format!("well-known matrix events have to start with `m.` found `{}`", name), - )); - } - - let s: String = name[2..] - .split(&['.', '_'] as &[char]) - .map(|s| s.chars().next().unwrap().to_uppercase().to_string() + &s[1..]) - .collect(); - - Ok(Ident::new(&s, span)) -} - fn field_return_type( name: &str, var: EventKindVariation, @@ -657,7 +637,7 @@ impl EventEnumVariant { impl EventEnumEntry { pub(crate) fn to_variant(&self) -> syn::Result { let attrs = self.attrs.clone(); - let ident = to_camel_case(&self.ev_type)?; + let ident = m_prefix_name_to_type_name(&self.ev_type)?; Ok(EventEnumVariant { attrs, ident }) } } diff --git a/crates/ruma-macros/src/util.rs b/crates/ruma-macros/src/util.rs index 1d3a4bba..e7005a69 100644 --- a/crates/ruma-macros/src/util.rs +++ b/crates/ruma-macros/src/util.rs @@ -1,7 +1,7 @@ use proc_macro2::TokenStream; use proc_macro_crate::{crate_name, FoundCrate}; use quote::{format_ident, quote}; -use syn::Ident; +use syn::{Ident, LitStr}; pub(crate) fn import_ruma_common() -> TokenStream { if let Ok(FoundCrate::Name(name)) = crate_name("ruma-common") { @@ -32,3 +32,24 @@ pub(crate) fn to_camel_case(name: &Ident) -> Ident { .collect(); Ident::new(&s, span) } + +/// Splits the given string on `.` and `_` removing the `m.` then camel casing to give a Rust type +/// name. +pub(crate) fn m_prefix_name_to_type_name(name: &LitStr) -> syn::Result { + let span = name.span(); + let name = name.value(); + + if &name[..2] != "m." { + return Err(syn::Error::new( + span, + format!("well-known matrix events have to start with `m.` found `{}`", name), + )); + } + + let s: String = name[2..] + .split(&['.', '_'] as &[char]) + .map(|s| s.chars().next().unwrap().to_uppercase().to_string() + &s[1..]) + .collect(); + + Ok(Ident::new(&s, span)) +}