From 9c65a7dcae7eec6a48108f69cd3bfc3043715721 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 3 Feb 2023 13:08:28 +0100 Subject: [PATCH] events: Use input visibility for generated types in EventContent derive --- crates/ruma-common/CHANGELOG.md | 3 ++ .../ruma-common/tests/events/event_content.rs | 1 + .../ui/13-private-event-content-type.rs | 12 ++++++++ .../ruma-macros/src/events/event_content.rs | 30 ++++++++++++------- 4 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 crates/ruma-common/tests/events/ui/13-private-event-content-type.rs diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index 2eddf170..fb2847be 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -3,6 +3,9 @@ Improvements: - 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 diff --git a/crates/ruma-common/tests/events/event_content.rs b/crates/ruma-common/tests/events/event_content.rs index f8c4fa16..d0257993 100644 --- a/crates/ruma-common/tests/events/event_content.rs +++ b/crates/ruma-common/tests/events/event_content.rs @@ -7,4 +7,5 @@ fn ui() { t.pass("tests/events/ui/10-content-wildcard.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.pass("tests/events/ui/13-private-event-content-type.rs"); } diff --git a/crates/ruma-common/tests/events/ui/13-private-event-content-type.rs b/crates/ruma-common/tests/events/ui/13-private-event-content-type.rs new file mode 100644 index 00000000..d0dc7af5 --- /dev/null +++ b/crates/ruma-common/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() {} diff --git a/crates/ruma-macros/src/events/event_content.rs b/crates/ruma-macros/src/events/event_content.rs index aa5815c7..0bd35e03 100644 --- a/crates/ruma-macros/src/events/event_content.rs +++ b/crates/ruma-macros/src/events/event_content.rs @@ -312,6 +312,7 @@ pub fn expand_event_content( event_kind.filter(|kind| needs_redacted(is_custom_redacted, *kind)).map(|kind| { generate_redacted_event_content( ident, + &input.vis, fields.clone(), &event_type, kind, @@ -329,6 +330,7 @@ pub fn expand_event_content( .map(|_| { generate_possibly_redacted_event_content( ident, + &input.vis, fields.clone(), &event_type, state_key_type.as_ref(), @@ -340,12 +342,13 @@ pub fn expand_event_content( }); 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) }); let event_content_impl = generate_event_content_impl( ident, + &input.vis, fields, &event_type, event_kind, @@ -359,7 +362,7 @@ pub fn expand_event_content( let static_event_content_impl = generate_static_event_content_impl(ident, &event_type, ruma_common); 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) }); @@ -375,6 +378,7 @@ pub fn expand_event_content( fn generate_redacted_event_content<'a>( ident: &Ident, + vis: &syn::Visibility, fields: impl Iterator, event_type: &LitStr, event_kind: EventKind, @@ -430,7 +434,7 @@ fn generate_redacted_event_content<'a>( quote! { impl #redacted_ident { #[doc = #doc] - pub fn new() -> Self { + #vis fn new() -> Self { Self {} } } @@ -439,6 +443,7 @@ fn generate_redacted_event_content<'a>( let redacted_event_content = generate_event_content_impl( &redacted_ident, + vis, kept_redacted_fields.iter(), event_type, Some(event_kind), @@ -469,7 +474,7 @@ fn generate_redacted_event_content<'a>( #[doc = #doc] #[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] - pub struct #redacted_ident { + #vis struct #redacted_ident { #( #kept_redacted_fields, )* } @@ -483,6 +488,7 @@ fn generate_redacted_event_content<'a>( fn generate_possibly_redacted_event_content<'a>( ident: &Ident, + vis: &syn::Visibility, fields: impl Iterator, event_type: &LitStr, state_key_type: Option<&TokenStream>, @@ -590,6 +596,7 @@ fn generate_possibly_redacted_event_content<'a>( if field_changed { let possibly_redacted_event_content = generate_event_content_impl( &possibly_redacted_ident, + vis, possibly_redacted_fields.iter(), event_type, Some(EventKind::State), @@ -608,7 +615,7 @@ fn generate_possibly_redacted_event_content<'a>( #[doc = #doc] #[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] - pub struct #possibly_redacted_ident { + #vis struct #possibly_redacted_ident { #( #possibly_redacted_fields, )* } @@ -619,7 +626,7 @@ fn generate_possibly_redacted_event_content<'a>( } else { Ok(quote! { #[doc = #doc] - pub type #possibly_redacted_ident = #ident; + #vis type #possibly_redacted_ident = #ident; #[automatically_derived] 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>( ident: &Ident, + vis: &syn::Visibility, fields: impl Iterator, ruma_common: &TokenStream, ) -> syn::Result { @@ -679,11 +687,11 @@ fn generate_event_content_without_relation<'a>( #[doc = #type_doc] #[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)] #[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 { #[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 { #( #without_relation_fields: self.#without_relation_fields, )* relates_to, @@ -696,6 +704,7 @@ fn generate_event_content_without_relation<'a>( fn generate_event_type_aliases( event_kind: EventKind, ident: &Ident, + vis: &syn::Visibility, event_type: &str, ruma_common: &TokenStream, ) -> syn::Result { @@ -748,7 +757,7 @@ fn generate_event_type_aliases( quote! { #[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(); @@ -775,6 +784,7 @@ impl fmt::Display for EventKindContentVariation { fn generate_event_content_impl<'a>( ident: &Ident, + vis: &syn::Visibility, mut fields: impl Iterator, event_type: &LitStr, event_kind: Option, @@ -841,7 +851,7 @@ fn generate_event_content_impl<'a>( event_type_ty_decl = Some(quote! { /// Implementation detail, you don't need to care about this. #[doc(hidden)] - pub struct #i { + #vis struct #i { // Set to None for intended type, Some for a different one ty: ::std::option::Option, }