events: Use input visibility for generated types in EventContent derive
This commit is contained in:
		
							parent
							
								
									e58b9fcd92
								
							
						
					
					
						commit
						9c65a7dcae
					
				| @ -3,6 +3,9 @@ | |||||||
| Improvements: | Improvements: | ||||||
| 
 | 
 | ||||||
| - Derive `Hash` for `ReceiptType` and `ReceiptThread` | - Derive `Hash` for `ReceiptType` and `ReceiptThread` | ||||||
|  | - Update `EventContent` derive macro to emit new type definitions and type | ||||||
|  |   aliases under the same visibility as the input type (this fixes a future- | ||||||
|  |   compatibility warning when deriving `EventContent` on a non-`pub` type) | ||||||
| 
 | 
 | ||||||
| # 0.11.1 | # 0.11.1 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -7,4 +7,5 @@ fn ui() { | |||||||
|     t.pass("tests/events/ui/10-content-wildcard.rs"); |     t.pass("tests/events/ui/10-content-wildcard.rs"); | ||||||
|     t.pass("tests/events/ui/11-content-without-relation-sanity-check.rs"); |     t.pass("tests/events/ui/11-content-without-relation-sanity-check.rs"); | ||||||
|     t.compile_fail("tests/events/ui/12-no-relates_to.rs"); |     t.compile_fail("tests/events/ui/12-no-relates_to.rs"); | ||||||
|  |     t.pass("tests/events/ui/13-private-event-content-type.rs"); | ||||||
| } | } | ||||||
|  | |||||||
| @ -0,0 +1,12 @@ | |||||||
|  | #![deny(private_in_public)] | ||||||
|  | 
 | ||||||
|  | use ruma_macros::EventContent; | ||||||
|  | use serde::{Deserialize, Serialize}; | ||||||
|  | 
 | ||||||
|  | #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] | ||||||
|  | #[ruma_event(type = "m.macro.test", kind = State, state_key_type = String)] | ||||||
|  | struct MacroTestContent { | ||||||
|  |     url: String, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn main() {} | ||||||
| @ -312,6 +312,7 @@ pub fn expand_event_content( | |||||||
|         event_kind.filter(|kind| needs_redacted(is_custom_redacted, *kind)).map(|kind| { |         event_kind.filter(|kind| needs_redacted(is_custom_redacted, *kind)).map(|kind| { | ||||||
|             generate_redacted_event_content( |             generate_redacted_event_content( | ||||||
|                 ident, |                 ident, | ||||||
|  |                 &input.vis, | ||||||
|                 fields.clone(), |                 fields.clone(), | ||||||
|                 &event_type, |                 &event_type, | ||||||
|                 kind, |                 kind, | ||||||
| @ -329,6 +330,7 @@ pub fn expand_event_content( | |||||||
|         .map(|_| { |         .map(|_| { | ||||||
|             generate_possibly_redacted_event_content( |             generate_possibly_redacted_event_content( | ||||||
|                 ident, |                 ident, | ||||||
|  |                 &input.vis, | ||||||
|                 fields.clone(), |                 fields.clone(), | ||||||
|                 &event_type, |                 &event_type, | ||||||
|                 state_key_type.as_ref(), |                 state_key_type.as_ref(), | ||||||
| @ -340,12 +342,13 @@ pub fn expand_event_content( | |||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|     let event_content_without_relation = has_without_relation.then(|| { |     let event_content_without_relation = has_without_relation.then(|| { | ||||||
|         generate_event_content_without_relation(ident, fields.clone(), ruma_common) |         generate_event_content_without_relation(ident, &input.vis, fields.clone(), ruma_common) | ||||||
|             .unwrap_or_else(syn::Error::into_compile_error) |             .unwrap_or_else(syn::Error::into_compile_error) | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     let event_content_impl = generate_event_content_impl( |     let event_content_impl = generate_event_content_impl( | ||||||
|         ident, |         ident, | ||||||
|  |         &input.vis, | ||||||
|         fields, |         fields, | ||||||
|         &event_type, |         &event_type, | ||||||
|         event_kind, |         event_kind, | ||||||
| @ -359,7 +362,7 @@ pub fn expand_event_content( | |||||||
|     let static_event_content_impl = |     let static_event_content_impl = | ||||||
|         generate_static_event_content_impl(ident, &event_type, ruma_common); |         generate_static_event_content_impl(ident, &event_type, ruma_common); | ||||||
|     let type_aliases = event_kind.map(|k| { |     let type_aliases = event_kind.map(|k| { | ||||||
|         generate_event_type_aliases(k, ident, &event_type.value(), ruma_common) |         generate_event_type_aliases(k, ident, &input.vis, &event_type.value(), ruma_common) | ||||||
|             .unwrap_or_else(syn::Error::into_compile_error) |             .unwrap_or_else(syn::Error::into_compile_error) | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
| @ -375,6 +378,7 @@ pub fn expand_event_content( | |||||||
| 
 | 
 | ||||||
| fn generate_redacted_event_content<'a>( | fn generate_redacted_event_content<'a>( | ||||||
|     ident: &Ident, |     ident: &Ident, | ||||||
|  |     vis: &syn::Visibility, | ||||||
|     fields: impl Iterator<Item = &'a Field>, |     fields: impl Iterator<Item = &'a Field>, | ||||||
|     event_type: &LitStr, |     event_type: &LitStr, | ||||||
|     event_kind: EventKind, |     event_kind: EventKind, | ||||||
| @ -430,7 +434,7 @@ fn generate_redacted_event_content<'a>( | |||||||
|         quote! { |         quote! { | ||||||
|             impl #redacted_ident { |             impl #redacted_ident { | ||||||
|                 #[doc = #doc] |                 #[doc = #doc] | ||||||
|                 pub fn new() -> Self { |                 #vis fn new() -> Self { | ||||||
|                     Self {} |                     Self {} | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
| @ -439,6 +443,7 @@ fn generate_redacted_event_content<'a>( | |||||||
| 
 | 
 | ||||||
|     let redacted_event_content = generate_event_content_impl( |     let redacted_event_content = generate_event_content_impl( | ||||||
|         &redacted_ident, |         &redacted_ident, | ||||||
|  |         vis, | ||||||
|         kept_redacted_fields.iter(), |         kept_redacted_fields.iter(), | ||||||
|         event_type, |         event_type, | ||||||
|         Some(event_kind), |         Some(event_kind), | ||||||
| @ -469,7 +474,7 @@ fn generate_redacted_event_content<'a>( | |||||||
|         #[doc = #doc] |         #[doc = #doc] | ||||||
|         #[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)] |         #[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)] | ||||||
|         #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] |         #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] | ||||||
|         pub struct #redacted_ident { |         #vis struct #redacted_ident { | ||||||
|             #( #kept_redacted_fields, )* |             #( #kept_redacted_fields, )* | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| @ -483,6 +488,7 @@ fn generate_redacted_event_content<'a>( | |||||||
| 
 | 
 | ||||||
| fn generate_possibly_redacted_event_content<'a>( | fn generate_possibly_redacted_event_content<'a>( | ||||||
|     ident: &Ident, |     ident: &Ident, | ||||||
|  |     vis: &syn::Visibility, | ||||||
|     fields: impl Iterator<Item = &'a Field>, |     fields: impl Iterator<Item = &'a Field>, | ||||||
|     event_type: &LitStr, |     event_type: &LitStr, | ||||||
|     state_key_type: Option<&TokenStream>, |     state_key_type: Option<&TokenStream>, | ||||||
| @ -590,6 +596,7 @@ fn generate_possibly_redacted_event_content<'a>( | |||||||
|     if field_changed { |     if field_changed { | ||||||
|         let possibly_redacted_event_content = generate_event_content_impl( |         let possibly_redacted_event_content = generate_event_content_impl( | ||||||
|             &possibly_redacted_ident, |             &possibly_redacted_ident, | ||||||
|  |             vis, | ||||||
|             possibly_redacted_fields.iter(), |             possibly_redacted_fields.iter(), | ||||||
|             event_type, |             event_type, | ||||||
|             Some(EventKind::State), |             Some(EventKind::State), | ||||||
| @ -608,7 +615,7 @@ fn generate_possibly_redacted_event_content<'a>( | |||||||
|             #[doc = #doc] |             #[doc = #doc] | ||||||
|             #[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)] |             #[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)] | ||||||
|             #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] |             #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] | ||||||
|             pub struct #possibly_redacted_ident { |             #vis struct #possibly_redacted_ident { | ||||||
|                 #( #possibly_redacted_fields, )* |                 #( #possibly_redacted_fields, )* | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
| @ -619,7 +626,7 @@ fn generate_possibly_redacted_event_content<'a>( | |||||||
|     } else { |     } else { | ||||||
|         Ok(quote! { |         Ok(quote! { | ||||||
|             #[doc = #doc] |             #[doc = #doc] | ||||||
|             pub type #possibly_redacted_ident = #ident; |             #vis type #possibly_redacted_ident = #ident; | ||||||
| 
 | 
 | ||||||
|             #[automatically_derived] |             #[automatically_derived] | ||||||
|             impl #ruma_common::events::PossiblyRedactedStateEventContent for #ident { |             impl #ruma_common::events::PossiblyRedactedStateEventContent for #ident { | ||||||
| @ -631,6 +638,7 @@ fn generate_possibly_redacted_event_content<'a>( | |||||||
| 
 | 
 | ||||||
| fn generate_event_content_without_relation<'a>( | fn generate_event_content_without_relation<'a>( | ||||||
|     ident: &Ident, |     ident: &Ident, | ||||||
|  |     vis: &syn::Visibility, | ||||||
|     fields: impl Iterator<Item = &'a Field>, |     fields: impl Iterator<Item = &'a Field>, | ||||||
|     ruma_common: &TokenStream, |     ruma_common: &TokenStream, | ||||||
| ) -> syn::Result<TokenStream> { | ) -> syn::Result<TokenStream> { | ||||||
| @ -679,11 +687,11 @@ fn generate_event_content_without_relation<'a>( | |||||||
|         #[doc = #type_doc] |         #[doc = #type_doc] | ||||||
|         #[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)] |         #[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)] | ||||||
|         #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] |         #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] | ||||||
|         pub struct #without_relation_ident #without_relation_struct |         #vis struct #without_relation_ident #without_relation_struct | ||||||
| 
 | 
 | ||||||
|         impl #without_relation_ident { |         impl #without_relation_ident { | ||||||
|             #[doc = #with_relation_fn_doc] |             #[doc = #with_relation_fn_doc] | ||||||
|             pub fn with_relation(self, relates_to: #relates_to_type) -> #ident { |             #vis fn with_relation(self, relates_to: #relates_to_type) -> #ident { | ||||||
|                 #ident { |                 #ident { | ||||||
|                     #( #without_relation_fields: self.#without_relation_fields, )* |                     #( #without_relation_fields: self.#without_relation_fields, )* | ||||||
|                     relates_to, |                     relates_to, | ||||||
| @ -696,6 +704,7 @@ fn generate_event_content_without_relation<'a>( | |||||||
| fn generate_event_type_aliases( | fn generate_event_type_aliases( | ||||||
|     event_kind: EventKind, |     event_kind: EventKind, | ||||||
|     ident: &Ident, |     ident: &Ident, | ||||||
|  |     vis: &syn::Visibility, | ||||||
|     event_type: &str, |     event_type: &str, | ||||||
|     ruma_common: &TokenStream, |     ruma_common: &TokenStream, | ||||||
| ) -> syn::Result<TokenStream> { | ) -> syn::Result<TokenStream> { | ||||||
| @ -748,7 +757,7 @@ fn generate_event_type_aliases( | |||||||
| 
 | 
 | ||||||
|         quote! { |         quote! { | ||||||
|             #[doc = #ev_type_doc] |             #[doc = #ev_type_doc] | ||||||
|             pub type #ev_type = #ruma_common::events::#ev_struct<#content_struct>; |             #vis type #ev_type = #ruma_common::events::#ev_struct<#content_struct>; | ||||||
|         } |         } | ||||||
|     }) |     }) | ||||||
|     .collect(); |     .collect(); | ||||||
| @ -775,6 +784,7 @@ impl fmt::Display for EventKindContentVariation { | |||||||
| 
 | 
 | ||||||
| fn generate_event_content_impl<'a>( | fn generate_event_content_impl<'a>( | ||||||
|     ident: &Ident, |     ident: &Ident, | ||||||
|  |     vis: &syn::Visibility, | ||||||
|     mut fields: impl Iterator<Item = &'a Field>, |     mut fields: impl Iterator<Item = &'a Field>, | ||||||
|     event_type: &LitStr, |     event_type: &LitStr, | ||||||
|     event_kind: Option<EventKind>, |     event_kind: Option<EventKind>, | ||||||
| @ -841,7 +851,7 @@ fn generate_event_content_impl<'a>( | |||||||
|             event_type_ty_decl = Some(quote! { |             event_type_ty_decl = Some(quote! { | ||||||
|                 /// Implementation detail, you don't need to care about this.
 |                 /// Implementation detail, you don't need to care about this.
 | ||||||
|                 #[doc(hidden)] |                 #[doc(hidden)] | ||||||
|                 pub struct #i { |                 #vis struct #i { | ||||||
|                     // Set to None for intended type, Some for a different one
 |                     // Set to None for intended type, Some for a different one
 | ||||||
|                     ty: ::std::option::Option<crate::PrivOwnedStr>, |                     ty: ::std::option::Option<crate::PrivOwnedStr>, | ||||||
|                 } |                 } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user