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.
This commit is contained in:
Devin Ragotzy 2020-08-11 14:32:58 -04:00 committed by Jonas Platte
parent 636cc503ed
commit cfe62f27d0
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
6 changed files with 45 additions and 9 deletions

View File

@ -16,6 +16,7 @@ version = "0.22.0-alpha.1"
syn = { version = "1.0.38", features = ["full"] } syn = { version = "1.0.38", features = ["full"] }
quote = "1.0.7" quote = "1.0.7"
proc-macro2 = "1.0.19" proc-macro2 = "1.0.19"
proc-macro-crate = "0.1.5"
[lib] [lib]
proc-macro = true proc-macro = true

View File

@ -40,7 +40,7 @@ pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
let deserialize_impl = expand_deserialize_event(&input, &var, &fields)?; 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); let eq_impl = expand_eq_ord_event(&input, &fields);
@ -345,6 +345,7 @@ fn expand_from_into(
kind: &EventKind, kind: &EventKind,
var: &EventKindVariation, var: &EventKindVariation,
fields: &[Field], fields: &[Field],
import_path: &TokenStream,
) -> Option<TokenStream> { ) -> Option<TokenStream> {
let ident = &input.ident; 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. /// Convert this sync event into a full event, one with a room_id field.
pub fn into_full_event( pub fn into_full_event(
self, self,
room_id: ::ruma_identifiers::RoomId, room_id: #import_path::exports::ruma_identifiers::RoomId,
) -> #full_struct #ty_gen { ) -> #full_struct #ty_gen {
let Self { #( #fields, )* } = self; let Self { #( #fields, )* } = self;
#full_struct { #full_struct {

View File

@ -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 // this is the non redacted event content's impl
impl #ident { impl #ident {
/// Transforms the full event content into a redacted content according to spec. /// 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, )* } #redacted_ident { #( #redaction_struct_fields: self.#redaction_struct_fields, )* }
} }
} }

View File

@ -226,7 +226,7 @@ fn expand_conversion_impl(
Some(quote! { Some(quote! {
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.
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 { match self {
#( #(
Self::#variants(event) => { Self::#variants(event) => {
@ -401,7 +401,7 @@ fn expand_redact(
pub fn redact( pub fn redact(
self, self,
redaction: #param, redaction: #param,
version: ::ruma_identifiers::RoomVersionId, version: #import_path::exports::ruma_identifiers::RoomVersionId,
) -> #redaction_enum { ) -> #redaction_enum {
match self { match self {
#( #(
@ -788,9 +788,9 @@ fn generate_accessor(
fn field_return_type(name: &str, var: &EventKindVariation) -> TokenStream { fn field_return_type(name: &str, var: &EventKindVariation) -> TokenStream {
match name { match name {
"origin_server_ts" => quote! { ::std::time::SystemTime }, "origin_server_ts" => quote! { ::std::time::SystemTime },
"room_id" => quote! { ::ruma_identifiers::RoomId }, "room_id" => quote! { #import_path::exports::ruma_identifiers::RoomId },
"event_id" => quote! { ::ruma_identifiers::EventId }, "event_id" => quote! { #import_path::exports::ruma_identifiers::EventId },
"sender" => quote! { ::ruma_identifiers::UserId }, "sender" => quote! { #import_path::exports::ruma_identifiers::UserId },
"state_key" => quote! { str }, "state_key" => quote! { str },
"unsigned" if &EventKindVariation::RedactedSync == var => { "unsigned" if &EventKindVariation::RedactedSync == var => {
quote! { ::ruma_events::RedactedSyncUnsigned } quote! { ::ruma_events::RedactedSyncUnsigned }

View File

@ -6,7 +6,10 @@
#![deny(missing_copy_implementations, missing_debug_implementations, missing_docs)] #![deny(missing_copy_implementations, missing_debug_implementations, missing_docs)]
use proc_macro::TokenStream; 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::{ use self::{
event::expand_event, event::expand_event,
@ -94,3 +97,15 @@ pub fn derive_state_event(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput); let input = parse_macro_input!(input as DeriveInput);
expand_event(input).unwrap_or_else(|err| err.to_compile_error()).into() 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 }
}
}

View File

@ -139,6 +139,25 @@ mod event_type;
// that expect `ruma_events` to exist in the prelude. // that expect `ruma_events` to exist in the prelude.
extern crate self as ruma_events; 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 call;
pub mod custom; pub mod custom;
pub mod direct; pub mod direct;