macros: Move additional utility function into util module
This commit is contained in:
		
							parent
							
								
									bb26f159e3
								
							
						
					
					
						commit
						0faa00ca95
					
				| @ -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<TokenStream> { | ||||
| @ -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<Ident> { | ||||
|     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<EventEnumVariant> { | ||||
|         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 }) | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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<Ident> { | ||||
|     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)) | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user