macros: Improve error handling
This commit is contained in:
		
							parent
							
								
									a50b2d313a
								
							
						
					
					
						commit
						634f93c985
					
				| @ -364,14 +364,14 @@ fn expand_redact_event( | |||||||
|     var: EventKindVariation, |     var: EventKindVariation, | ||||||
|     fields: &[Field], |     fields: &[Field], | ||||||
|     ruma_common: &TokenStream, |     ruma_common: &TokenStream, | ||||||
| ) -> TokenStream { | ) -> syn::Result<TokenStream> { | ||||||
|     let redacted_type = kind.to_event_ident(var.to_redacted()); |     let redacted_type = kind.to_event_ident(var.to_redacted())?; | ||||||
|     let redacted_event_type_enum = kind.to_event_type_enum(); |     let redacted_event_type_enum = kind.to_event_type_enum(); | ||||||
|     let ident = &input.ident; |     let ident = &input.ident; | ||||||
| 
 | 
 | ||||||
|     let mut generics = input.generics.clone(); |     let mut generics = input.generics.clone(); | ||||||
|     if generics.params.is_empty() { |     if generics.params.is_empty() { | ||||||
|         return TokenStream::new(); |         return Ok(TokenStream::new()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     assert_eq!(generics.params.len(), 1, "expected one generic parameter"); |     assert_eq!(generics.params.len(), 1, "expected one generic parameter"); | ||||||
| @ -410,7 +410,7 @@ fn expand_redact_event( | |||||||
|         } |         } | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     quote! { |     Ok(quote! { | ||||||
|         #[automatically_derived] |         #[automatically_derived] | ||||||
|         impl #impl_generics #ruma_common::events::Redact for #ident #ty_gen #where_clause { |         impl #impl_generics #ruma_common::events::Redact for #ident #ty_gen #where_clause { | ||||||
|             type Redacted = #ruma_common::events::#redacted_type< |             type Redacted = #ruma_common::events::#redacted_type< | ||||||
| @ -429,7 +429,7 @@ fn expand_redact_event( | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     }) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn expand_sync_from_into_full( | fn expand_sync_from_into_full( | ||||||
| @ -438,13 +438,13 @@ fn expand_sync_from_into_full( | |||||||
|     var: EventKindVariation, |     var: EventKindVariation, | ||||||
|     fields: &[Field], |     fields: &[Field], | ||||||
|     ruma_common: &TokenStream, |     ruma_common: &TokenStream, | ||||||
| ) -> TokenStream { | ) -> syn::Result<TokenStream> { | ||||||
|     let ident = &input.ident; |     let ident = &input.ident; | ||||||
|     let full_struct = kind.to_event_ident(var.to_full()); |     let full_struct = kind.to_event_ident(var.to_full())?; | ||||||
|     let (impl_generics, ty_gen, where_clause) = input.generics.split_for_impl(); |     let (impl_generics, ty_gen, where_clause) = input.generics.split_for_impl(); | ||||||
|     let fields: Vec<_> = fields.iter().flat_map(|f| &f.ident).collect(); |     let fields: Vec<_> = fields.iter().flat_map(|f| &f.ident).collect(); | ||||||
| 
 | 
 | ||||||
|     quote! { |     Ok(quote! { | ||||||
|         #[automatically_derived] |         #[automatically_derived] | ||||||
|         impl #impl_generics ::std::convert::From<#full_struct #ty_gen> |         impl #impl_generics ::std::convert::From<#full_struct #ty_gen> | ||||||
|             for #ident #ty_gen #where_clause |             for #ident #ty_gen #where_clause | ||||||
| @ -469,7 +469,7 @@ fn expand_sync_from_into_full( | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     }) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn expand_eq_ord_event(input: &DeriveInput) -> TokenStream { | fn expand_eq_ord_event(input: &DeriveInput) -> TokenStream { | ||||||
|  | |||||||
| @ -379,7 +379,7 @@ fn generate_event_type_aliases( | |||||||
|         EventKindVariation::RedactedSync, |         EventKindVariation::RedactedSync, | ||||||
|     ] |     ] | ||||||
|     .iter() |     .iter() | ||||||
|     .filter_map(|&var| Some((var, event_kind.try_to_event_ident(var)?))) |     .filter_map(|&var| Some((var, event_kind.to_event_ident(var).ok()?))) | ||||||
|     .map(|(var, ev_struct)| { |     .map(|(var, ev_struct)| { | ||||||
|         let ev_type = format_ident!("{}{}", var, ev_type_s); |         let ev_type = format_ident!("{}{}", var, ev_type_s); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -83,9 +83,9 @@ fn expand_event_enum( | |||||||
|     attrs: &[Attribute], |     attrs: &[Attribute], | ||||||
|     variants: &[EventEnumVariant], |     variants: &[EventEnumVariant], | ||||||
|     ruma_common: &TokenStream, |     ruma_common: &TokenStream, | ||||||
| ) -> TokenStream { | ) -> syn::Result<TokenStream> { | ||||||
|     let event_struct = kind.to_event_ident(var); |     let event_struct = kind.to_event_ident(var)?; | ||||||
|     let ident = kind.to_event_enum_ident(var); |     let ident = kind.to_event_enum_ident(var)?; | ||||||
| 
 | 
 | ||||||
|     let variant_decls = variants.iter().map(|v| v.decl()); |     let variant_decls = variants.iter().map(|v| v.decl()); | ||||||
|     let content: Vec<_> = |     let content: Vec<_> = | ||||||
| @ -93,11 +93,11 @@ fn expand_event_enum( | |||||||
| 
 | 
 | ||||||
|     let custom_ty = format_ident!("Custom{}Content", kind); |     let custom_ty = format_ident!("Custom{}Content", kind); | ||||||
| 
 | 
 | ||||||
|     let deserialize_impl = expand_deserialize_impl(kind, var, events, variants, ruma_common); |     let deserialize_impl = expand_deserialize_impl(kind, var, events, variants, ruma_common)?; | ||||||
|     let field_accessor_impl = expand_accessor_methods(kind, var, variants, ruma_common); |     let field_accessor_impl = expand_accessor_methods(kind, var, variants, ruma_common)?; | ||||||
|     let from_impl = expand_from_impl(&ident, &content, variants); |     let from_impl = expand_from_impl(&ident, &content, variants); | ||||||
| 
 | 
 | ||||||
|     quote! { |     Ok(quote! { | ||||||
|         #( #attrs )* |         #( #attrs )* | ||||||
|         #[derive(Clone, Debug)] |         #[derive(Clone, Debug)] | ||||||
|         #[allow(clippy::large_enum_variant)] |         #[allow(clippy::large_enum_variant)] | ||||||
| @ -117,7 +117,7 @@ fn expand_event_enum( | |||||||
|         #deserialize_impl |         #deserialize_impl | ||||||
|         #field_accessor_impl |         #field_accessor_impl | ||||||
|         #from_impl |         #from_impl | ||||||
|     } |     }) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn expand_deserialize_impl( | fn expand_deserialize_impl( | ||||||
| @ -126,11 +126,11 @@ fn expand_deserialize_impl( | |||||||
|     events: &[LitStr], |     events: &[LitStr], | ||||||
|     variants: &[EventEnumVariant], |     variants: &[EventEnumVariant], | ||||||
|     ruma_common: &TokenStream, |     ruma_common: &TokenStream, | ||||||
| ) -> TokenStream { | ) -> syn::Result<TokenStream> { | ||||||
|     let serde = quote! { #ruma_common::exports::serde }; |     let serde = quote! { #ruma_common::exports::serde }; | ||||||
|     let serde_json = quote! { #ruma_common::exports::serde_json }; |     let serde_json = quote! { #ruma_common::exports::serde_json }; | ||||||
| 
 | 
 | ||||||
|     let ident = kind.to_event_enum_ident(var); |     let ident = kind.to_event_enum_ident(var)?; | ||||||
| 
 | 
 | ||||||
|     let variant_attrs = variants.iter().map(|v| { |     let variant_attrs = variants.iter().map(|v| { | ||||||
|         let attrs = &v.attrs; |         let attrs = &v.attrs; | ||||||
| @ -139,7 +139,7 @@ fn expand_deserialize_impl( | |||||||
|     let self_variants = variants.iter().map(|v| v.ctor(quote! { Self })); |     let self_variants = variants.iter().map(|v| v.ctor(quote! { Self })); | ||||||
|     let content = events.iter().map(|event| to_event_path(event, kind, var, ruma_common)); |     let content = events.iter().map(|event| to_event_path(event, kind, var, ruma_common)); | ||||||
| 
 | 
 | ||||||
|     quote! { |     Ok(quote! { | ||||||
|         impl<'de> #serde::de::Deserialize<'de> for #ident { |         impl<'de> #serde::de::Deserialize<'de> for #ident { | ||||||
|             fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |             fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||||||
|             where |             where | ||||||
| @ -166,7 +166,7 @@ fn expand_deserialize_impl( | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     }) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn expand_from_impl( | fn expand_from_impl( | ||||||
| @ -196,14 +196,14 @@ fn expand_from_full_event( | |||||||
|     kind: EventKind, |     kind: EventKind, | ||||||
|     var: EventKindVariation, |     var: EventKindVariation, | ||||||
|     variants: &[EventEnumVariant], |     variants: &[EventEnumVariant], | ||||||
| ) -> TokenStream { | ) -> syn::Result<TokenStream> { | ||||||
|     let ident = kind.to_event_enum_ident(var); |     let ident = kind.to_event_enum_ident(var)?; | ||||||
|     let sync = kind.to_event_enum_ident(var.to_sync()); |     let sync = kind.to_event_enum_ident(var.to_sync())?; | ||||||
| 
 | 
 | ||||||
|     let ident_variants = variants.iter().map(|v| v.match_arm(&ident)); |     let ident_variants = variants.iter().map(|v| v.match_arm(&ident)); | ||||||
|     let self_variants = variants.iter().map(|v| v.ctor(quote! { Self })); |     let self_variants = variants.iter().map(|v| v.ctor(quote! { Self })); | ||||||
| 
 | 
 | ||||||
|     quote! { |     Ok(quote! { | ||||||
|         #[automatically_derived] |         #[automatically_derived] | ||||||
|         impl ::std::convert::From<#ident> for #sync { |         impl ::std::convert::From<#ident> for #sync { | ||||||
|             fn from(event: #ident) -> Self { |             fn from(event: #ident) -> Self { | ||||||
| @ -219,7 +219,7 @@ fn expand_from_full_event( | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     }) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn expand_into_full_event( | fn expand_into_full_event( | ||||||
| @ -227,14 +227,14 @@ fn expand_into_full_event( | |||||||
|     var: EventKindVariation, |     var: EventKindVariation, | ||||||
|     variants: &[EventEnumVariant], |     variants: &[EventEnumVariant], | ||||||
|     ruma_common: &TokenStream, |     ruma_common: &TokenStream, | ||||||
| ) -> TokenStream { | ) -> syn::Result<TokenStream> { | ||||||
|     let ident = kind.to_event_enum_ident(var); |     let ident = kind.to_event_enum_ident(var)?; | ||||||
|     let full = kind.to_event_enum_ident(var.to_full()); |     let full = kind.to_event_enum_ident(var.to_full())?; | ||||||
| 
 | 
 | ||||||
|     let self_variants = variants.iter().map(|v| v.match_arm(quote! { Self })); |     let self_variants = variants.iter().map(|v| v.match_arm(quote! { Self })); | ||||||
|     let full_variants = variants.iter().map(|v| v.ctor(&full)); |     let full_variants = variants.iter().map(|v| v.ctor(&full)); | ||||||
| 
 | 
 | ||||||
|     quote! { |     Ok(quote! { | ||||||
|         #[automatically_derived] |         #[automatically_derived] | ||||||
|         impl #ident { |         impl #ident { | ||||||
|             /// Convert this sync event into a full event (one with a `room_id` field).
 |             /// Convert this sync event into a full event (one with a `room_id` field).
 | ||||||
| @ -254,7 +254,7 @@ fn expand_into_full_event( | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     }) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /// Create a content enum from `EventEnumInput`.
 | /// Create a content enum from `EventEnumInput`.
 | ||||||
| @ -353,14 +353,14 @@ fn expand_redact( | |||||||
|     var: EventKindVariation, |     var: EventKindVariation, | ||||||
|     variants: &[EventEnumVariant], |     variants: &[EventEnumVariant], | ||||||
|     ruma_common: &TokenStream, |     ruma_common: &TokenStream, | ||||||
| ) -> TokenStream { | ) -> syn::Result<TokenStream> { | ||||||
|     let ident = kind.to_event_enum_ident(var); |     let ident = kind.to_event_enum_ident(var)?; | ||||||
|     let redacted_enum = kind.to_event_enum_ident(var.to_redacted()); |     let redacted_enum = kind.to_event_enum_ident(var.to_redacted())?; | ||||||
| 
 | 
 | ||||||
|     let self_variants = variants.iter().map(|v| v.match_arm(quote! { Self })); |     let self_variants = variants.iter().map(|v| v.match_arm(quote! { Self })); | ||||||
|     let redacted_variants = variants.iter().map(|v| v.ctor(&redacted_enum)); |     let redacted_variants = variants.iter().map(|v| v.ctor(&redacted_enum)); | ||||||
| 
 | 
 | ||||||
|     quote! { |     Ok(quote! { | ||||||
|         #[automatically_derived] |         #[automatically_derived] | ||||||
|         impl #ruma_common::events::Redact for #ident { |         impl #ruma_common::events::Redact for #ident { | ||||||
|             type Redacted = #redacted_enum; |             type Redacted = #redacted_enum; | ||||||
| @ -382,22 +382,22 @@ fn expand_redact( | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     }) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn expand_possibly_redacted_enum( | fn expand_possibly_redacted_enum( | ||||||
|     kind: EventKind, |     kind: EventKind, | ||||||
|     var: EventKindVariation, |     var: EventKindVariation, | ||||||
|     ruma_common: &TokenStream, |     ruma_common: &TokenStream, | ||||||
| ) -> TokenStream { | ) -> syn::Result<TokenStream> { | ||||||
|     let serde = quote! { #ruma_common::exports::serde }; |     let serde = quote! { #ruma_common::exports::serde }; | ||||||
|     let serde_json = quote! { #ruma_common::exports::serde_json }; |     let serde_json = quote! { #ruma_common::exports::serde_json }; | ||||||
| 
 | 
 | ||||||
|     let ident = format_ident!("AnyPossiblyRedacted{}", kind.to_event_ident(var)); |     let ident = format_ident!("AnyPossiblyRedacted{}", kind.to_event_ident(var)?); | ||||||
|     let regular_enum_ident = kind.to_event_enum_ident(var); |     let regular_enum_ident = kind.to_event_enum_ident(var)?; | ||||||
|     let redacted_enum_ident = kind.to_event_enum_ident(var.to_redacted()); |     let redacted_enum_ident = kind.to_event_enum_ident(var.to_redacted())?; | ||||||
| 
 | 
 | ||||||
|     quote! { |     Ok(quote! { | ||||||
|         /// An enum that holds either regular un-redacted events or redacted events.
 |         /// An enum that holds either regular un-redacted events or redacted events.
 | ||||||
|         #[derive(Clone, Debug)] |         #[derive(Clone, Debug)] | ||||||
|         #[allow(clippy::exhaustive_enums)] |         #[allow(clippy::exhaustive_enums)] | ||||||
| @ -426,7 +426,7 @@ fn expand_possibly_redacted_enum( | |||||||
|                 }) |                 }) | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     }) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn expand_accessor_methods( | fn expand_accessor_methods( | ||||||
| @ -434,8 +434,8 @@ fn expand_accessor_methods( | |||||||
|     var: EventKindVariation, |     var: EventKindVariation, | ||||||
|     variants: &[EventEnumVariant], |     variants: &[EventEnumVariant], | ||||||
|     ruma_common: &TokenStream, |     ruma_common: &TokenStream, | ||||||
| ) -> TokenStream { | ) -> syn::Result<TokenStream> { | ||||||
|     let ident = kind.to_event_enum_ident(var); |     let ident = kind.to_event_enum_ident(var)?; | ||||||
|     let event_type_enum = format_ident!("{}Type", kind); |     let event_type_enum = format_ident!("{}Type", kind); | ||||||
|     let self_variants: Vec<_> = variants.iter().map(|v| v.match_arm(quote! { Self })).collect(); |     let self_variants: Vec<_> = variants.iter().map(|v| v.match_arm(quote! { Self })).collect(); | ||||||
| 
 | 
 | ||||||
| @ -526,7 +526,7 @@ fn expand_accessor_methods( | |||||||
|         }) |         }) | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     quote! { |     Ok(quote! { | ||||||
|         #[automatically_derived] |         #[automatically_derived] | ||||||
|         impl #ident { |         impl #ident { | ||||||
|             /// Returns the `type` of this event.
 |             /// Returns the `type` of this event.
 | ||||||
| @ -544,7 +544,7 @@ fn expand_accessor_methods( | |||||||
| 
 | 
 | ||||||
|             #( #methods )* |             #( #methods )* | ||||||
|         } |         } | ||||||
|     } |     }) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn to_event_path( | fn to_event_path( | ||||||
|  | |||||||
| @ -2,6 +2,7 @@ | |||||||
| 
 | 
 | ||||||
| use std::fmt; | use std::fmt; | ||||||
| 
 | 
 | ||||||
|  | use proc_macro2::Span; | ||||||
| use quote::{format_ident, IdentFragment}; | use quote::{format_ident, IdentFragment}; | ||||||
| use syn::{ | use syn::{ | ||||||
|     braced, |     braced, | ||||||
| @ -122,7 +123,7 @@ impl EventKind { | |||||||
|         matches!(self, Self::GlobalAccountData | Self::RoomAccountData) |         matches!(self, Self::GlobalAccountData | Self::RoomAccountData) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn try_to_event_ident(self, var: EventKindVariation) -> Option<Ident> { |     pub fn to_event_ident(self, var: EventKindVariation) -> syn::Result<Ident> { | ||||||
|         use EventKindVariation as V; |         use EventKindVariation as V; | ||||||
| 
 | 
 | ||||||
|         match (self, var) { |         match (self, var) { | ||||||
| @ -132,19 +133,19 @@ impl EventKind { | |||||||
|                 Self::MessageLike | Self::RoomRedaction | Self::State, |                 Self::MessageLike | Self::RoomRedaction | Self::State, | ||||||
|                 V::Redacted | V::RedactedSync, |                 V::Redacted | V::RedactedSync, | ||||||
|             ) |             ) | ||||||
|             | (Self::State, V::Stripped | V::Initial) => Some(format_ident!("{}{}", var, self)), |             | (Self::State, V::Stripped | V::Initial) => Ok(format_ident!("{}{}", var, self)), | ||||||
|             _ => None, |             _ => Err(syn::Error::new( | ||||||
|  |                 Span::call_site(), | ||||||
|  |                 format!( | ||||||
|  |                     "({:?}, {:?}) is not a valid event kind / variation combination", | ||||||
|  |                     self, var | ||||||
|  |                 ), | ||||||
|  |             )), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn to_event_ident(self, var: EventKindVariation) -> Ident { |     pub fn to_event_enum_ident(self, var: EventKindVariation) -> syn::Result<Ident> { | ||||||
|         self.try_to_event_ident(var).unwrap_or_else(|| { |         Ok(format_ident!("Any{}", self.to_event_ident(var)?)) | ||||||
|             panic!("({:?}, {:?}) is not a valid event kind / variation combination", self, var); |  | ||||||
|         }) |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     pub fn to_event_enum_ident(self, var: EventKindVariation) -> Ident { |  | ||||||
|         format_ident!("Any{}", self.to_event_ident(var)) |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn to_event_type_enum(self) -> Ident { |     pub fn to_event_type_enum(self) -> Ident { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user