From cfe62f27d0a2804669f44bde94a0978b93e8a1c1 Mon Sep 17 00:00:00 2001 From: Devin Ragotzy Date: Tue, 11 Aug 2020 14:32:58 -0400 Subject: [PATCH] Re-export ruma-events-macros and deps in ruma-events This also adds the dependency and function to find crate name at macro expansion time. --- ruma-events-macros/Cargo.toml | 1 + ruma-events-macros/src/event.rs | 5 +++-- ruma-events-macros/src/event_content.rs | 2 +- ruma-events-macros/src/event_enum.rs | 10 +++++----- ruma-events-macros/src/lib.rs | 17 ++++++++++++++++- ruma-events/src/lib.rs | 19 +++++++++++++++++++ 6 files changed, 45 insertions(+), 9 deletions(-) diff --git a/ruma-events-macros/Cargo.toml b/ruma-events-macros/Cargo.toml index 7ed79a4f..710190fb 100644 --- a/ruma-events-macros/Cargo.toml +++ b/ruma-events-macros/Cargo.toml @@ -16,6 +16,7 @@ version = "0.22.0-alpha.1" syn = { version = "1.0.38", features = ["full"] } quote = "1.0.7" proc-macro2 = "1.0.19" +proc-macro-crate = "0.1.5" [lib] proc-macro = true diff --git a/ruma-events-macros/src/event.rs b/ruma-events-macros/src/event.rs index c56ce5ce..77e080d9 100644 --- a/ruma-events-macros/src/event.rs +++ b/ruma-events-macros/src/event.rs @@ -40,7 +40,7 @@ pub fn expand_event(input: DeriveInput) -> syn::Result { let deserialize_impl = expand_deserialize_event(&input, &var, &fields)?; - let conversion_impl = expand_from_into(&input, &kind, &var, &fields); + let conversion_impl = expand_from_into(&input, &kind, &var, &fields, &import_path); let eq_impl = expand_eq_ord_event(&input, &fields); @@ -345,6 +345,7 @@ fn expand_from_into( kind: &EventKind, var: &EventKindVariation, fields: &[Field], + import_path: &TokenStream, ) -> Option { let ident = &input.ident; @@ -379,7 +380,7 @@ fn expand_from_into( /// Convert this sync event into a full event, one with a room_id field. pub fn into_full_event( self, - room_id: ::ruma_identifiers::RoomId, + room_id: #import_path::exports::ruma_identifiers::RoomId, ) -> #full_struct #ty_gen { let Self { #( #fields, )* } = self; #full_struct { diff --git a/ruma-events-macros/src/event_content.rs b/ruma-events-macros/src/event_content.rs index 5cd86d3d..a0a69bb1 100644 --- a/ruma-events-macros/src/event_content.rs +++ b/ruma-events-macros/src/event_content.rs @@ -172,7 +172,7 @@ pub fn expand_event_content(input: &DeriveInput, emit_redacted: bool) -> syn::Re // this is the non redacted event content's impl impl #ident { /// Transforms the full event content into a redacted content according to spec. - pub fn redact(self, version: ::ruma_identifiers::RoomVersionId) -> #redacted_ident { + pub fn redact(self, version: #import_path::exports::ruma_identifiers::RoomVersionId) -> #redacted_ident { #redacted_ident { #( #redaction_struct_fields: self.#redaction_struct_fields, )* } } } diff --git a/ruma-events-macros/src/event_enum.rs b/ruma-events-macros/src/event_enum.rs index c344cefc..ac785c98 100644 --- a/ruma-events-macros/src/event_enum.rs +++ b/ruma-events-macros/src/event_enum.rs @@ -226,7 +226,7 @@ fn expand_conversion_impl( Some(quote! { impl #ident { /// Convert this sync event into a full event, one with a room_id field. - pub fn into_full_event(self, room_id: ::ruma_identifiers::RoomId) -> #full { + pub fn into_full_event(self, room_id: #import_path::exports::ruma_identifiers::RoomId) -> #full { match self { #( Self::#variants(event) => { @@ -401,7 +401,7 @@ fn expand_redact( pub fn redact( self, redaction: #param, - version: ::ruma_identifiers::RoomVersionId, + version: #import_path::exports::ruma_identifiers::RoomVersionId, ) -> #redaction_enum { match self { #( @@ -788,9 +788,9 @@ fn generate_accessor( fn field_return_type(name: &str, var: &EventKindVariation) -> TokenStream { match name { "origin_server_ts" => quote! { ::std::time::SystemTime }, - "room_id" => quote! { ::ruma_identifiers::RoomId }, - "event_id" => quote! { ::ruma_identifiers::EventId }, - "sender" => quote! { ::ruma_identifiers::UserId }, + "room_id" => quote! { #import_path::exports::ruma_identifiers::RoomId }, + "event_id" => quote! { #import_path::exports::ruma_identifiers::EventId }, + "sender" => quote! { #import_path::exports::ruma_identifiers::UserId }, "state_key" => quote! { str }, "unsigned" if &EventKindVariation::RedactedSync == var => { quote! { ::ruma_events::RedactedSyncUnsigned } diff --git a/ruma-events-macros/src/lib.rs b/ruma-events-macros/src/lib.rs index 838b1887..6d7da773 100644 --- a/ruma-events-macros/src/lib.rs +++ b/ruma-events-macros/src/lib.rs @@ -6,7 +6,10 @@ #![deny(missing_copy_implementations, missing_debug_implementations, missing_docs)] use proc_macro::TokenStream; -use syn::{parse_macro_input, DeriveInput}; +use proc_macro2 as pm2; +use proc_macro_crate::crate_name; +use quote::quote; +use syn::{parse_macro_input, DeriveInput, Ident}; use self::{ event::expand_event, @@ -94,3 +97,15 @@ pub fn derive_state_event(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); expand_event(input).unwrap_or_else(|err| err.to_compile_error()).into() } + +pub(crate) fn import_ruma_events() -> pm2::TokenStream { + if let Ok(possibly_renamed) = crate_name("ruma-events") { + let import = Ident::new(&possibly_renamed, pm2::Span::call_site()); + quote! { ::#import } + } else if let Ok(possibly_renamed) = crate_name("ruma") { + let import = Ident::new(&possibly_renamed, pm2::Span::call_site()); + quote! { ::#import::events } + } else { + quote! { ::ruma_events } + } +} diff --git a/ruma-events/src/lib.rs b/ruma-events/src/lib.rs index 5935b830..30a5ba60 100644 --- a/ruma-events/src/lib.rs +++ b/ruma-events/src/lib.rs @@ -139,6 +139,25 @@ mod event_type; // that expect `ruma_events` to exist in the prelude. extern crate self as ruma_events; +/// Re-exports to allow users to declare their own event types using the +/// macros used internally. +/// +/// It is not considered part of ruma-events' public API. +#[doc(hidden)] +pub mod exports { + pub use js_int; + pub use ruma_identifiers; + 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_events_macros::{ + BasicEventContent, EphemeralRoomEventContent, Event, MessageEventContent, StateEventContent, + }; +} + pub mod call; pub mod custom; pub mod direct;