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:
parent
636cc503ed
commit
cfe62f27d0
@ -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
|
||||
|
@ -40,7 +40,7 @@ pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||
|
||||
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<TokenStream> {
|
||||
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 {
|
||||
|
@ -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, )* }
|
||||
}
|
||||
}
|
||||
|
@ -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 }
|
||||
|
@ -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 }
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user