common: Create crate-level macro re-exports module
This commit is contained in:
		
							parent
							
								
									43c47ef676
								
							
						
					
					
						commit
						1192bd1099
					
				| @ -140,19 +140,6 @@ mod enums; | ||||
| mod event_kinds; | ||||
| mod unsigned; | ||||
| 
 | ||||
| /// Re-exports to allow users to declare their own event types using the
 | ||||
| /// macros used internally.
 | ||||
| ///
 | ||||
| /// It is not considered part of this module's public API.
 | ||||
| #[doc(hidden)] | ||||
| pub mod exports { | ||||
|     pub use crate as ruma_common; | ||||
|     pub use ruma_identifiers; | ||||
|     pub use ruma_serde; | ||||
|     pub use serde; | ||||
|     pub use serde_json; | ||||
| } | ||||
| 
 | ||||
| /// Re-export of all the derives needed to create your own event types.
 | ||||
| pub mod macros { | ||||
|     pub use ruma_macros::{Event, EventContent}; | ||||
|  | ||||
| @ -47,3 +47,14 @@ impl fmt::Debug for PrivOwnedStr { | ||||
|         self.0.fmt(f) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /// Re-exports used by macro-generated code.
 | ||||
| ///
 | ||||
| /// It is not considered part of this module's public API.
 | ||||
| #[doc(hidden)] | ||||
| pub mod exports { | ||||
|     pub use ruma_identifiers; | ||||
|     pub use ruma_serde; | ||||
|     pub use serde; | ||||
|     pub use serde_json; | ||||
| } | ||||
|  | ||||
| @ -11,11 +11,11 @@ use super::{ | ||||
|     event_parse::{to_kind_variation, EventKind, EventKindVariation}, | ||||
|     util::is_non_stripped_room_event, | ||||
| }; | ||||
| use crate::import_ruma_events; | ||||
| use crate::import_ruma_common; | ||||
| 
 | ||||
| /// Derive `Event` macro code generation.
 | ||||
| pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> { | ||||
|     let ruma_events = import_ruma_events(); | ||||
|     let ruma_common = import_ruma_common(); | ||||
| 
 | ||||
|     let ident = &input.ident; | ||||
|     let (kind, var) = to_kind_variation(ident).ok_or_else(|| { | ||||
| @ -44,17 +44,17 @@ pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> { | ||||
| 
 | ||||
|     let mut res = TokenStream::new(); | ||||
| 
 | ||||
|     res.extend(expand_serialize_event(&input, var, &fields, &ruma_events)); | ||||
|     res.extend(expand_deserialize_event(&input, kind, var, &fields, &ruma_events)?); | ||||
|     res.extend(expand_serialize_event(&input, var, &fields, &ruma_common)); | ||||
|     res.extend(expand_deserialize_event(&input, kind, var, &fields, &ruma_common)?); | ||||
| 
 | ||||
|     if var.is_sync() { | ||||
|         res.extend(expand_sync_from_into_full(&input, kind, var, &fields, &ruma_events)); | ||||
|         res.extend(expand_sync_from_into_full(&input, kind, var, &fields, &ruma_common)); | ||||
|     } | ||||
| 
 | ||||
|     if matches!(kind, EventKind::MessageLike | EventKind::State) | ||||
|         && matches!(var, EventKindVariation::Full | EventKindVariation::Sync) | ||||
|     { | ||||
|         res.extend(expand_redact_event(&input, kind, var, &fields, &ruma_events)); | ||||
|         res.extend(expand_redact_event(&input, kind, var, &fields, &ruma_common)); | ||||
|     } | ||||
| 
 | ||||
|     if is_non_stripped_room_event(kind, var) { | ||||
| @ -68,9 +68,9 @@ fn expand_serialize_event( | ||||
|     input: &DeriveInput, | ||||
|     var: EventKindVariation, | ||||
|     fields: &[Field], | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let serde = quote! { #ruma_events::exports::serde }; | ||||
|     let serde = quote! { #ruma_common::exports::serde }; | ||||
| 
 | ||||
|     let ident = &input.ident; | ||||
|     let (impl_gen, ty_gen, where_clause) = input.generics.split_for_impl(); | ||||
| @ -80,7 +80,7 @@ fn expand_serialize_event( | ||||
|             let name = field.ident.as_ref().unwrap(); | ||||
|             if name == "content" && var.is_redacted() { | ||||
|                 quote! { | ||||
|                     if #ruma_events::RedactedEventContent::has_serialize_fields(&self.content) { | ||||
|                     if #ruma_common::events::RedactedEventContent::has_serialize_fields(&self.content) { | ||||
|                         state.serialize_field("content", &self.content)?; | ||||
|                     } | ||||
|                 } | ||||
| @ -119,7 +119,7 @@ fn expand_serialize_event( | ||||
|             { | ||||
|                 use #serde::ser::{SerializeStruct as _, Error as _}; | ||||
| 
 | ||||
|                 let event_type = #ruma_events::EventContent::event_type(&self.content); | ||||
|                 let event_type = #ruma_common::events::EventContent::event_type(&self.content); | ||||
| 
 | ||||
|                 let mut state = serializer.serialize_struct(stringify!(#ident), 7)?; | ||||
| 
 | ||||
| @ -136,10 +136,10 @@ fn expand_deserialize_event( | ||||
|     _kind: EventKind, | ||||
|     var: EventKindVariation, | ||||
|     fields: &[Field], | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> syn::Result<TokenStream> { | ||||
|     let serde = quote! { #ruma_events::exports::serde }; | ||||
|     let serde_json = quote! { #ruma_events::exports::serde_json }; | ||||
|     let serde = quote! { #ruma_common::exports::serde }; | ||||
|     let serde_json = quote! { #ruma_common::exports::serde_json }; | ||||
| 
 | ||||
|     let ident = &input.ident; | ||||
|     // we know there is a content field already
 | ||||
| @ -180,10 +180,10 @@ fn expand_deserialize_event( | ||||
|                 if matches!(_kind, EventKind::State) && name == "unsigned" { | ||||
|                     match var { | ||||
|                         EventKindVariation::Full | EventKindVariation::Sync => { | ||||
|                             ty = quote! { #ruma_events::UnsignedWithPrevContent }; | ||||
|                             ty = quote! { #ruma_common::events::UnsignedWithPrevContent }; | ||||
|                         } | ||||
|                         EventKindVariation::Redacted | EventKindVariation::RedactedSync => { | ||||
|                             ty = quote! { #ruma_events::RedactedUnsignedWithPrevContent }; | ||||
|                             ty = quote! { #ruma_common::events::RedactedUnsignedWithPrevContent }; | ||||
|                         } | ||||
|                         EventKindVariation::Stripped | EventKindVariation::Initial => { | ||||
|                             unreachable!() | ||||
| @ -204,16 +204,16 @@ fn expand_deserialize_event( | ||||
|                 if is_generic && var.is_redacted() { | ||||
|                     quote! { | ||||
|                         let content = match C::has_deserialize_fields() { | ||||
|                             #ruma_events::HasDeserializeFields::False => { | ||||
|                             #ruma_common::events::HasDeserializeFields::False => { | ||||
|                                 C::empty(&event_type).map_err(A::Error::custom)? | ||||
|                             }, | ||||
|                             #ruma_events::HasDeserializeFields::True => { | ||||
|                             #ruma_common::events::HasDeserializeFields::True => { | ||||
|                                 let json = content.ok_or_else( | ||||
|                                     || #serde::de::Error::missing_field("content"), | ||||
|                                 )?; | ||||
|                                 C::from_parts(&event_type, &json).map_err(A::Error::custom)? | ||||
|                             }, | ||||
|                             #ruma_events::HasDeserializeFields::Optional => { | ||||
|                             #ruma_common::events::HasDeserializeFields::Optional => { | ||||
|                                 let json = content.unwrap_or( | ||||
|                                     #serde_json::value::RawValue::from_string("{}".to_owned()) | ||||
|                                         .unwrap() | ||||
| @ -412,9 +412,9 @@ fn expand_redact_event( | ||||
|     kind: EventKind, | ||||
|     var: EventKindVariation, | ||||
|     fields: &[Field], | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers }; | ||||
|     let ruma_identifiers = quote! { #ruma_common::exports::ruma_identifiers }; | ||||
| 
 | ||||
|     let redacted_type = kind.to_event_ident(var.to_redacted()); | ||||
|     let redacted_content_trait = | ||||
| @ -433,10 +433,10 @@ fn expand_redact_event( | ||||
|     }; | ||||
| 
 | ||||
|     let where_clause = generics.make_where_clause(); | ||||
|     where_clause.predicates.push(parse_quote! { #ty_param: #ruma_events::RedactContent }); | ||||
|     where_clause.predicates.push(parse_quote! { #ty_param: #ruma_common::events::RedactContent }); | ||||
|     where_clause.predicates.push(parse_quote! { | ||||
|         <#ty_param as #ruma_events::RedactContent>::Redacted: | ||||
|             #ruma_events::#redacted_content_trait | ||||
|         <#ty_param as #ruma_common::events::RedactContent>::Redacted: | ||||
|             #ruma_common::events::#redacted_content_trait | ||||
|     }); | ||||
| 
 | ||||
|     let (impl_generics, ty_gen, where_clause) = generics.split_for_impl(); | ||||
| @ -448,8 +448,9 @@ fn expand_redact_event( | ||||
|             None | ||||
|         } else if ident == "unsigned" { | ||||
|             Some(quote! { | ||||
|                 unsigned: | ||||
|                     #ruma_events::RedactedUnsigned::new_because(::std::boxed::Box::new(redaction)) | ||||
|                 unsigned: #ruma_common::events::RedactedUnsigned::new_because( | ||||
|                     ::std::boxed::Box::new(redaction), | ||||
|                 ) | ||||
|             }) | ||||
|         } else { | ||||
|             Some(quote! { | ||||
| @ -460,17 +461,18 @@ fn expand_redact_event( | ||||
| 
 | ||||
|     quote! { | ||||
|         #[automatically_derived] | ||||
|         impl #impl_generics #ruma_events::Redact for #ident #ty_gen #where_clause { | ||||
|             type Redacted = | ||||
|                 #ruma_events::#redacted_type<<#ty_param as #ruma_events::RedactContent>::Redacted>; | ||||
|         impl #impl_generics #ruma_common::events::Redact for #ident #ty_gen #where_clause { | ||||
|             type Redacted = #ruma_common::events::#redacted_type< | ||||
|                 <#ty_param as #ruma_common::events::RedactContent>::Redacted, | ||||
|             >; | ||||
| 
 | ||||
|             fn redact( | ||||
|                 self, | ||||
|                 redaction: #ruma_events::room::redaction::SyncRoomRedactionEvent, | ||||
|                 redaction: #ruma_common::events::room::redaction::SyncRoomRedactionEvent, | ||||
|                 version: &#ruma_identifiers::RoomVersionId, | ||||
|             ) -> Self::Redacted { | ||||
|                 let content = #ruma_events::RedactContent::redact(self.content, version); | ||||
|                 #ruma_events::#redacted_type { | ||||
|                 let content = #ruma_common::events::RedactContent::redact(self.content, version); | ||||
|                 #ruma_common::events::#redacted_type { | ||||
|                     content, | ||||
|                     #(#fields),* | ||||
|                 } | ||||
| @ -484,9 +486,9 @@ fn expand_sync_from_into_full( | ||||
|     kind: EventKind, | ||||
|     var: EventKindVariation, | ||||
|     fields: &[Field], | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers }; | ||||
|     let ruma_identifiers = quote! { #ruma_common::exports::ruma_identifiers }; | ||||
| 
 | ||||
|     let ident = &input.ident; | ||||
|     let full_struct = kind.to_event_ident(var.to_full()); | ||||
|  | ||||
| @ -103,7 +103,7 @@ impl Parse for MetaAttrs { | ||||
| /// Create an `EventContent` implementation for a struct.
 | ||||
| pub fn expand_event_content( | ||||
|     input: &DeriveInput, | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> syn::Result<TokenStream> { | ||||
|     let content_attr = input | ||||
|         .attrs | ||||
| @ -147,17 +147,17 @@ pub fn expand_event_content( | ||||
| 
 | ||||
|     // We only generate redacted content structs for state and message-like events
 | ||||
|     let redacted_event_content = needs_redacted(&content_attr, event_kind) | ||||
|         .then(|| generate_redacted_event_content(input, event_type, ruma_events, event_kind)) | ||||
|         .then(|| generate_redacted_event_content(input, event_type, ruma_common, event_kind)) | ||||
|         .transpose()?; | ||||
| 
 | ||||
|     let event_content_impl = generate_event_content_impl(&input.ident, event_type, ruma_events); | ||||
|     let event_content_impl = generate_event_content_impl(&input.ident, event_type, ruma_common); | ||||
|     let static_event_content_impl = event_kind.map(|k| { | ||||
|         generate_static_event_content_impl(&input.ident, k, false, event_type, ruma_events) | ||||
|         generate_static_event_content_impl(&input.ident, k, false, event_type, ruma_common) | ||||
|     }); | ||||
|     let marker_trait_impl = | ||||
|         event_kind.map(|k| generate_marker_trait_impl(k, &input.ident, ruma_events)).transpose()?; | ||||
|         event_kind.map(|k| generate_marker_trait_impl(k, &input.ident, ruma_common)).transpose()?; | ||||
|     let type_aliases = event_kind | ||||
|         .map(|k| generate_event_type_aliases(k, &input.ident, &event_type.value(), ruma_events)) | ||||
|         .map(|k| generate_event_type_aliases(k, &input.ident, &event_type.value(), ruma_common)) | ||||
|         .transpose()?; | ||||
| 
 | ||||
|     Ok(quote! { | ||||
| @ -172,12 +172,12 @@ pub fn expand_event_content( | ||||
| fn generate_redacted_event_content( | ||||
|     input: &DeriveInput, | ||||
|     event_type: &LitStr, | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
|     event_kind: Option<EventKind>, | ||||
| ) -> Result<TokenStream, syn::Error> { | ||||
|     let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers }; | ||||
|     let serde = quote! { #ruma_events::exports::serde }; | ||||
|     let serde_json = quote! { #ruma_events::exports::serde_json }; | ||||
|     let ruma_identifiers = quote! { #ruma_common::exports::ruma_identifiers }; | ||||
|     let serde = quote! { #ruma_common::exports::serde }; | ||||
|     let serde_json = quote! { #ruma_common::exports::serde_json }; | ||||
| 
 | ||||
|     let ident = &input.ident; | ||||
|     let doc = format!("Redacted form of [`{}`]", ident); | ||||
| @ -241,9 +241,9 @@ fn generate_redacted_event_content( | ||||
|     }; | ||||
| 
 | ||||
|     let (has_deserialize_fields, has_serialize_fields) = if kept_redacted_fields.is_empty() { | ||||
|         (quote! { #ruma_events::HasDeserializeFields::False }, quote! { false }) | ||||
|         (quote! { #ruma_common::events::HasDeserializeFields::False }, quote! { false }) | ||||
|     } else { | ||||
|         (quote! { #ruma_events::HasDeserializeFields::True }, quote! { true }) | ||||
|         (quote! { #ruma_common::events::HasDeserializeFields::True }, quote! { true }) | ||||
|     }; | ||||
| 
 | ||||
|     let constructor = kept_redacted_fields.is_empty().then(|| { | ||||
| @ -259,28 +259,28 @@ fn generate_redacted_event_content( | ||||
|     }); | ||||
| 
 | ||||
|     let redacted_event_content = | ||||
|         generate_event_content_impl(&redacted_ident, event_type, ruma_events); | ||||
|         generate_event_content_impl(&redacted_ident, event_type, ruma_common); | ||||
| 
 | ||||
|     let marker_trait_impl = match event_kind { | ||||
|         Some(EventKind::MessageLike) => quote! { | ||||
|             #[automatically_derived] | ||||
|             impl #ruma_events::RedactedMessageLikeEventContent for #redacted_ident {} | ||||
|             impl #ruma_common::events::RedactedMessageLikeEventContent for #redacted_ident {} | ||||
|         }, | ||||
|         Some(EventKind::State) => quote! { | ||||
|             #[automatically_derived] | ||||
|             impl #ruma_events::RedactedStateEventContent for #redacted_ident {} | ||||
|             impl #ruma_common::events::RedactedStateEventContent for #redacted_ident {} | ||||
|         }, | ||||
|         _ => TokenStream::new(), | ||||
|     }; | ||||
| 
 | ||||
|     let static_event_content_impl = event_kind.map(|k| { | ||||
|         generate_static_event_content_impl(&redacted_ident, k, true, event_type, ruma_events) | ||||
|         generate_static_event_content_impl(&redacted_ident, k, true, event_type, ruma_common) | ||||
|     }); | ||||
| 
 | ||||
|     Ok(quote! { | ||||
|         // this is the non redacted event content's impl
 | ||||
|         #[automatically_derived] | ||||
|         impl #ruma_events::RedactContent for #ident { | ||||
|         impl #ruma_common::events::RedactContent for #ident { | ||||
|             type Redacted = #redacted_ident; | ||||
| 
 | ||||
|             fn redact(self, version: &#ruma_identifiers::RoomVersionId) -> #redacted_ident { | ||||
| @ -300,7 +300,7 @@ fn generate_redacted_event_content( | ||||
|         #redacted_event_content | ||||
| 
 | ||||
|         #[automatically_derived] | ||||
|         impl #ruma_events::RedactedEventContent for #redacted_ident { | ||||
|         impl #ruma_common::events::RedactedEventContent for #redacted_ident { | ||||
|             fn empty(ev_type: &str) -> #serde_json::Result<Self> { | ||||
|                 if ev_type != #event_type { | ||||
|                     return Err(#serde::de::Error::custom( | ||||
| @ -315,7 +315,7 @@ fn generate_redacted_event_content( | ||||
|                 #has_serialize_fields | ||||
|             } | ||||
| 
 | ||||
|             fn has_deserialize_fields() -> #ruma_events::HasDeserializeFields { | ||||
|             fn has_deserialize_fields() -> #ruma_common::events::HasDeserializeFields { | ||||
|                 #has_deserialize_fields | ||||
|             } | ||||
|         } | ||||
| @ -329,7 +329,7 @@ fn generate_event_type_aliases( | ||||
|     event_kind: EventKind, | ||||
|     ident: &Ident, | ||||
|     event_type: &str, | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> syn::Result<TokenStream> { | ||||
|     // The redaction module has its own event types.
 | ||||
|     if ident == "RoomRedactionEventContent" { | ||||
| @ -374,7 +374,7 @@ fn generate_event_type_aliases( | ||||
| 
 | ||||
|         quote! { | ||||
|             #[doc = #ev_type_doc] | ||||
|             pub type #ev_type = #ruma_events::#ev_struct<#content_struct>; | ||||
|             pub type #ev_type = #ruma_common::events::#ev_struct<#content_struct>; | ||||
|         } | ||||
|     }) | ||||
|     .collect(); | ||||
| @ -385,7 +385,7 @@ fn generate_event_type_aliases( | ||||
| fn generate_marker_trait_impl( | ||||
|     event_kind: EventKind, | ||||
|     ident: &Ident, | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> syn::Result<TokenStream> { | ||||
|     let marker_trait = match event_kind { | ||||
|         EventKind::GlobalAccountData => quote! { GlobalAccountDataEventContent }, | ||||
| @ -408,21 +408,21 @@ fn generate_marker_trait_impl( | ||||
| 
 | ||||
|     Ok(quote! { | ||||
|         #[automatically_derived] | ||||
|         impl #ruma_events::#marker_trait for #ident {} | ||||
|         impl #ruma_common::events::#marker_trait for #ident {} | ||||
|     }) | ||||
| } | ||||
| 
 | ||||
| fn generate_event_content_impl( | ||||
|     ident: &Ident, | ||||
|     event_type: &LitStr, | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let serde = quote! { #ruma_events::exports::serde }; | ||||
|     let serde_json = quote! { #ruma_events::exports::serde_json }; | ||||
|     let serde = quote! { #ruma_common::exports::serde }; | ||||
|     let serde_json = quote! { #ruma_common::exports::serde_json }; | ||||
| 
 | ||||
|     quote! { | ||||
|         #[automatically_derived] | ||||
|         impl #ruma_events::EventContent for #ident { | ||||
|         impl #ruma_common::events::EventContent for #ident { | ||||
|             fn event_type(&self) -> &str { | ||||
|                 #event_type | ||||
|             } | ||||
| @ -448,7 +448,7 @@ fn generate_static_event_content_impl( | ||||
|     event_kind: EventKind, | ||||
|     redacted: bool, | ||||
|     event_type: &LitStr, | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let event_kind = match event_kind { | ||||
|         EventKind::GlobalAccountData => quote! { GlobalAccountData }, | ||||
| @ -466,8 +466,9 @@ fn generate_static_event_content_impl( | ||||
|     }; | ||||
| 
 | ||||
|     quote! { | ||||
|         impl #ruma_events::StaticEventContent for #ident { | ||||
|             const KIND: #ruma_events::EventKind = #ruma_events::EventKind::#event_kind; | ||||
|         impl #ruma_common::events::StaticEventContent for #ident { | ||||
|             const KIND: #ruma_common::events::EventKind = | ||||
|                 #ruma_common::events::EventKind::#event_kind; | ||||
|             const TYPE: &'static ::std::primitive::str = #event_type; | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @ -13,7 +13,7 @@ use super::{ | ||||
| pub fn expand_event_enums(input: &EventEnumDecl) -> syn::Result<TokenStream> { | ||||
|     use EventKindVariation as V; | ||||
| 
 | ||||
|     let ruma_events = crate::import_ruma_events(); | ||||
|     let ruma_common = crate::import_ruma_common(); | ||||
| 
 | ||||
|     let mut res = TokenStream::new(); | ||||
| 
 | ||||
| @ -25,31 +25,31 @@ pub fn expand_event_enums(input: &EventEnumDecl) -> syn::Result<TokenStream> { | ||||
| 
 | ||||
|     let events = &events; | ||||
|     let variants = &variants; | ||||
|     let ruma_events = &ruma_events; | ||||
|     let ruma_common = &ruma_common; | ||||
| 
 | ||||
|     res.extend(expand_event_enum(kind, V::Full, events, attrs, variants, ruma_events)); | ||||
|     res.extend(expand_content_enum(kind, events, attrs, variants, ruma_events)); | ||||
|     res.extend(expand_event_enum(kind, V::Full, events, attrs, variants, ruma_common)); | ||||
|     res.extend(expand_content_enum(kind, events, attrs, variants, ruma_common)); | ||||
| 
 | ||||
|     if matches!(kind, EventKind::Ephemeral | EventKind::MessageLike | EventKind::State) { | ||||
|         res.extend(expand_event_enum(kind, V::Sync, events, attrs, variants, ruma_events)); | ||||
|         res.extend(expand_event_enum(kind, V::Sync, events, attrs, variants, ruma_common)); | ||||
|         res.extend(expand_from_full_event(kind, V::Full, variants)); | ||||
|         res.extend(expand_into_full_event(kind, V::Sync, variants, ruma_events)); | ||||
|         res.extend(expand_into_full_event(kind, V::Sync, variants, ruma_common)); | ||||
|     } | ||||
| 
 | ||||
|     if matches!(kind, EventKind::State) { | ||||
|         res.extend(expand_event_enum(kind, V::Stripped, events, attrs, variants, ruma_events)); | ||||
|         res.extend(expand_event_enum(kind, V::Initial, events, attrs, variants, ruma_events)); | ||||
|         res.extend(expand_event_enum(kind, V::Stripped, events, attrs, variants, ruma_common)); | ||||
|         res.extend(expand_event_enum(kind, V::Initial, events, attrs, variants, ruma_common)); | ||||
|     } | ||||
| 
 | ||||
|     if matches!(kind, EventKind::MessageLike | EventKind::State) { | ||||
|         res.extend(expand_event_enum(kind, V::Redacted, events, attrs, variants, ruma_events)); | ||||
|         res.extend(expand_event_enum(kind, V::RedactedSync, events, attrs, variants, ruma_events)); | ||||
|         res.extend(expand_redact(kind, V::Full, variants, ruma_events)); | ||||
|         res.extend(expand_redact(kind, V::Sync, variants, ruma_events)); | ||||
|         res.extend(expand_possibly_redacted_enum(kind, V::Full, ruma_events)); | ||||
|         res.extend(expand_possibly_redacted_enum(kind, V::Sync, ruma_events)); | ||||
|         res.extend(expand_event_enum(kind, V::Redacted, events, attrs, variants, ruma_common)); | ||||
|         res.extend(expand_event_enum(kind, V::RedactedSync, events, attrs, variants, ruma_common)); | ||||
|         res.extend(expand_redact(kind, V::Full, variants, ruma_common)); | ||||
|         res.extend(expand_redact(kind, V::Sync, variants, ruma_common)); | ||||
|         res.extend(expand_possibly_redacted_enum(kind, V::Full, ruma_common)); | ||||
|         res.extend(expand_possibly_redacted_enum(kind, V::Sync, ruma_common)); | ||||
|         res.extend(expand_from_full_event(kind, V::Redacted, variants)); | ||||
|         res.extend(expand_into_full_event(kind, V::RedactedSync, variants, ruma_events)); | ||||
|         res.extend(expand_into_full_event(kind, V::RedactedSync, variants, ruma_common)); | ||||
|     } | ||||
| 
 | ||||
|     Ok(res) | ||||
| @ -61,17 +61,17 @@ fn expand_event_enum( | ||||
|     events: &[LitStr], | ||||
|     attrs: &[Attribute], | ||||
|     variants: &[EventEnumVariant], | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let event_struct = kind.to_event_ident(var); | ||||
|     let ident = kind.to_event_enum_ident(var); | ||||
| 
 | ||||
|     let variant_decls = variants.iter().map(|v| v.decl()); | ||||
|     let content: Vec<_> = | ||||
|         events.iter().map(|event| to_event_path(event, kind, var, ruma_events)).collect(); | ||||
|         events.iter().map(|event| to_event_path(event, kind, var, ruma_common)).collect(); | ||||
| 
 | ||||
|     let deserialize_impl = expand_deserialize_impl(kind, var, events, variants, ruma_events); | ||||
|     let field_accessor_impl = expand_accessor_methods(kind, var, variants, ruma_events); | ||||
|     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 from_impl = expand_from_impl(&ident, &content, variants); | ||||
| 
 | ||||
|     quote! { | ||||
| @ -86,7 +86,11 @@ fn expand_event_enum( | ||||
|             )* | ||||
|             /// An event not defined by the Matrix specification
 | ||||
|             #[doc(hidden)] | ||||
|             _Custom(#ruma_events::#event_struct<#ruma_events::_custom::CustomEventContent>), | ||||
|             _Custom( | ||||
|                 #ruma_common::events::#event_struct< | ||||
|                     #ruma_common::events::_custom::CustomEventContent, | ||||
|                 >, | ||||
|             ), | ||||
|         } | ||||
| 
 | ||||
|         #deserialize_impl | ||||
| @ -100,11 +104,11 @@ fn expand_deserialize_impl( | ||||
|     var: EventKindVariation, | ||||
|     events: &[LitStr], | ||||
|     variants: &[EventEnumVariant], | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let ruma_serde = quote! { #ruma_events::exports::ruma_serde }; | ||||
|     let serde = quote! { #ruma_events::exports::serde }; | ||||
|     let serde_json = quote! { #ruma_events::exports::serde_json }; | ||||
|     let ruma_serde = quote! { #ruma_common::exports::ruma_serde }; | ||||
|     let serde = quote! { #ruma_common::exports::serde }; | ||||
|     let serde_json = quote! { #ruma_common::exports::serde_json }; | ||||
| 
 | ||||
|     let ident = kind.to_event_enum_ident(var); | ||||
| 
 | ||||
| @ -113,7 +117,7 @@ fn expand_deserialize_impl( | ||||
|         quote! { #(#attrs)* } | ||||
|     }); | ||||
|     let self_variants = variants.iter().map(|v| v.ctor(quote! { Self })); | ||||
|     let content = events.iter().map(|event| to_event_path(event, kind, var, ruma_events)); | ||||
|     let content = events.iter().map(|event| to_event_path(event, kind, var, ruma_common)); | ||||
| 
 | ||||
|     quote! { | ||||
|         impl<'de> #serde::de::Deserialize<'de> for #ident { | ||||
| @ -124,7 +128,7 @@ fn expand_deserialize_impl( | ||||
|                 use #serde::de::Error as _; | ||||
| 
 | ||||
|                 let json = Box::<#serde_json::value::RawValue>::deserialize(deserializer)?; | ||||
|                 let #ruma_events::EventTypeDeHelper { ev_type, .. } = | ||||
|                 let #ruma_common::events::EventTypeDeHelper { ev_type, .. } = | ||||
|                     #ruma_serde::from_raw_json_value(&json)?; | ||||
| 
 | ||||
|                 match &*ev_type { | ||||
| @ -202,9 +206,9 @@ fn expand_into_full_event( | ||||
|     kind: EventKind, | ||||
|     var: EventKindVariation, | ||||
|     variants: &[EventEnumVariant], | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers }; | ||||
|     let ruma_identifiers = quote! { #ruma_common::exports::ruma_identifiers }; | ||||
| 
 | ||||
|     let ident = kind.to_event_enum_ident(var); | ||||
|     let full = kind.to_event_enum_ident(var.to_full()); | ||||
| @ -241,17 +245,17 @@ fn expand_content_enum( | ||||
|     events: &[LitStr], | ||||
|     attrs: &[Attribute], | ||||
|     variants: &[EventEnumVariant], | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let serde = quote! { #ruma_events::exports::serde }; | ||||
|     let serde_json = quote! { #ruma_events::exports::serde_json }; | ||||
|     let serde = quote! { #ruma_common::exports::serde }; | ||||
|     let serde_json = quote! { #ruma_common::exports::serde_json }; | ||||
| 
 | ||||
|     let ident = kind.to_content_enum(); | ||||
| 
 | ||||
|     let event_type_str = events; | ||||
| 
 | ||||
|     let content: Vec<_> = | ||||
|         events.iter().map(|ev| to_event_content_path(kind, ev, None, ruma_events)).collect(); | ||||
|         events.iter().map(|ev| to_event_content_path(kind, ev, None, ruma_common)).collect(); | ||||
| 
 | ||||
|     let variant_decls = variants.iter().map(|v| v.decl()).collect::<Vec<_>>(); | ||||
| 
 | ||||
| @ -262,11 +266,11 @@ fn expand_content_enum( | ||||
|     let variant_arms = variants.iter().map(|v| v.match_arm(quote! { Self })).collect::<Vec<_>>(); | ||||
|     let variant_ctors = variants.iter().map(|v| v.ctor(quote! { Self })); | ||||
| 
 | ||||
|     let marker_trait_impl = expand_marker_trait_impl(kind, ruma_events); | ||||
|     let marker_trait_impl = expand_marker_trait_impl(kind, ruma_common); | ||||
|     let from_impl = expand_from_impl(&ident, &content, variants); | ||||
| 
 | ||||
|     let serialize_custom_event_error_path = | ||||
|         quote! { #ruma_events::serialize_custom_event_error }.to_string(); | ||||
|         quote! { #ruma_common::events::serialize_custom_event_error }.to_string(); | ||||
| 
 | ||||
|     quote! { | ||||
|         #( #attrs )* | ||||
| @ -287,7 +291,7 @@ fn expand_content_enum( | ||||
|         } | ||||
| 
 | ||||
|         #[automatically_derived] | ||||
|         impl #ruma_events::EventContent for #ident { | ||||
|         impl #ruma_common::events::EventContent for #ident { | ||||
|             fn event_type(&self) -> &::std::primitive::str { | ||||
|                 match self { | ||||
|                     #( #variant_arms(content) => content.event_type(), )* | ||||
| @ -324,9 +328,9 @@ fn expand_redact( | ||||
|     kind: EventKind, | ||||
|     var: EventKindVariation, | ||||
|     variants: &[EventEnumVariant], | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers }; | ||||
|     let ruma_identifiers = quote! { #ruma_common::exports::ruma_identifiers }; | ||||
| 
 | ||||
|     let ident = kind.to_event_enum_ident(var); | ||||
|     let redacted_enum = kind.to_event_enum_ident(var.to_redacted()); | ||||
| @ -336,22 +340,22 @@ fn expand_redact( | ||||
| 
 | ||||
|     quote! { | ||||
|         #[automatically_derived] | ||||
|         impl #ruma_events::Redact for #ident { | ||||
|         impl #ruma_common::events::Redact for #ident { | ||||
|             type Redacted = #redacted_enum; | ||||
| 
 | ||||
|             fn redact( | ||||
|                 self, | ||||
|                 redaction: #ruma_events::room::redaction::SyncRoomRedactionEvent, | ||||
|                 redaction: #ruma_common::events::room::redaction::SyncRoomRedactionEvent, | ||||
|                 version: &#ruma_identifiers::RoomVersionId, | ||||
|             ) -> #redacted_enum { | ||||
|                 match self { | ||||
|                     #( | ||||
|                         #self_variants(event) => #redacted_variants( | ||||
|                             #ruma_events::Redact::redact(event, redaction, version), | ||||
|                             #ruma_common::events::Redact::redact(event, redaction, version), | ||||
|                         ), | ||||
|                     )* | ||||
|                     Self::_Custom(event) => #redacted_enum::_Custom( | ||||
|                         #ruma_events::Redact::redact(event, redaction, version), | ||||
|                         #ruma_common::events::Redact::redact(event, redaction, version), | ||||
|                     ) | ||||
|                 } | ||||
|             } | ||||
| @ -362,11 +366,11 @@ fn expand_redact( | ||||
| fn expand_possibly_redacted_enum( | ||||
|     kind: EventKind, | ||||
|     var: EventKindVariation, | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let ruma_serde = quote! { #ruma_events::exports::ruma_serde }; | ||||
|     let serde = quote! { #ruma_events::exports::serde }; | ||||
|     let serde_json = quote! { #ruma_events::exports::serde_json }; | ||||
|     let ruma_serde = quote! { #ruma_common::exports::ruma_serde }; | ||||
|     let serde = quote! { #ruma_common::exports::serde }; | ||||
|     let serde_json = quote! { #ruma_common::exports::serde_json }; | ||||
| 
 | ||||
|     let ident = format_ident!("AnyPossiblyRedacted{}", kind.to_event_ident(var)); | ||||
|     let regular_enum_ident = kind.to_event_enum_ident(var); | ||||
| @ -390,7 +394,7 @@ fn expand_possibly_redacted_enum( | ||||
|                 D: #serde::de::Deserializer<'de>, | ||||
|             { | ||||
|                 let json = Box::<#serde_json::value::RawValue>::deserialize(deserializer)?; | ||||
|                 let #ruma_events::RedactionDeHelper { unsigned } = | ||||
|                 let #ruma_common::events::RedactionDeHelper { unsigned } = | ||||
|                     #ruma_serde::from_raw_json_value(&json)?; | ||||
| 
 | ||||
|                 Ok(match unsigned { | ||||
| @ -404,7 +408,7 @@ fn expand_possibly_redacted_enum( | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| fn expand_marker_trait_impl(kind: EventKind, ruma_events: &TokenStream) -> TokenStream { | ||||
| fn expand_marker_trait_impl(kind: EventKind, ruma_common: &TokenStream) -> TokenStream { | ||||
|     let marker_trait = match kind { | ||||
|         EventKind::State => quote! { StateEventContent }, | ||||
|         EventKind::MessageLike => quote! { MessageLikeEventContent }, | ||||
| @ -418,7 +422,7 @@ fn expand_marker_trait_impl(kind: EventKind, ruma_events: &TokenStream) -> Token | ||||
|     let ident = kind.to_content_enum(); | ||||
|     quote! { | ||||
|         #[automatically_derived] | ||||
|         impl #ruma_events::#marker_trait for #ident {} | ||||
|         impl #ruma_common::events::#marker_trait for #ident {} | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @ -426,7 +430,7 @@ fn expand_accessor_methods( | ||||
|     kind: EventKind, | ||||
|     var: EventKindVariation, | ||||
|     variants: &[EventEnumVariant], | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let ident = kind.to_event_enum_ident(var); | ||||
|     let self_variants: Vec<_> = variants.iter().map(|v| v.match_arm(quote! { Self })).collect(); | ||||
| @ -448,7 +452,7 @@ fn expand_accessor_methods( | ||||
|                         Self::_Custom(event) => { | ||||
|                             event.prev_content.as_ref().map(|c| #content_enum::_Custom { | ||||
|                                 event_type: crate::PrivOwnedStr( | ||||
|                                     #ruma_events::EventContent::event_type(c).into(), | ||||
|                                     #ruma_common::events::EventContent::event_type(c).into(), | ||||
|                                 ), | ||||
|                             }) | ||||
|                         }, | ||||
| @ -464,7 +468,7 @@ fn expand_accessor_methods( | ||||
|                     #( #self_variants(event) => #content_variants(event.content.clone()), )* | ||||
|                     Self::_Custom(event) => #content_enum::_Custom { | ||||
|                         event_type: crate::PrivOwnedStr( | ||||
|                             #ruma_events::EventContent::event_type(&event.content).into(), | ||||
|                             #ruma_common::events::EventContent::event_type(&event.content).into(), | ||||
|                         ), | ||||
|                     }, | ||||
|                 } | ||||
| @ -478,7 +482,7 @@ fn expand_accessor_methods( | ||||
|         has_field(kind, var).then(|| { | ||||
|             let docs = format!("Returns this event's {} field.", name); | ||||
|             let ident = Ident::new(name, Span::call_site()); | ||||
|             let field_type = field_return_type(name, var, ruma_events); | ||||
|             let field_type = field_return_type(name, var, ruma_common); | ||||
|             let variants = variants.iter().map(|v| v.match_arm(quote! { Self })); | ||||
| 
 | ||||
|             quote! { | ||||
| @ -500,9 +504,9 @@ fn expand_accessor_methods( | ||||
|             pub fn event_type(&self) -> &::std::primitive::str { | ||||
|                 match self { | ||||
|                     #( #self_variants(event) => | ||||
|                         #ruma_events::EventContent::event_type(&event.content), )* | ||||
|                         #ruma_common::events::EventContent::event_type(&event.content), )* | ||||
|                     Self::_Custom(event) => | ||||
|                         #ruma_events::EventContent::event_type(&event.content), | ||||
|                         #ruma_common::events::EventContent::event_type(&event.content), | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
| @ -517,7 +521,7 @@ fn to_event_path( | ||||
|     name: &LitStr, | ||||
|     kind: EventKind, | ||||
|     var: EventKindVariation, | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let span = name.span(); | ||||
|     let name = name.value(); | ||||
| @ -540,14 +544,14 @@ fn to_event_path( | ||||
|     } else { | ||||
|         format_ident!("{}{}Event", var, event) | ||||
|     }; | ||||
|     quote! { #ruma_events::#( #path )::*::#event_name } | ||||
|     quote! { #ruma_common::events::#( #path )::*::#event_name } | ||||
| } | ||||
| 
 | ||||
| fn to_event_content_path( | ||||
|     kind: EventKind, | ||||
|     name: &LitStr, | ||||
|     prefix: Option<&str>, | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let span = name.span(); | ||||
|     let name = name.value(); | ||||
| @ -572,7 +576,7 @@ fn to_event_content_path( | ||||
|     let path = path.iter().map(|s| Ident::new(s, span)); | ||||
| 
 | ||||
|     quote! { | ||||
|         #ruma_events::#( #path )::*::#content_str | ||||
|         #ruma_common::events::#( #path )::*::#content_str | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @ -600,10 +604,9 @@ fn to_camel_case(name: &LitStr) -> syn::Result<Ident> { | ||||
| fn field_return_type( | ||||
|     name: &str, | ||||
|     var: EventKindVariation, | ||||
|     ruma_events: &TokenStream, | ||||
|     ruma_common: &TokenStream, | ||||
| ) -> TokenStream { | ||||
|     let ruma_common = quote! { #ruma_events::exports::ruma_common }; | ||||
|     let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers }; | ||||
|     let ruma_identifiers = quote! { #ruma_common::exports::ruma_identifiers }; | ||||
| 
 | ||||
|     match name { | ||||
|         "origin_server_ts" => quote! { #ruma_common::MilliSecondsSinceUnixEpoch }, | ||||
| @ -613,9 +616,9 @@ fn field_return_type( | ||||
|         "state_key" => quote! { ::std::primitive::str }, | ||||
|         "unsigned" => { | ||||
|             if var.is_redacted() { | ||||
|                 quote! { #ruma_events::RedactedUnsigned } | ||||
|                 quote! { #ruma_common::events::RedactedUnsigned } | ||||
|             } else { | ||||
|                 quote! { #ruma_events::Unsigned } | ||||
|                 quote! { #ruma_common::events::Unsigned } | ||||
|             } | ||||
|         } | ||||
|         _ => panic!("the `ruma_macros::event_enum::EVENT_FIELD` const was changed"), | ||||
|  | ||||
| @ -6,9 +6,9 @@ use super::event_parse::{EventEnumEntry, EventEnumInput, EventKind}; | ||||
| 
 | ||||
| pub fn expand_event_type_enum( | ||||
|     input: EventEnumInput, | ||||
|     ruma_events: TokenStream, | ||||
|     ruma_common: TokenStream, | ||||
| ) -> syn::Result<TokenStream> { | ||||
|     let ruma_serde = quote! { #ruma_events::exports::ruma_serde }; | ||||
|     let ruma_serde = quote! { #ruma_common::exports::ruma_serde }; | ||||
| 
 | ||||
|     let mut room: Vec<&Vec<EventEnumEntry>> = vec![]; | ||||
|     let mut state: Vec<&Vec<EventEnumEntry>> = vec![]; | ||||
|  | ||||
| @ -71,7 +71,7 @@ mod serde; | ||||
| //// supported:  https://github.com/rust-lang/rust/issues/74563
 | ||||
| #[proc_macro] | ||||
| pub fn event_enum(input: TokenStream) -> TokenStream { | ||||
|     let ruma_events = import_ruma_events(); | ||||
|     let ruma_common = import_ruma_common(); | ||||
| 
 | ||||
|     let event_enum_input = syn::parse_macro_input!(input as EventEnumInput); | ||||
|     let enums = event_enum_input | ||||
| @ -79,7 +79,7 @@ pub fn event_enum(input: TokenStream) -> TokenStream { | ||||
|         .iter() | ||||
|         .map(expand_event_enums) | ||||
|         .collect::<syn::Result<pm2::TokenStream>>(); | ||||
|     let event_types = expand_event_type_enum(event_enum_input, ruma_events); | ||||
|     let event_types = expand_event_type_enum(event_enum_input, ruma_common); | ||||
|     event_types | ||||
|         .and_then(|types| { | ||||
|             enums.map(|mut enums| { | ||||
| @ -94,10 +94,10 @@ pub fn event_enum(input: TokenStream) -> TokenStream { | ||||
| /// Generates an implementation of `ruma_common::events::EventContent`.
 | ||||
| #[proc_macro_derive(EventContent, attributes(ruma_event))] | ||||
| pub fn derive_event_content(input: TokenStream) -> TokenStream { | ||||
|     let ruma_events = import_ruma_events(); | ||||
|     let ruma_common = import_ruma_common(); | ||||
|     let input = parse_macro_input!(input as DeriveInput); | ||||
| 
 | ||||
|     expand_event_content(&input, &ruma_events).unwrap_or_else(syn::Error::into_compile_error).into() | ||||
|     expand_event_content(&input, &ruma_common).unwrap_or_else(syn::Error::into_compile_error).into() | ||||
| } | ||||
| 
 | ||||
| /// Generates implementations needed to serialize and deserialize Matrix events.
 | ||||
| @ -107,21 +107,21 @@ pub fn derive_event(input: TokenStream) -> TokenStream { | ||||
|     expand_event(input).unwrap_or_else(syn::Error::into_compile_error).into() | ||||
| } | ||||
| 
 | ||||
| pub(crate) fn import_ruma_events() -> pm2::TokenStream { | ||||
| 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::events } | ||||
|         quote! { ::#import } | ||||
|     } else if let Ok(FoundCrate::Name(name)) = crate_name("ruma") { | ||||
|         let import = format_ident!("{}", name); | ||||
|         quote! { ::#import::events } | ||||
|         quote! { ::#import } | ||||
|     } else if let Ok(FoundCrate::Name(name)) = crate_name("matrix-sdk") { | ||||
|         let import = format_ident!("{}", name); | ||||
|         quote! { ::#import::ruma::events } | ||||
|         quote! { ::#import::ruma } | ||||
|     } else if let Ok(FoundCrate::Name(name)) = crate_name("matrix-sdk-appservice") { | ||||
|         let import = format_ident!("{}", name); | ||||
|         quote! { ::#import::ruma::events } | ||||
|         quote! { ::#import::ruma } | ||||
|     } else { | ||||
|         quote! { ::ruma_common::events } | ||||
|         quote! { ::ruma_common } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user