Use proc_macro_crate name in the event content derives
This commit is contained in:
parent
9bc257b19e
commit
e2f84679b3
@ -76,7 +76,11 @@ impl Parse for MetaAttrs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create an `EventContent` implementation for a struct.
|
/// Create an `EventContent` implementation for a struct.
|
||||||
pub fn expand_event_content(input: &DeriveInput, emit_redacted: bool) -> syn::Result<TokenStream> {
|
pub fn expand_event_content(
|
||||||
|
input: &DeriveInput,
|
||||||
|
emit_redacted: bool,
|
||||||
|
import_path: &TokenStream,
|
||||||
|
) -> syn::Result<TokenStream> {
|
||||||
let ident = &input.ident;
|
let ident = &input.ident;
|
||||||
|
|
||||||
let content_attr = input
|
let content_attr = input
|
||||||
@ -147,7 +151,7 @@ pub fn expand_event_content(input: &DeriveInput, emit_redacted: bool) -> syn::Re
|
|||||||
{ #( #kept_redacted_fields, )* }
|
{ #( #kept_redacted_fields, )* }
|
||||||
},
|
},
|
||||||
quote! {
|
quote! {
|
||||||
Err(::serde::de::Error::custom(
|
Err(#import_path::exports::serde::de::Error::custom(
|
||||||
format!("this redacted event has fields that cannot be constructed")
|
format!("this redacted event has fields that cannot be constructed")
|
||||||
))
|
))
|
||||||
},
|
},
|
||||||
@ -155,9 +159,9 @@ pub fn expand_event_content(input: &DeriveInput, emit_redacted: bool) -> syn::Re
|
|||||||
};
|
};
|
||||||
|
|
||||||
let has_deserialize_fields = if kept_redacted_fields.is_empty() {
|
let has_deserialize_fields = if kept_redacted_fields.is_empty() {
|
||||||
quote! { ::ruma_events::HasDeserializeFields::False }
|
quote! { #import_path::HasDeserializeFields::False }
|
||||||
} else {
|
} else {
|
||||||
quote! { ::ruma_events::HasDeserializeFields::True }
|
quote! { #import_path::HasDeserializeFields::True }
|
||||||
};
|
};
|
||||||
|
|
||||||
let has_serialize_fields = if kept_redacted_fields.is_empty() {
|
let has_serialize_fields = if kept_redacted_fields.is_empty() {
|
||||||
@ -166,7 +170,8 @@ pub fn expand_event_content(input: &DeriveInput, emit_redacted: bool) -> syn::Re
|
|||||||
quote! { true }
|
quote! { true }
|
||||||
};
|
};
|
||||||
|
|
||||||
let redacted_event_content = generate_event_content_impl(&redacted_ident, event_type);
|
let redacted_event_content =
|
||||||
|
generate_event_content_impl(&redacted_ident, event_type, import_path);
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
// this is the non redacted event content's impl
|
// this is the non redacted event content's impl
|
||||||
@ -178,15 +183,20 @@ pub fn expand_event_content(input: &DeriveInput, emit_redacted: bool) -> syn::Re
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[doc = #doc]
|
#[doc = #doc]
|
||||||
#[derive(Clone, Debug, ::serde::Deserialize, ::serde::Serialize)]
|
#[derive(
|
||||||
|
Clone,
|
||||||
|
Debug,
|
||||||
|
#import_path::exports::serde::Deserialize,
|
||||||
|
#import_path::exports::serde::Serialize
|
||||||
|
)]
|
||||||
pub struct #redacted_ident #redacted_fields
|
pub struct #redacted_ident #redacted_fields
|
||||||
|
|
||||||
#redacted_event_content
|
#redacted_event_content
|
||||||
|
|
||||||
impl ::ruma_events::RedactedEventContent for #redacted_ident {
|
impl #import_path::RedactedEventContent for #redacted_ident {
|
||||||
fn empty(ev_type: &str) -> Result<Self, ::serde_json::Error> {
|
fn empty(ev_type: &str) -> Result<Self, #import_path::exports::serde_json::Error> {
|
||||||
if ev_type != #event_type {
|
if ev_type != #event_type {
|
||||||
return Err(::serde::de::Error::custom(
|
return Err(#import_path::exports::serde::de::Error::custom(
|
||||||
format!("expected event type `{}`, found `{}`", #event_type, ev_type)
|
format!("expected event type `{}`, found `{}`", #event_type, ev_type)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -198,7 +208,7 @@ pub fn expand_event_content(input: &DeriveInput, emit_redacted: bool) -> syn::Re
|
|||||||
#has_serialize_fields
|
#has_serialize_fields
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_deserialize_fields() -> ::ruma_events::HasDeserializeFields {
|
fn has_deserialize_fields() -> #import_path::HasDeserializeFields {
|
||||||
#has_deserialize_fields
|
#has_deserialize_fields
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -207,7 +217,7 @@ pub fn expand_event_content(input: &DeriveInput, emit_redacted: bool) -> syn::Re
|
|||||||
TokenStream::new()
|
TokenStream::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
let event_content = generate_event_content_impl(ident, event_type);
|
let event_content = generate_event_content_impl(ident, event_type, import_path);
|
||||||
|
|
||||||
Ok(quote! {
|
Ok(quote! {
|
||||||
#event_content
|
#event_content
|
||||||
@ -217,50 +227,62 @@ pub fn expand_event_content(input: &DeriveInput, emit_redacted: bool) -> syn::Re
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a `BasicEventContent` implementation for a struct
|
/// Create a `BasicEventContent` implementation for a struct
|
||||||
pub fn expand_basic_event_content(input: &DeriveInput) -> syn::Result<TokenStream> {
|
pub fn expand_basic_event_content(
|
||||||
|
input: &DeriveInput,
|
||||||
|
import_path: &TokenStream,
|
||||||
|
) -> syn::Result<TokenStream> {
|
||||||
let ident = input.ident.clone();
|
let ident = input.ident.clone();
|
||||||
let event_content_impl = expand_event_content(input, false)?;
|
let event_content_impl = expand_event_content(input, false, import_path)?;
|
||||||
|
|
||||||
Ok(quote! {
|
Ok(quote! {
|
||||||
#event_content_impl
|
#event_content_impl
|
||||||
|
|
||||||
impl ::ruma_events::BasicEventContent for #ident { }
|
impl #import_path::BasicEventContent for #ident { }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a `EphemeralRoomEventContent` implementation for a struct
|
/// Create a `EphemeralRoomEventContent` implementation for a struct
|
||||||
pub fn expand_ephemeral_room_event_content(input: &DeriveInput) -> syn::Result<TokenStream> {
|
pub fn expand_ephemeral_room_event_content(
|
||||||
|
input: &DeriveInput,
|
||||||
|
import_path: &TokenStream,
|
||||||
|
) -> syn::Result<TokenStream> {
|
||||||
let ident = input.ident.clone();
|
let ident = input.ident.clone();
|
||||||
let event_content_impl = expand_event_content(input, false)?;
|
let event_content_impl = expand_event_content(input, false, import_path)?;
|
||||||
|
|
||||||
Ok(quote! {
|
Ok(quote! {
|
||||||
#event_content_impl
|
#event_content_impl
|
||||||
|
|
||||||
impl ::ruma_events::EphemeralRoomEventContent for #ident { }
|
impl #import_path::EphemeralRoomEventContent for #ident { }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a `RoomEventContent` implementation for a struct.
|
/// Create a `RoomEventContent` implementation for a struct.
|
||||||
pub fn expand_room_event_content(input: &DeriveInput) -> syn::Result<TokenStream> {
|
pub fn expand_room_event_content(
|
||||||
|
input: &DeriveInput,
|
||||||
|
import_path: &TokenStream,
|
||||||
|
) -> syn::Result<TokenStream> {
|
||||||
let ident = input.ident.clone();
|
let ident = input.ident.clone();
|
||||||
let event_content_impl = expand_event_content(input, true)?;
|
let event_content_impl = expand_event_content(input, true, import_path)?;
|
||||||
|
|
||||||
Ok(quote! {
|
Ok(quote! {
|
||||||
#event_content_impl
|
#event_content_impl
|
||||||
|
|
||||||
impl ::ruma_events::RoomEventContent for #ident { }
|
impl #import_path::RoomEventContent for #ident { }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a `MessageEventContent` implementation for a struct
|
/// Create a `MessageEventContent` implementation for a struct
|
||||||
pub fn expand_message_event_content(input: &DeriveInput) -> syn::Result<TokenStream> {
|
pub fn expand_message_event_content(
|
||||||
|
input: &DeriveInput,
|
||||||
|
import_path: &TokenStream,
|
||||||
|
) -> syn::Result<TokenStream> {
|
||||||
let ident = input.ident.clone();
|
let ident = input.ident.clone();
|
||||||
let room_ev_content = expand_room_event_content(input)?;
|
let room_ev_content = expand_room_event_content(input, import_path)?;
|
||||||
|
|
||||||
let redacted_marker_trait = if needs_redacted_from_input(input) {
|
let redacted_marker_trait = if needs_redacted_from_input(input) {
|
||||||
let ident = format_ident!("Redacted{}", &ident);
|
let ident = format_ident!("Redacted{}", &ident);
|
||||||
quote! {
|
quote! {
|
||||||
impl ::ruma_events::RedactedMessageEventContent for #ident { }
|
impl #import_path::RedactedMessageEventContent for #ident { }
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
TokenStream::new()
|
TokenStream::new()
|
||||||
@ -269,21 +291,24 @@ pub fn expand_message_event_content(input: &DeriveInput) -> syn::Result<TokenStr
|
|||||||
Ok(quote! {
|
Ok(quote! {
|
||||||
#room_ev_content
|
#room_ev_content
|
||||||
|
|
||||||
impl ::ruma_events::MessageEventContent for #ident { }
|
impl #import_path::MessageEventContent for #ident { }
|
||||||
|
|
||||||
#redacted_marker_trait
|
#redacted_marker_trait
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a `StateEventContent` implementation for a struct
|
/// Create a `StateEventContent` implementation for a struct
|
||||||
pub fn expand_state_event_content(input: &DeriveInput) -> syn::Result<TokenStream> {
|
pub fn expand_state_event_content(
|
||||||
|
input: &DeriveInput,
|
||||||
|
import_path: &TokenStream,
|
||||||
|
) -> syn::Result<TokenStream> {
|
||||||
let ident = input.ident.clone();
|
let ident = input.ident.clone();
|
||||||
let room_ev_content = expand_room_event_content(input)?;
|
let room_ev_content = expand_room_event_content(input, import_path)?;
|
||||||
|
|
||||||
let redacted_marker_trait = if needs_redacted_from_input(input) {
|
let redacted_marker_trait = if needs_redacted_from_input(input) {
|
||||||
let ident = format_ident!("Redacted{}", input.ident);
|
let ident = format_ident!("Redacted{}", input.ident);
|
||||||
quote! {
|
quote! {
|
||||||
impl ::ruma_events::RedactedStateEventContent for #ident { }
|
impl #import_path::RedactedStateEventContent for #ident { }
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
TokenStream::new()
|
TokenStream::new()
|
||||||
@ -292,30 +317,34 @@ pub fn expand_state_event_content(input: &DeriveInput) -> syn::Result<TokenStrea
|
|||||||
Ok(quote! {
|
Ok(quote! {
|
||||||
#room_ev_content
|
#room_ev_content
|
||||||
|
|
||||||
impl ::ruma_events::StateEventContent for #ident { }
|
impl #import_path::StateEventContent for #ident { }
|
||||||
|
|
||||||
#redacted_marker_trait
|
#redacted_marker_trait
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_event_content_impl(ident: &Ident, event_type: &LitStr) -> TokenStream {
|
fn generate_event_content_impl(
|
||||||
|
ident: &Ident,
|
||||||
|
event_type: &LitStr,
|
||||||
|
import_path: &TokenStream,
|
||||||
|
) -> TokenStream {
|
||||||
quote! {
|
quote! {
|
||||||
impl ::ruma_events::EventContent for #ident {
|
impl #import_path::EventContent for #ident {
|
||||||
fn event_type(&self) -> &str {
|
fn event_type(&self) -> &str {
|
||||||
#event_type
|
#event_type
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_parts(
|
fn from_parts(
|
||||||
ev_type: &str,
|
ev_type: &str,
|
||||||
content: Box<::serde_json::value::RawValue>
|
content: Box<#import_path::exports::serde_json::value::RawValue>
|
||||||
) -> Result<Self, ::serde_json::Error> {
|
) -> Result<Self, #import_path::exports::serde_json::Error> {
|
||||||
if ev_type != #event_type {
|
if ev_type != #event_type {
|
||||||
return Err(::serde::de::Error::custom(
|
return Err(#import_path::exports::serde::de::Error::custom(
|
||||||
format!("expected event type `{}`, found `{}`", #event_type, ev_type)
|
format!("expected event type `{}`, found `{}`", #event_type, ev_type)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
::serde_json::from_str(content.get())
|
#import_path::exports::serde_json::from_str(content.get())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,43 +52,67 @@ pub fn event_enum(input: TokenStream) -> TokenStream {
|
|||||||
/// Generates an implementation of `ruma_events::EventContent`.
|
/// Generates an implementation of `ruma_events::EventContent`.
|
||||||
#[proc_macro_derive(EventContent, attributes(ruma_event))]
|
#[proc_macro_derive(EventContent, attributes(ruma_event))]
|
||||||
pub fn derive_event_content(input: TokenStream) -> TokenStream {
|
pub fn derive_event_content(input: TokenStream) -> TokenStream {
|
||||||
|
let import_path = import_ruma_events();
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
expand_event_content(&input, true).unwrap_or_else(|err| err.to_compile_error()).into()
|
|
||||||
|
expand_event_content(&input, true, &import_path)
|
||||||
|
.unwrap_or_else(|err| err.to_compile_error())
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates an implementation of `ruma_events::BasicEventContent` and it's super traits.
|
/// Generates an implementation of `ruma_events::BasicEventContent` and it's super traits.
|
||||||
#[proc_macro_derive(BasicEventContent, attributes(ruma_event))]
|
#[proc_macro_derive(BasicEventContent, attributes(ruma_event))]
|
||||||
pub fn derive_basic_event_content(input: TokenStream) -> TokenStream {
|
pub fn derive_basic_event_content(input: TokenStream) -> TokenStream {
|
||||||
|
let import_path = import_ruma_events();
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
expand_basic_event_content(&input).unwrap_or_else(|err| err.to_compile_error()).into()
|
|
||||||
|
expand_basic_event_content(&input, &import_path)
|
||||||
|
.unwrap_or_else(|err| err.to_compile_error())
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates an implementation of `ruma_events::RoomEventContent` and it's super traits.
|
/// Generates an implementation of `ruma_events::RoomEventContent` and it's super traits.
|
||||||
#[proc_macro_derive(RoomEventContent, attributes(ruma_event))]
|
#[proc_macro_derive(RoomEventContent, attributes(ruma_event))]
|
||||||
pub fn derive_room_event_content(input: TokenStream) -> TokenStream {
|
pub fn derive_room_event_content(input: TokenStream) -> TokenStream {
|
||||||
|
let import_path = import_ruma_events();
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
expand_room_event_content(&input).unwrap_or_else(|err| err.to_compile_error()).into()
|
|
||||||
|
expand_room_event_content(&input, &import_path)
|
||||||
|
.unwrap_or_else(|err| err.to_compile_error())
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates an implementation of `ruma_events::MessageEventContent` and it's super traits.
|
/// Generates an implementation of `ruma_events::MessageEventContent` and it's super traits.
|
||||||
#[proc_macro_derive(MessageEventContent, attributes(ruma_event))]
|
#[proc_macro_derive(MessageEventContent, attributes(ruma_event))]
|
||||||
pub fn derive_message_event_content(input: TokenStream) -> TokenStream {
|
pub fn derive_message_event_content(input: TokenStream) -> TokenStream {
|
||||||
|
let import_path = import_ruma_events();
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
expand_message_event_content(&input).unwrap_or_else(|err| err.to_compile_error()).into()
|
|
||||||
|
expand_message_event_content(&input, &import_path)
|
||||||
|
.unwrap_or_else(|err| err.to_compile_error())
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates an implementation of `ruma_events::StateEventContent` and it's super traits.
|
/// Generates an implementation of `ruma_events::StateEventContent` and it's super traits.
|
||||||
#[proc_macro_derive(StateEventContent, attributes(ruma_event))]
|
#[proc_macro_derive(StateEventContent, attributes(ruma_event))]
|
||||||
pub fn derive_state_event_content(input: TokenStream) -> TokenStream {
|
pub fn derive_state_event_content(input: TokenStream) -> TokenStream {
|
||||||
|
let import_path = import_ruma_events();
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
expand_state_event_content(&input).unwrap_or_else(|err| err.to_compile_error()).into()
|
|
||||||
|
expand_state_event_content(&input, &import_path)
|
||||||
|
.unwrap_or_else(|err| err.to_compile_error())
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates an implementation of `ruma_events::EphemeralRoomEventContent` and it's super traits.
|
/// Generates an implementation of `ruma_events::EphemeralRoomEventContent` and it's super traits.
|
||||||
#[proc_macro_derive(EphemeralRoomEventContent, attributes(ruma_event))]
|
#[proc_macro_derive(EphemeralRoomEventContent, attributes(ruma_event))]
|
||||||
pub fn derive_ephemeral_room_event_content(input: TokenStream) -> TokenStream {
|
pub fn derive_ephemeral_room_event_content(input: TokenStream) -> TokenStream {
|
||||||
|
let import_path = import_ruma_events();
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
expand_ephemeral_room_event_content(&input).unwrap_or_else(|err| err.to_compile_error()).into()
|
|
||||||
|
expand_ephemeral_room_event_content(&input, &import_path)
|
||||||
|
.unwrap_or_else(|err| err.to_compile_error())
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates implementations needed to serialize and deserialize Matrix events.
|
/// Generates implementations needed to serialize and deserialize Matrix events.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user