events-macros: Improve readability, fix lines >100 chars
This commit is contained in:
parent
5175d3d622
commit
675efbc5f1
@ -11,7 +11,7 @@ use crate::{
|
||||
|
||||
/// Derive `Event` macro code generation.
|
||||
pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||
let import_path = import_ruma_events();
|
||||
let ruma_events = import_ruma_events();
|
||||
|
||||
let ident = &input.ident;
|
||||
let (kind, var) = to_kind_variation(ident).ok_or_else(|| {
|
||||
@ -41,19 +41,16 @@ pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||
));
|
||||
};
|
||||
|
||||
let serialize_impl = expand_serialize_event(&input, &var, &fields, &import_path);
|
||||
let deserialize_impl = expand_deserialize_event(&input, &var, &fields, &import_path);
|
||||
let conversion_impl = expand_from_into(&input, &kind, &var, &fields, &import_path);
|
||||
let serialize_impl = expand_serialize_event(&input, &var, &fields, &ruma_events);
|
||||
let deserialize_impl = expand_deserialize_event(&input, &var, &fields, &ruma_events);
|
||||
let conversion_impl = expand_from_into(&input, &kind, &var, &fields, &ruma_events);
|
||||
|
||||
let eq_impl = expand_eq_ord_event(&input, &fields);
|
||||
|
||||
Ok(quote! {
|
||||
#conversion_impl
|
||||
|
||||
#serialize_impl
|
||||
|
||||
#deserialize_impl
|
||||
|
||||
#eq_impl
|
||||
})
|
||||
}
|
||||
@ -62,8 +59,11 @@ fn expand_serialize_event(
|
||||
input: &DeriveInput,
|
||||
var: &EventKindVariation,
|
||||
fields: &[Field],
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> TokenStream {
|
||||
let js_int = quote! { #ruma_events::exports::js_int };
|
||||
let serde = quote! { #ruma_events::exports::serde };
|
||||
|
||||
let ident = &input.ident;
|
||||
let (impl_gen, ty_gen, where_clause) = input.generics.split_for_impl();
|
||||
let serialize_fields = fields
|
||||
@ -72,7 +72,7 @@ fn expand_serialize_event(
|
||||
let name = field.ident.as_ref().unwrap();
|
||||
if name == "content" && var.is_redacted() {
|
||||
quote! {
|
||||
if #import_path::RedactedEventContent::has_serialize_fields(&self.content) {
|
||||
if #ruma_events::RedactedEventContent::has_serialize_fields(&self.content) {
|
||||
state.serialize_field("content", &self.content)?;
|
||||
}
|
||||
}
|
||||
@ -87,7 +87,7 @@ fn expand_serialize_event(
|
||||
let time_since_epoch =
|
||||
self.origin_server_ts.duration_since(::std::time::UNIX_EPOCH).unwrap();
|
||||
|
||||
let timestamp = <#import_path::exports::js_int::UInt as ::std::convert::TryFrom<_>>::try_from(
|
||||
let timestamp = <#js_int::UInt as ::std::convert::TryFrom<_>>::try_from(
|
||||
time_since_epoch.as_millis(),
|
||||
).map_err(S::Error::custom)?;
|
||||
|
||||
@ -108,14 +108,14 @@ fn expand_serialize_event(
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
quote! {
|
||||
impl #impl_gen #import_path::exports::serde::ser::Serialize for #ident #ty_gen #where_clause {
|
||||
impl #impl_gen #serde::ser::Serialize for #ident #ty_gen #where_clause {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: #import_path::exports::serde::ser::Serializer,
|
||||
S: #serde::ser::Serializer,
|
||||
{
|
||||
use #import_path::exports::serde::ser::{SerializeStruct as _, Error as _};
|
||||
use #serde::ser::{SerializeStruct as _, Error as _};
|
||||
|
||||
let event_type = #import_path::EventContent::event_type(&self.content);
|
||||
let event_type = #ruma_events::EventContent::event_type(&self.content);
|
||||
|
||||
let mut state = serializer.serialize_struct(stringify!(#ident), 7)?;
|
||||
|
||||
@ -131,8 +131,12 @@ fn expand_deserialize_event(
|
||||
input: &DeriveInput,
|
||||
var: &EventKindVariation,
|
||||
fields: &[Field],
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> TokenStream {
|
||||
let js_int = quote! { #ruma_events::exports::js_int };
|
||||
let serde = quote! { #ruma_events::exports::serde };
|
||||
let serde_json = quote! { #ruma_events::exports::serde_json };
|
||||
|
||||
let ident = &input.ident;
|
||||
// we know there is a content field already
|
||||
let content_type = fields
|
||||
@ -160,12 +164,12 @@ fn expand_deserialize_event(
|
||||
let ty = &field.ty;
|
||||
if name == "content" || name == "prev_content" {
|
||||
if is_generic {
|
||||
quote! { Box<#import_path::exports::serde_json::value::RawValue> }
|
||||
quote! { Box<#serde_json::value::RawValue> }
|
||||
} else {
|
||||
quote! { #content_type }
|
||||
}
|
||||
} else if name == "origin_server_ts" {
|
||||
quote! { #import_path::exports::js_int::UInt }
|
||||
quote! { #js_int::UInt }
|
||||
} else {
|
||||
quote! { #ty }
|
||||
}
|
||||
@ -180,18 +184,18 @@ fn expand_deserialize_event(
|
||||
if is_generic && var.is_redacted() {
|
||||
quote! {
|
||||
let content = match C::has_deserialize_fields() {
|
||||
#import_path::HasDeserializeFields::False => {
|
||||
#ruma_events::HasDeserializeFields::False => {
|
||||
C::empty(&event_type).map_err(A::Error::custom)?
|
||||
},
|
||||
#import_path::HasDeserializeFields::True => {
|
||||
#ruma_events::HasDeserializeFields::True => {
|
||||
let json = content.ok_or_else(
|
||||
|| #import_path::exports::serde::de::Error::missing_field("content"),
|
||||
|| #serde::de::Error::missing_field("content"),
|
||||
)?;
|
||||
C::from_parts(&event_type, json).map_err(A::Error::custom)?
|
||||
},
|
||||
#import_path::HasDeserializeFields::Optional => {
|
||||
#ruma_events::HasDeserializeFields::Optional => {
|
||||
let json = content.unwrap_or(
|
||||
#import_path::exports::serde_json::value::RawValue::from_string("{}".to_string())
|
||||
#serde_json::value::RawValue::from_string("{}".to_string())
|
||||
.unwrap()
|
||||
);
|
||||
C::from_parts(&event_type, json).map_err(A::Error::custom)?
|
||||
@ -200,13 +204,14 @@ fn expand_deserialize_event(
|
||||
}
|
||||
} else if is_generic {
|
||||
quote! {
|
||||
let json = content.ok_or_else(|| #import_path::exports::serde::de::Error::missing_field("content"))?;
|
||||
let json =
|
||||
content.ok_or_else(|| #serde::de::Error::missing_field("content"))?;
|
||||
let content = C::from_parts(&event_type, json).map_err(A::Error::custom)?;
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
let content = content.ok_or_else(
|
||||
|| #import_path::exports::serde::de::Error::missing_field("content"),
|
||||
|| #serde::de::Error::missing_field("content"),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
@ -235,14 +240,14 @@ fn expand_deserialize_event(
|
||||
let t = time.into();
|
||||
::std::time::UNIX_EPOCH + ::std::time::Duration::from_millis(t)
|
||||
})
|
||||
.ok_or_else(|| #import_path::exports::serde::de::Error::missing_field("origin_server_ts"))?;
|
||||
.ok_or_else(|| #serde::de::Error::missing_field("origin_server_ts"))?;
|
||||
}
|
||||
} else if name == "unsigned" {
|
||||
quote! { let unsigned = unsigned.unwrap_or_default(); }
|
||||
} else {
|
||||
quote! {
|
||||
let #name = #name.ok_or_else(|| {
|
||||
#import_path::exports::serde::de::Error::missing_field(stringify!(#name))
|
||||
#serde::de::Error::missing_field(stringify!(#name))
|
||||
})?;
|
||||
}
|
||||
}
|
||||
@ -264,12 +269,12 @@ fn expand_deserialize_event(
|
||||
};
|
||||
|
||||
quote! {
|
||||
impl #deserialize_impl_gen #import_path::exports::serde::de::Deserialize<'de> for #ident #ty_gen #where_clause {
|
||||
impl #deserialize_impl_gen #serde::de::Deserialize<'de> for #ident #ty_gen #where_clause {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: #import_path::exports::serde::de::Deserializer<'de>,
|
||||
D: #serde::de::Deserializer<'de>,
|
||||
{
|
||||
#[derive(#import_path::exports::serde::Deserialize)]
|
||||
#[derive(#serde::Deserialize)]
|
||||
#[serde(field_identifier, rename_all = "snake_case")]
|
||||
enum Field {
|
||||
// since this is represented as an enum we have to add it so the JSON picks it
|
||||
@ -284,7 +289,7 @@ fn expand_deserialize_event(
|
||||
/// the `content` and `prev_content` fields.
|
||||
struct EventVisitor #impl_generics (#deserialize_phantom_type #ty_gen);
|
||||
|
||||
impl #deserialize_impl_gen #import_path::exports::serde::de::Visitor<'de>
|
||||
impl #deserialize_impl_gen #serde::de::Visitor<'de>
|
||||
for EventVisitor #ty_gen #where_clause
|
||||
{
|
||||
type Value = #ident #ty_gen;
|
||||
@ -298,9 +303,9 @@ fn expand_deserialize_event(
|
||||
|
||||
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
||||
where
|
||||
A: #import_path::exports::serde::de::MapAccess<'de>,
|
||||
A: #serde::de::MapAccess<'de>,
|
||||
{
|
||||
use #import_path::exports::serde::de::Error as _;
|
||||
use #serde::de::Error as _;
|
||||
|
||||
let mut event_type: Option<String> = None;
|
||||
#( let mut #field_names: Option<#deserialize_var_types> = None; )*
|
||||
@ -308,18 +313,18 @@ fn expand_deserialize_event(
|
||||
while let Some(key) = map.next_key()? {
|
||||
match key {
|
||||
Field::Unknown => {
|
||||
let _: #import_path::exports::serde::de::IgnoredAny = map.next_value()?;
|
||||
let _: #serde::de::IgnoredAny = map.next_value()?;
|
||||
},
|
||||
Field::Type => {
|
||||
if event_type.is_some() {
|
||||
return Err(#import_path::exports::serde::de::Error::duplicate_field("type"));
|
||||
return Err(#serde::de::Error::duplicate_field("type"));
|
||||
}
|
||||
event_type = Some(map.next_value()?);
|
||||
}
|
||||
#(
|
||||
Field::#enum_variants => {
|
||||
if #field_names.is_some() {
|
||||
return Err(#import_path::exports::serde::de::Error::duplicate_field(
|
||||
return Err(#serde::de::Error::duplicate_field(
|
||||
stringify!(#field_names),
|
||||
));
|
||||
}
|
||||
@ -330,7 +335,7 @@ fn expand_deserialize_event(
|
||||
}
|
||||
|
||||
let event_type =
|
||||
event_type.ok_or_else(|| #import_path::exports::serde::de::Error::missing_field("type"))?;
|
||||
event_type.ok_or_else(|| #serde::de::Error::missing_field("type"))?;
|
||||
#( #ok_or_else_fields )*
|
||||
|
||||
Ok(#ident {
|
||||
@ -350,8 +355,10 @@ fn expand_from_into(
|
||||
kind: &EventKind,
|
||||
var: &EventKindVariation,
|
||||
fields: &[Field],
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> Option<TokenStream> {
|
||||
let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers };
|
||||
|
||||
let ident = &input.ident;
|
||||
|
||||
let (impl_generics, ty_gen, where_clause) = input.generics.split_for_impl();
|
||||
@ -385,7 +392,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: #import_path::exports::ruma_identifiers::RoomId,
|
||||
room_id: #ruma_identifiers::RoomId,
|
||||
) -> #full_struct #ty_gen {
|
||||
let Self { #( #fields, )* } = self;
|
||||
#full_struct {
|
||||
|
@ -79,8 +79,12 @@ impl Parse for MetaAttrs {
|
||||
pub fn expand_event_content(
|
||||
input: &DeriveInput,
|
||||
emit_redacted: bool,
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> syn::Result<TokenStream> {
|
||||
let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers };
|
||||
let serde = quote! { #ruma_events::exports::serde };
|
||||
let serde_json = quote! { #ruma_events::exports::serde_json };
|
||||
|
||||
let ident = &input.ident;
|
||||
|
||||
let content_attr = input
|
||||
@ -151,7 +155,7 @@ pub fn expand_event_content(
|
||||
{ #( #kept_redacted_fields, )* }
|
||||
},
|
||||
quote! {
|
||||
Err(#import_path::exports::serde::de::Error::custom(
|
||||
Err(#serde::de::Error::custom(
|
||||
format!("this redacted event has fields that cannot be constructed")
|
||||
))
|
||||
},
|
||||
@ -159,9 +163,9 @@ pub fn expand_event_content(
|
||||
};
|
||||
|
||||
let has_deserialize_fields = if kept_redacted_fields.is_empty() {
|
||||
quote! { #import_path::HasDeserializeFields::False }
|
||||
quote! { #ruma_events::HasDeserializeFields::False }
|
||||
} else {
|
||||
quote! { #import_path::HasDeserializeFields::True }
|
||||
quote! { #ruma_events::HasDeserializeFields::True }
|
||||
};
|
||||
|
||||
let has_serialize_fields = if kept_redacted_fields.is_empty() {
|
||||
@ -171,32 +175,29 @@ pub fn expand_event_content(
|
||||
};
|
||||
|
||||
let redacted_event_content =
|
||||
generate_event_content_impl(&redacted_ident, event_type, import_path);
|
||||
generate_event_content_impl(&redacted_ident, event_type, ruma_events);
|
||||
|
||||
quote! {
|
||||
// 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: #import_path::exports::ruma_identifiers::RoomVersionId) -> #redacted_ident {
|
||||
#redacted_ident { #( #redaction_struct_fields: self.#redaction_struct_fields, )* }
|
||||
pub fn redact(self, version: #ruma_identifiers::RoomVersionId) -> #redacted_ident {
|
||||
#redacted_ident {
|
||||
#( #redaction_struct_fields: self.#redaction_struct_fields, )*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc = #doc]
|
||||
#[derive(
|
||||
Clone,
|
||||
Debug,
|
||||
#import_path::exports::serde::Deserialize,
|
||||
#import_path::exports::serde::Serialize
|
||||
)]
|
||||
#[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)]
|
||||
pub struct #redacted_ident #redacted_fields
|
||||
|
||||
#redacted_event_content
|
||||
|
||||
impl #import_path::RedactedEventContent for #redacted_ident {
|
||||
fn empty(ev_type: &str) -> Result<Self, #import_path::exports::serde_json::Error> {
|
||||
impl #ruma_events::RedactedEventContent for #redacted_ident {
|
||||
fn empty(ev_type: &str) -> Result<Self, #serde_json::Error> {
|
||||
if ev_type != #event_type {
|
||||
return Err(#import_path::exports::serde::de::Error::custom(
|
||||
return Err(#serde::de::Error::custom(
|
||||
format!("expected event type `{}`, found `{}`", #event_type, ev_type)
|
||||
));
|
||||
}
|
||||
@ -208,7 +209,7 @@ pub fn expand_event_content(
|
||||
#has_serialize_fields
|
||||
}
|
||||
|
||||
fn has_deserialize_fields() -> #import_path::HasDeserializeFields {
|
||||
fn has_deserialize_fields() -> #ruma_events::HasDeserializeFields {
|
||||
#has_deserialize_fields
|
||||
}
|
||||
}
|
||||
@ -217,7 +218,7 @@ pub fn expand_event_content(
|
||||
TokenStream::new()
|
||||
};
|
||||
|
||||
let event_content = generate_event_content_impl(ident, event_type, import_path);
|
||||
let event_content = generate_event_content_impl(ident, event_type, ruma_events);
|
||||
|
||||
Ok(quote! {
|
||||
#event_content
|
||||
@ -229,60 +230,60 @@ pub fn expand_event_content(
|
||||
/// Create a `BasicEventContent` implementation for a struct
|
||||
pub fn expand_basic_event_content(
|
||||
input: &DeriveInput,
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> syn::Result<TokenStream> {
|
||||
let ident = input.ident.clone();
|
||||
let event_content_impl = expand_event_content(input, false, import_path)?;
|
||||
let event_content_impl = expand_event_content(input, false, ruma_events)?;
|
||||
|
||||
Ok(quote! {
|
||||
#event_content_impl
|
||||
|
||||
impl #import_path::BasicEventContent for #ident { }
|
||||
impl #ruma_events::BasicEventContent for #ident { }
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a `EphemeralRoomEventContent` implementation for a struct
|
||||
pub fn expand_ephemeral_room_event_content(
|
||||
input: &DeriveInput,
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> syn::Result<TokenStream> {
|
||||
let ident = input.ident.clone();
|
||||
let event_content_impl = expand_event_content(input, false, import_path)?;
|
||||
let event_content_impl = expand_event_content(input, false, ruma_events)?;
|
||||
|
||||
Ok(quote! {
|
||||
#event_content_impl
|
||||
|
||||
impl #import_path::EphemeralRoomEventContent for #ident { }
|
||||
impl #ruma_events::EphemeralRoomEventContent for #ident { }
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a `RoomEventContent` implementation for a struct.
|
||||
pub fn expand_room_event_content(
|
||||
input: &DeriveInput,
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> syn::Result<TokenStream> {
|
||||
let ident = input.ident.clone();
|
||||
let event_content_impl = expand_event_content(input, true, import_path)?;
|
||||
let event_content_impl = expand_event_content(input, true, ruma_events)?;
|
||||
|
||||
Ok(quote! {
|
||||
#event_content_impl
|
||||
|
||||
impl #import_path::RoomEventContent for #ident { }
|
||||
impl #ruma_events::RoomEventContent for #ident { }
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a `MessageEventContent` implementation for a struct
|
||||
pub fn expand_message_event_content(
|
||||
input: &DeriveInput,
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> syn::Result<TokenStream> {
|
||||
let ident = input.ident.clone();
|
||||
let room_ev_content = expand_room_event_content(input, import_path)?;
|
||||
let room_ev_content = expand_room_event_content(input, ruma_events)?;
|
||||
|
||||
let redacted_marker_trait = if needs_redacted_from_input(input) {
|
||||
let ident = format_ident!("Redacted{}", &ident);
|
||||
quote! {
|
||||
impl #import_path::RedactedMessageEventContent for #ident { }
|
||||
impl #ruma_events::RedactedMessageEventContent for #ident { }
|
||||
}
|
||||
} else {
|
||||
TokenStream::new()
|
||||
@ -291,7 +292,7 @@ pub fn expand_message_event_content(
|
||||
Ok(quote! {
|
||||
#room_ev_content
|
||||
|
||||
impl #import_path::MessageEventContent for #ident { }
|
||||
impl #ruma_events::MessageEventContent for #ident { }
|
||||
|
||||
#redacted_marker_trait
|
||||
})
|
||||
@ -300,15 +301,15 @@ pub fn expand_message_event_content(
|
||||
/// Create a `StateEventContent` implementation for a struct
|
||||
pub fn expand_state_event_content(
|
||||
input: &DeriveInput,
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> syn::Result<TokenStream> {
|
||||
let ident = input.ident.clone();
|
||||
let room_ev_content = expand_room_event_content(input, import_path)?;
|
||||
let room_ev_content = expand_room_event_content(input, ruma_events)?;
|
||||
|
||||
let redacted_marker_trait = if needs_redacted_from_input(input) {
|
||||
let ident = format_ident!("Redacted{}", input.ident);
|
||||
quote! {
|
||||
impl #import_path::RedactedStateEventContent for #ident { }
|
||||
impl #ruma_events::RedactedStateEventContent for #ident { }
|
||||
}
|
||||
} else {
|
||||
TokenStream::new()
|
||||
@ -317,7 +318,7 @@ pub fn expand_state_event_content(
|
||||
Ok(quote! {
|
||||
#room_ev_content
|
||||
|
||||
impl #import_path::StateEventContent for #ident { }
|
||||
impl #ruma_events::StateEventContent for #ident { }
|
||||
|
||||
#redacted_marker_trait
|
||||
})
|
||||
@ -326,25 +327,28 @@ pub fn expand_state_event_content(
|
||||
fn generate_event_content_impl(
|
||||
ident: &Ident,
|
||||
event_type: &LitStr,
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> TokenStream {
|
||||
let serde = quote! { #ruma_events::exports::serde };
|
||||
let serde_json = quote! { #ruma_events::exports::serde_json };
|
||||
|
||||
quote! {
|
||||
impl #import_path::EventContent for #ident {
|
||||
impl #ruma_events::EventContent for #ident {
|
||||
fn event_type(&self) -> &str {
|
||||
#event_type
|
||||
}
|
||||
|
||||
fn from_parts(
|
||||
ev_type: &str,
|
||||
content: Box<#import_path::exports::serde_json::value::RawValue>
|
||||
) -> Result<Self, #import_path::exports::serde_json::Error> {
|
||||
content: Box<#serde_json::value::RawValue>
|
||||
) -> Result<Self, #serde_json::Error> {
|
||||
if ev_type != #event_type {
|
||||
return Err(#import_path::exports::serde::de::Error::custom(
|
||||
return Err(#serde::de::Error::custom(
|
||||
format!("expected event type `{}`, found `{}`", #event_type, ev_type)
|
||||
));
|
||||
}
|
||||
|
||||
#import_path::exports::serde_json::from_str(content.get())
|
||||
#serde_json::from_str(content.get())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ const EVENT_FIELDS: &[(&str, EventKindFn)] = &[
|
||||
|
||||
/// Create a content enum from `EventEnumInput`.
|
||||
pub fn expand_event_enum(input: EventEnumInput) -> syn::Result<TokenStream> {
|
||||
let import_path = crate::import_ruma_events();
|
||||
let ruma_events = crate::import_ruma_events();
|
||||
|
||||
let name = &input.name;
|
||||
let attrs = &input.attrs;
|
||||
@ -58,7 +58,7 @@ pub fn expand_event_enum(input: EventEnumInput) -> syn::Result<TokenStream> {
|
||||
attrs,
|
||||
&variants,
|
||||
&EventKindVariation::Full,
|
||||
&import_path,
|
||||
&ruma_events,
|
||||
);
|
||||
|
||||
let sync_event_enum = expand_any_with_deser(
|
||||
@ -67,7 +67,7 @@ pub fn expand_event_enum(input: EventEnumInput) -> syn::Result<TokenStream> {
|
||||
attrs,
|
||||
&variants,
|
||||
&EventKindVariation::Sync,
|
||||
&import_path,
|
||||
&ruma_events,
|
||||
);
|
||||
|
||||
let stripped_event_enum = expand_any_with_deser(
|
||||
@ -76,7 +76,7 @@ pub fn expand_event_enum(input: EventEnumInput) -> syn::Result<TokenStream> {
|
||||
attrs,
|
||||
&variants,
|
||||
&EventKindVariation::Stripped,
|
||||
&import_path,
|
||||
&ruma_events,
|
||||
);
|
||||
|
||||
let initial_event_enum = expand_any_with_deser(
|
||||
@ -85,12 +85,12 @@ pub fn expand_event_enum(input: EventEnumInput) -> syn::Result<TokenStream> {
|
||||
attrs,
|
||||
&variants,
|
||||
&EventKindVariation::Initial,
|
||||
&import_path,
|
||||
&ruma_events,
|
||||
);
|
||||
|
||||
let redacted_event_enums = expand_any_redacted(name, &events, attrs, &variants, &import_path);
|
||||
let redacted_event_enums = expand_any_redacted(name, &events, attrs, &variants, &ruma_events);
|
||||
|
||||
let event_content_enum = expand_content_enum(name, &events, attrs, &variants, &import_path);
|
||||
let event_content_enum = expand_content_enum(name, &events, attrs, &variants, &ruma_events);
|
||||
|
||||
Ok(quote! {
|
||||
#event_enum
|
||||
@ -113,8 +113,11 @@ fn expand_any_with_deser(
|
||||
attrs: &[Attribute],
|
||||
variants: &[EventEnumVariant],
|
||||
var: &EventKindVariation,
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> Option<TokenStream> {
|
||||
let serde = quote! { #ruma_events::exports::serde };
|
||||
let serde_json = quote! { #ruma_events::exports::serde_json };
|
||||
|
||||
// If the event cannot be generated this bails out returning None which is rendered the same
|
||||
// as an empty `TokenStream`. This is effectively the check if the given input generates
|
||||
// a valid event enum.
|
||||
@ -122,18 +125,18 @@ fn expand_any_with_deser(
|
||||
|
||||
let content = events
|
||||
.iter()
|
||||
.map(|event| to_event_path(event, &event_struct, import_path))
|
||||
.map(|event| to_event_path(event, &event_struct, ruma_events))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let variant_decls = variants.iter().map(|v| v.decl());
|
||||
let self_variants = variants.iter().map(|v| v.ctor(quote!(Self)));
|
||||
|
||||
let (custom_variant, custom_deserialize) =
|
||||
generate_custom_variant(&event_struct, var, import_path);
|
||||
generate_custom_variant(&event_struct, var, ruma_events);
|
||||
|
||||
let any_enum = quote! {
|
||||
#( #attrs )*
|
||||
#[derive(Clone, Debug, #import_path::exports::serde::Serialize)]
|
||||
#[derive(Clone, Debug, #serde::Serialize)]
|
||||
#[serde(untagged)]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
pub enum #ident {
|
||||
@ -151,21 +154,21 @@ fn expand_any_with_deser(
|
||||
});
|
||||
|
||||
let event_deserialize_impl = quote! {
|
||||
impl<'de> #import_path::exports::serde::de::Deserialize<'de> for #ident {
|
||||
impl<'de> #serde::de::Deserialize<'de> for #ident {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: #import_path::exports::serde::de::Deserializer<'de>,
|
||||
D: #serde::de::Deserializer<'de>,
|
||||
{
|
||||
use #import_path::exports::serde::de::Error as _;
|
||||
use #serde::de::Error as _;
|
||||
|
||||
let json = Box::<#import_path::exports::serde_json::value::RawValue>::deserialize(deserializer)?;
|
||||
let #import_path::EventDeHelper { ev_type, .. } =
|
||||
#import_path::from_raw_json_value(&json)?;
|
||||
let json = Box::<#serde_json::value::RawValue>::deserialize(deserializer)?;
|
||||
let #ruma_events::EventDeHelper { ev_type, .. } =
|
||||
#ruma_events::from_raw_json_value(&json)?;
|
||||
|
||||
match ev_type.as_str() {
|
||||
#(
|
||||
#variant_attrs #events => {
|
||||
let event = #import_path::exports::serde_json::from_str::<#content>(json.get())
|
||||
let event = #serde_json::from_str::<#content>(json.get())
|
||||
.map_err(D::Error::custom)?;
|
||||
Ok(#self_variants(event))
|
||||
},
|
||||
@ -176,13 +179,13 @@ fn expand_any_with_deser(
|
||||
}
|
||||
};
|
||||
|
||||
let event_enum_to_from_sync = expand_conversion_impl(kind, var, &variants, import_path);
|
||||
let event_enum_to_from_sync = expand_conversion_impl(kind, var, &variants, ruma_events);
|
||||
|
||||
let redacted_enum = expand_redacted_enum(kind, var, import_path);
|
||||
let redacted_enum = expand_redacted_enum(kind, var, ruma_events);
|
||||
|
||||
let field_accessor_impl = accessor_methods(kind, var, &variants, import_path);
|
||||
let field_accessor_impl = accessor_methods(kind, var, &variants, ruma_events);
|
||||
|
||||
let redact_impl = expand_redact(&ident, kind, var, &variants, import_path);
|
||||
let redact_impl = expand_redact(&ident, kind, var, &variants, ruma_events);
|
||||
|
||||
Some(quote! {
|
||||
#any_enum
|
||||
@ -203,8 +206,10 @@ fn expand_conversion_impl(
|
||||
kind: &EventKind,
|
||||
var: &EventKindVariation,
|
||||
variants: &[EventEnumVariant],
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> Option<TokenStream> {
|
||||
let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers };
|
||||
|
||||
let ident = kind.to_event_enum_ident(var)?;
|
||||
let variants = &variants
|
||||
.iter()
|
||||
@ -235,7 +240,7 @@ fn expand_conversion_impl(
|
||||
let redaction = if let (EventKind::Message, EventKindVariation::Full) = (kind, var) {
|
||||
quote! {
|
||||
#ident::RoomRedaction(event) => Self::RoomRedaction(
|
||||
#import_path::room::redaction::SyncRedactionEvent::from(event),
|
||||
#ruma_events::room::redaction::SyncRedactionEvent::from(event),
|
||||
),
|
||||
}
|
||||
} else {
|
||||
@ -248,12 +253,12 @@ fn expand_conversion_impl(
|
||||
match event {
|
||||
#(
|
||||
#ident_variants(event) => {
|
||||
#self_variants(#import_path::#sync_struct::from(event))
|
||||
#self_variants(#ruma_events::#sync_struct::from(event))
|
||||
},
|
||||
)*
|
||||
#redaction
|
||||
#ident::Custom(event) => {
|
||||
Self::Custom(#import_path::#sync_struct::from(event))
|
||||
Self::Custom(#ruma_events::#sync_struct::from(event))
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -286,7 +291,7 @@ fn expand_conversion_impl(
|
||||
/// Convert this sync event into a full event, one with a room_id field.
|
||||
pub fn into_full_event(
|
||||
self,
|
||||
room_id: #import_path::exports::ruma_identifiers::RoomId
|
||||
room_id: #ruma_identifiers::RoomId
|
||||
) -> #full {
|
||||
match self {
|
||||
#(
|
||||
@ -317,17 +322,17 @@ fn expand_any_redacted(
|
||||
events: &[LitStr],
|
||||
attrs: &[Attribute],
|
||||
variants: &[EventEnumVariant],
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> TokenStream {
|
||||
use EventKindVariation as V;
|
||||
|
||||
if kind.is_state() {
|
||||
let full_state =
|
||||
expand_any_with_deser(kind, events, attrs, variants, &V::Redacted, import_path);
|
||||
expand_any_with_deser(kind, events, attrs, variants, &V::Redacted, ruma_events);
|
||||
let sync_state =
|
||||
expand_any_with_deser(kind, events, attrs, variants, &V::RedactedSync, import_path);
|
||||
expand_any_with_deser(kind, events, attrs, variants, &V::RedactedSync, ruma_events);
|
||||
let stripped_state =
|
||||
expand_any_with_deser(kind, events, attrs, variants, &V::RedactedStripped, import_path);
|
||||
expand_any_with_deser(kind, events, attrs, variants, &V::RedactedStripped, ruma_events);
|
||||
|
||||
quote! {
|
||||
#full_state
|
||||
@ -338,9 +343,9 @@ fn expand_any_redacted(
|
||||
}
|
||||
} else if kind.is_message() {
|
||||
let full_message =
|
||||
expand_any_with_deser(kind, events, attrs, variants, &V::Redacted, import_path);
|
||||
expand_any_with_deser(kind, events, attrs, variants, &V::Redacted, ruma_events);
|
||||
let sync_message =
|
||||
expand_any_with_deser(kind, events, attrs, variants, &V::RedactedSync, import_path);
|
||||
expand_any_with_deser(kind, events, attrs, variants, &V::RedactedSync, ruma_events);
|
||||
|
||||
quote! {
|
||||
#full_message
|
||||
@ -358,19 +363,22 @@ fn expand_content_enum(
|
||||
events: &[LitStr],
|
||||
attrs: &[Attribute],
|
||||
variants: &[EventEnumVariant],
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> TokenStream {
|
||||
let serde = quote! { #ruma_events::exports::serde };
|
||||
let serde_json = quote! { #ruma_events::exports::serde_json };
|
||||
|
||||
let ident = kind.to_content_enum();
|
||||
let event_type_str = events;
|
||||
|
||||
let content =
|
||||
events.iter().map(|ev| to_event_content_path(ev, import_path)).collect::<Vec<_>>();
|
||||
events.iter().map(|ev| to_event_content_path(ev, ruma_events)).collect::<Vec<_>>();
|
||||
|
||||
let variant_decls = variants.iter().map(|v| v.decl());
|
||||
|
||||
let content_enum = quote! {
|
||||
#( #attrs )*
|
||||
#[derive(Clone, Debug, #import_path::exports::serde::Serialize)]
|
||||
#[derive(Clone, Debug, #serde::Serialize)]
|
||||
#[serde(untagged)]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
pub enum #ident {
|
||||
@ -379,7 +387,7 @@ fn expand_content_enum(
|
||||
#variant_decls(#content),
|
||||
)*
|
||||
/// Content of an event not defined by the Matrix specification.
|
||||
Custom(#import_path::custom::CustomEventContent),
|
||||
Custom(#ruma_events::custom::CustomEventContent),
|
||||
}
|
||||
};
|
||||
|
||||
@ -391,7 +399,7 @@ fn expand_content_enum(
|
||||
let variant_ctors = variants.iter().map(|v| v.ctor(quote!(Self)));
|
||||
|
||||
let event_content_impl = quote! {
|
||||
impl #import_path::EventContent for #ident {
|
||||
impl #ruma_events::EventContent for #ident {
|
||||
fn event_type(&self) -> &str {
|
||||
match self {
|
||||
#( #variant_arms(content) => content.event_type(), )*
|
||||
@ -400,8 +408,8 @@ fn expand_content_enum(
|
||||
}
|
||||
|
||||
fn from_parts(
|
||||
event_type: &str, input: Box<#import_path::exports::serde_json::value::RawValue>,
|
||||
) -> Result<Self, #import_path::exports::serde_json::Error> {
|
||||
event_type: &str, input: Box<#serde_json::value::RawValue>,
|
||||
) -> Result<Self, #serde_json::Error> {
|
||||
match event_type {
|
||||
#(
|
||||
#variant_attrs #event_type_str => {
|
||||
@ -411,7 +419,7 @@ fn expand_content_enum(
|
||||
)*
|
||||
ev_type => {
|
||||
let content =
|
||||
#import_path::custom::CustomEventContent::from_parts(ev_type, input)?;
|
||||
#ruma_events::custom::CustomEventContent::from_parts(ev_type, input)?;
|
||||
Ok(Self::Custom(content))
|
||||
},
|
||||
}
|
||||
@ -419,7 +427,7 @@ fn expand_content_enum(
|
||||
}
|
||||
};
|
||||
|
||||
let marker_trait_impls = marker_traits(&kind, import_path);
|
||||
let marker_trait_impls = marker_traits(&kind, ruma_events);
|
||||
|
||||
quote! {
|
||||
#content_enum
|
||||
@ -435,32 +443,34 @@ fn expand_redact(
|
||||
kind: &EventKind,
|
||||
var: &EventKindVariation,
|
||||
variants: &[EventEnumVariant],
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> Option<TokenStream> {
|
||||
let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers };
|
||||
|
||||
if let EventKindVariation::Full | EventKindVariation::Sync | EventKindVariation::Stripped = var
|
||||
{
|
||||
let (param, redaction_type, redaction_enum) = match var {
|
||||
EventKindVariation::Full => {
|
||||
let struct_id = kind.to_event_ident(&EventKindVariation::Redacted)?;
|
||||
(
|
||||
quote! { #import_path::room::redaction::RedactionEvent },
|
||||
quote! { #import_path::#struct_id },
|
||||
quote! { #ruma_events::room::redaction::RedactionEvent },
|
||||
quote! { #ruma_events::#struct_id },
|
||||
kind.to_event_enum_ident(&EventKindVariation::Redacted)?,
|
||||
)
|
||||
}
|
||||
EventKindVariation::Sync => {
|
||||
let struct_id = kind.to_event_ident(&EventKindVariation::RedactedSync)?;
|
||||
(
|
||||
quote! { #import_path::room::redaction::SyncRedactionEvent },
|
||||
quote! { #import_path::#struct_id },
|
||||
quote! { #ruma_events::room::redaction::SyncRedactionEvent },
|
||||
quote! { #ruma_events::#struct_id },
|
||||
kind.to_event_enum_ident(&EventKindVariation::RedactedSync)?,
|
||||
)
|
||||
}
|
||||
EventKindVariation::Stripped => {
|
||||
let struct_id = kind.to_event_ident(&EventKindVariation::RedactedStripped)?;
|
||||
(
|
||||
quote! { #import_path::room::redaction::SyncRedactionEvent },
|
||||
quote! { #import_path::#struct_id },
|
||||
quote! { #ruma_events::room::redaction::SyncRedactionEvent },
|
||||
quote! { #ruma_events::#struct_id },
|
||||
kind.to_event_enum_ident(&EventKindVariation::RedactedStripped)?,
|
||||
)
|
||||
}
|
||||
@ -471,7 +481,7 @@ fn expand_redact(
|
||||
let redaction_variants = variants.iter().map(|v| v.ctor(&redaction_enum));
|
||||
|
||||
let fields = EVENT_FIELDS.iter().map(|(name, has_field)| {
|
||||
generate_redacted_fields(name, kind, var, *has_field, import_path)
|
||||
generate_redacted_fields(name, kind, var, *has_field, ruma_events)
|
||||
});
|
||||
|
||||
let fields = quote! { #( #fields )* };
|
||||
@ -482,7 +492,7 @@ fn expand_redact(
|
||||
pub fn redact(
|
||||
self,
|
||||
redaction: #param,
|
||||
version: #import_path::exports::ruma_identifiers::RoomVersionId,
|
||||
version: #ruma_identifiers::RoomVersionId,
|
||||
) -> #redaction_enum {
|
||||
match self {
|
||||
#(
|
||||
@ -513,8 +523,11 @@ fn expand_redact(
|
||||
fn expand_redacted_enum(
|
||||
kind: &EventKind,
|
||||
var: &EventKindVariation,
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> Option<TokenStream> {
|
||||
let serde = quote! { #ruma_events::exports::serde };
|
||||
let serde_json = quote! { #ruma_events::exports::serde_json };
|
||||
|
||||
if let EventKind::State | EventKind::Message = kind {
|
||||
let ident = format_ident!("AnyPossiblyRedacted{}", kind.to_event_ident(var)?);
|
||||
|
||||
@ -528,7 +541,7 @@ fn expand_redacted_enum(
|
||||
|
||||
Some(quote! {
|
||||
/// An enum that holds either regular un-redacted events or redacted events.
|
||||
#[derive(Clone, Debug, #import_path::exports::serde::Serialize)]
|
||||
#[derive(Clone, Debug, #serde::Serialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum #ident {
|
||||
/// An un-redacted event.
|
||||
@ -538,20 +551,20 @@ fn expand_redacted_enum(
|
||||
Redacted(#redacted_enum_ident),
|
||||
}
|
||||
|
||||
impl<'de> #import_path::exports::serde::de::Deserialize<'de> for #ident {
|
||||
impl<'de> #serde::de::Deserialize<'de> for #ident {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: #import_path::exports::serde::de::Deserializer<'de>,
|
||||
D: #serde::de::Deserializer<'de>,
|
||||
{
|
||||
let json = Box::<#import_path::exports::serde_json::value::RawValue>::deserialize(deserializer)?;
|
||||
let #import_path::EventDeHelper { unsigned, .. } =
|
||||
#import_path::from_raw_json_value(&json)?;
|
||||
let json = Box::<#serde_json::value::RawValue>::deserialize(deserializer)?;
|
||||
let #ruma_events::EventDeHelper { unsigned, .. } =
|
||||
#ruma_events::from_raw_json_value(&json)?;
|
||||
|
||||
Ok(match unsigned {
|
||||
Some(unsigned) if unsigned.redacted_because.is_some() => {
|
||||
Self::Redacted(#import_path::from_raw_json_value(&json)?)
|
||||
Self::Redacted(#ruma_events::from_raw_json_value(&json)?)
|
||||
}
|
||||
_ => Self::Regular(#import_path::from_raw_json_value(&json)?),
|
||||
_ => Self::Regular(#ruma_events::from_raw_json_value(&json)?),
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -570,7 +583,7 @@ fn generate_redacted_fields(
|
||||
kind: &EventKind,
|
||||
var: &EventKindVariation,
|
||||
is_event_kind: EventKindFn,
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> TokenStream {
|
||||
if is_event_kind(kind, var) {
|
||||
let name = Ident::new(name, Span::call_site());
|
||||
@ -583,7 +596,7 @@ fn generate_redacted_fields(
|
||||
};
|
||||
|
||||
quote! {
|
||||
unsigned: #import_path::#redaction_type {
|
||||
unsigned: #ruma_events::#redaction_type {
|
||||
redacted_because: Some(::std::boxed::Box::new(redaction)),
|
||||
},
|
||||
}
|
||||
@ -600,21 +613,25 @@ fn generate_redacted_fields(
|
||||
fn generate_custom_variant(
|
||||
event_struct: &Ident,
|
||||
var: &EventKindVariation,
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> (TokenStream, TokenStream) {
|
||||
use EventKindVariation as V;
|
||||
|
||||
let serde_json = quote! { #ruma_events::exports::serde_json };
|
||||
|
||||
if matches!(var, V::Redacted | V::RedactedSync | V::RedactedStripped) {
|
||||
(
|
||||
quote! {
|
||||
/// A redacted event not defined by the Matrix specification
|
||||
Custom(#import_path::#event_struct<#import_path::custom::RedactedCustomEventContent>),
|
||||
Custom(
|
||||
#ruma_events::#event_struct<#ruma_events::custom::RedactedCustomEventContent>,
|
||||
),
|
||||
},
|
||||
quote! {
|
||||
event => {
|
||||
let event = #import_path::exports::serde_json::from_str::<
|
||||
#import_path::#event_struct<#import_path::custom::RedactedCustomEventContent>,
|
||||
>(json.get())
|
||||
let event = #serde_json::from_str::<#ruma_events::#event_struct<
|
||||
#ruma_events::custom::RedactedCustomEventContent,
|
||||
>>(json.get())
|
||||
.map_err(D::Error::custom)?;
|
||||
|
||||
Ok(Self::Custom(event))
|
||||
@ -625,13 +642,13 @@ fn generate_custom_variant(
|
||||
(
|
||||
quote! {
|
||||
/// An event not defined by the Matrix specification
|
||||
Custom(#import_path::#event_struct<#import_path::custom::CustomEventContent>),
|
||||
Custom(#ruma_events::#event_struct<#ruma_events::custom::CustomEventContent>),
|
||||
},
|
||||
quote! {
|
||||
event => {
|
||||
let event =
|
||||
#import_path::exports::serde_json::from_str::<
|
||||
#import_path::#event_struct<#import_path::custom::CustomEventContent>
|
||||
#serde_json::from_str::<
|
||||
#ruma_events::#event_struct<#ruma_events::custom::CustomEventContent>
|
||||
>(json.get())
|
||||
.map_err(D::Error::custom)?;
|
||||
|
||||
@ -642,22 +659,22 @@ fn generate_custom_variant(
|
||||
}
|
||||
}
|
||||
|
||||
fn marker_traits(kind: &EventKind, import_path: &TokenStream) -> TokenStream {
|
||||
fn marker_traits(kind: &EventKind, ruma_events: &TokenStream) -> TokenStream {
|
||||
let ident = kind.to_content_enum();
|
||||
match kind {
|
||||
EventKind::State => quote! {
|
||||
impl #import_path::RoomEventContent for #ident {}
|
||||
impl #import_path::StateEventContent for #ident {}
|
||||
impl #ruma_events::RoomEventContent for #ident {}
|
||||
impl #ruma_events::StateEventContent for #ident {}
|
||||
},
|
||||
EventKind::Message => quote! {
|
||||
impl #import_path::RoomEventContent for #ident {}
|
||||
impl #import_path::MessageEventContent for #ident {}
|
||||
impl #ruma_events::RoomEventContent for #ident {}
|
||||
impl #ruma_events::MessageEventContent for #ident {}
|
||||
},
|
||||
EventKind::Ephemeral => quote! {
|
||||
impl #import_path::EphemeralRoomEventContent for #ident {}
|
||||
impl #ruma_events::EphemeralRoomEventContent for #ident {}
|
||||
},
|
||||
EventKind::Basic => quote! {
|
||||
impl #import_path::BasicEventContent for #ident {}
|
||||
impl #ruma_events::BasicEventContent for #ident {}
|
||||
},
|
||||
_ => TokenStream::new(),
|
||||
}
|
||||
@ -667,7 +684,7 @@ fn accessor_methods(
|
||||
kind: &EventKind,
|
||||
var: &EventKindVariation,
|
||||
variants: &[EventEnumVariant],
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> Option<TokenStream> {
|
||||
use EventKindVariation as V;
|
||||
|
||||
@ -675,11 +692,11 @@ fn accessor_methods(
|
||||
|
||||
// matching `EventKindVariation`s
|
||||
if let V::Redacted | V::RedactedSync | V::RedactedStripped = var {
|
||||
return redacted_accessor_methods(kind, var, variants, import_path);
|
||||
return redacted_accessor_methods(kind, var, variants, ruma_events);
|
||||
}
|
||||
|
||||
let methods = EVENT_FIELDS.iter().map(|(name, has_field)| {
|
||||
generate_accessor(name, kind, var, *has_field, variants, import_path)
|
||||
generate_accessor(name, kind, var, *has_field, variants, ruma_events)
|
||||
});
|
||||
|
||||
let content_enum = kind.to_content_enum();
|
||||
@ -752,11 +769,11 @@ fn redacted_accessor_methods(
|
||||
kind: &EventKind,
|
||||
var: &EventKindVariation,
|
||||
variants: &[EventEnumVariant],
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> Option<TokenStream> {
|
||||
let ident = kind.to_event_enum_ident(var)?;
|
||||
let methods = EVENT_FIELDS.iter().map(|(name, has_field)| {
|
||||
generate_accessor(name, kind, var, *has_field, variants, import_path)
|
||||
generate_accessor(name, kind, var, *has_field, variants, ruma_events)
|
||||
});
|
||||
|
||||
Some(quote! {
|
||||
@ -766,7 +783,7 @@ fn redacted_accessor_methods(
|
||||
})
|
||||
}
|
||||
|
||||
fn to_event_path(name: &LitStr, struct_name: &Ident, import_path: &TokenStream) -> TokenStream {
|
||||
fn to_event_path(name: &LitStr, struct_name: &Ident, ruma_events: &TokenStream) -> TokenStream {
|
||||
let span = name.span();
|
||||
let name = name.value();
|
||||
|
||||
@ -790,7 +807,7 @@ fn to_event_path(name: &LitStr, struct_name: &Ident, import_path: &TokenStream)
|
||||
} else {
|
||||
quote! { SyncRedactionEvent }
|
||||
};
|
||||
quote! { #import_path::room::redaction::#redaction }
|
||||
quote! { #ruma_events::room::redaction::#redaction }
|
||||
}
|
||||
"ToDeviceEvent"
|
||||
| "SyncStateEvent"
|
||||
@ -799,20 +816,20 @@ fn to_event_path(name: &LitStr, struct_name: &Ident, import_path: &TokenStream)
|
||||
| "SyncMessageEvent"
|
||||
| "SyncEphemeralRoomEvent" => {
|
||||
let content = format_ident!("{}EventContent", event);
|
||||
quote! { #import_path::#struct_name<#import_path::#( #path )::*::#content> }
|
||||
quote! { #ruma_events::#struct_name<#ruma_events::#( #path )::*::#content> }
|
||||
}
|
||||
struct_str if struct_str.contains("Redacted") => {
|
||||
let content = format_ident!("Redacted{}EventContent", event);
|
||||
quote! { #import_path::#struct_name<#import_path::#( #path )::*::#content> }
|
||||
quote! { #ruma_events::#struct_name<#ruma_events::#( #path )::*::#content> }
|
||||
}
|
||||
_ => {
|
||||
let event_name = format_ident!("{}Event", event);
|
||||
quote! { #import_path::#( #path )::*::#event_name }
|
||||
quote! { #ruma_events::#( #path )::*::#event_name }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn to_event_content_path(name: &LitStr, import_path: &TokenStream) -> TokenStream {
|
||||
fn to_event_content_path(name: &LitStr, ruma_events: &TokenStream) -> TokenStream {
|
||||
let span = name.span();
|
||||
let name = name.value();
|
||||
|
||||
@ -830,7 +847,7 @@ fn to_event_content_path(name: &LitStr, import_path: &TokenStream) -> TokenStrea
|
||||
let content_str = format_ident!("{}EventContent", event);
|
||||
let path = path.iter().map(|s| Ident::new(s, span));
|
||||
quote! {
|
||||
#import_path::#( #path )::*::#content_str
|
||||
#ruma_events::#( #path )::*::#content_str
|
||||
}
|
||||
}
|
||||
|
||||
@ -861,12 +878,12 @@ fn generate_accessor(
|
||||
var: &EventKindVariation,
|
||||
is_event_kind: EventKindFn,
|
||||
variants: &[EventEnumVariant],
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> TokenStream {
|
||||
if is_event_kind(kind, var) {
|
||||
let docs = format!("Returns this event's {} field.", name);
|
||||
let ident = Ident::new(name, Span::call_site());
|
||||
let field_type = field_return_type(name, var, import_path);
|
||||
let field_type = field_return_type(name, var, ruma_events);
|
||||
let variants = variants.iter().map(|v| v.match_arm(quote!(Self)));
|
||||
|
||||
quote! {
|
||||
@ -886,21 +903,23 @@ fn generate_accessor(
|
||||
fn field_return_type(
|
||||
name: &str,
|
||||
var: &EventKindVariation,
|
||||
import_path: &TokenStream,
|
||||
ruma_events: &TokenStream,
|
||||
) -> TokenStream {
|
||||
let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers };
|
||||
|
||||
match name {
|
||||
"origin_server_ts" => quote! { ::std::time::SystemTime },
|
||||
"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 },
|
||||
"room_id" => quote! { #ruma_identifiers::RoomId },
|
||||
"event_id" => quote! { #ruma_identifiers::EventId },
|
||||
"sender" => quote! { #ruma_identifiers::UserId },
|
||||
"state_key" => quote! { str },
|
||||
"unsigned" if &EventKindVariation::RedactedSync == var => {
|
||||
quote! { #import_path::RedactedSyncUnsigned }
|
||||
quote! { #ruma_events::RedactedSyncUnsigned }
|
||||
}
|
||||
"unsigned" if &EventKindVariation::Redacted == var => {
|
||||
quote! { #import_path::RedactedUnsigned }
|
||||
quote! { #ruma_events::RedactedUnsigned }
|
||||
}
|
||||
"unsigned" => quote! { #import_path::Unsigned },
|
||||
"unsigned" => quote! { #ruma_events::Unsigned },
|
||||
_ => panic!("the `ruma_events_macros::event_enum::EVENT_FIELD` const was changed"),
|
||||
}
|
||||
}
|
||||
|
@ -54,10 +54,10 @@ pub fn event_enum(input: TokenStream) -> TokenStream {
|
||||
/// Generates an implementation of `ruma_events::EventContent`.
|
||||
#[proc_macro_derive(EventContent, attributes(ruma_event))]
|
||||
pub fn derive_event_content(input: TokenStream) -> TokenStream {
|
||||
let import_path = import_ruma_events();
|
||||
let ruma_events = import_ruma_events();
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
expand_event_content(&input, true, &import_path)
|
||||
expand_event_content(&input, true, &ruma_events)
|
||||
.unwrap_or_else(|err| err.to_compile_error())
|
||||
.into()
|
||||
}
|
||||
@ -65,10 +65,10 @@ pub fn derive_event_content(input: TokenStream) -> TokenStream {
|
||||
/// Generates an implementation of `ruma_events::BasicEventContent` and it's super traits.
|
||||
#[proc_macro_derive(BasicEventContent, attributes(ruma_event))]
|
||||
pub fn derive_basic_event_content(input: TokenStream) -> TokenStream {
|
||||
let import_path = import_ruma_events();
|
||||
let ruma_events = import_ruma_events();
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
expand_basic_event_content(&input, &import_path)
|
||||
expand_basic_event_content(&input, &ruma_events)
|
||||
.unwrap_or_else(|err| err.to_compile_error())
|
||||
.into()
|
||||
}
|
||||
@ -76,10 +76,10 @@ pub fn derive_basic_event_content(input: TokenStream) -> TokenStream {
|
||||
/// Generates an implementation of `ruma_events::RoomEventContent` and it's super traits.
|
||||
#[proc_macro_derive(RoomEventContent, attributes(ruma_event))]
|
||||
pub fn derive_room_event_content(input: TokenStream) -> TokenStream {
|
||||
let import_path = import_ruma_events();
|
||||
let ruma_events = import_ruma_events();
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
expand_room_event_content(&input, &import_path)
|
||||
expand_room_event_content(&input, &ruma_events)
|
||||
.unwrap_or_else(|err| err.to_compile_error())
|
||||
.into()
|
||||
}
|
||||
@ -87,10 +87,10 @@ pub fn derive_room_event_content(input: TokenStream) -> TokenStream {
|
||||
/// Generates an implementation of `ruma_events::MessageEventContent` and it's super traits.
|
||||
#[proc_macro_derive(MessageEventContent, attributes(ruma_event))]
|
||||
pub fn derive_message_event_content(input: TokenStream) -> TokenStream {
|
||||
let import_path = import_ruma_events();
|
||||
let ruma_events = import_ruma_events();
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
expand_message_event_content(&input, &import_path)
|
||||
expand_message_event_content(&input, &ruma_events)
|
||||
.unwrap_or_else(|err| err.to_compile_error())
|
||||
.into()
|
||||
}
|
||||
@ -98,10 +98,10 @@ pub fn derive_message_event_content(input: TokenStream) -> TokenStream {
|
||||
/// Generates an implementation of `ruma_events::StateEventContent` and it's super traits.
|
||||
#[proc_macro_derive(StateEventContent, attributes(ruma_event))]
|
||||
pub fn derive_state_event_content(input: TokenStream) -> TokenStream {
|
||||
let import_path = import_ruma_events();
|
||||
let ruma_events = import_ruma_events();
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
expand_state_event_content(&input, &import_path)
|
||||
expand_state_event_content(&input, &ruma_events)
|
||||
.unwrap_or_else(|err| err.to_compile_error())
|
||||
.into()
|
||||
}
|
||||
@ -109,10 +109,10 @@ pub fn derive_state_event_content(input: TokenStream) -> TokenStream {
|
||||
/// Generates an implementation of `ruma_events::EphemeralRoomEventContent` and it's super traits.
|
||||
#[proc_macro_derive(EphemeralRoomEventContent, attributes(ruma_event))]
|
||||
pub fn derive_ephemeral_room_event_content(input: TokenStream) -> TokenStream {
|
||||
let import_path = import_ruma_events();
|
||||
let ruma_events = import_ruma_events();
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
expand_ephemeral_room_event_content(&input, &import_path)
|
||||
expand_ephemeral_room_event_content(&input, &ruma_events)
|
||||
.unwrap_or_else(|err| err.to_compile_error())
|
||||
.into()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user