Remove support for state events in ruma_event!

This commit is contained in:
Jonas Platte 2020-06-04 11:29:48 +02:00
parent b95b7db0d4
commit 76cd387506
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
3 changed files with 2 additions and 184 deletions

View File

@ -53,9 +53,6 @@ impl From<RumaEventInput> for RumaEvent {
content_name.clone(),
input.fields.unwrap_or_else(Vec::new),
),
EventKind::StateEvent => {
populate_state_fields(content_name.clone(), input.fields.unwrap_or_else(Vec::new))
}
};
fields.sort_unstable_by_key(|field| field.ident.clone().unwrap());
@ -73,32 +70,20 @@ impl From<RumaEventInput> for RumaEvent {
}
impl ToTokens for RumaEvent {
// TODO: Maybe break this off into functions so it's not so large. Then remove the clippy
// allowance.
#[allow(clippy::cognitive_complexity)]
fn to_tokens(&self, tokens: &mut TokenStream) {
// let attrs = &self.attrs;
let content_name = &self.content_name;
// let event_fields = &self.fields;
let event_type = &self.event_type;
// let event_type = &self.event_type;
let name = &self.name;
let content_docstring = format!("The payload for `{}`.", name);
let content = match &self.content {
Content::Struct(fields) => {
// TODO this will all be removed so this is only temp
let event_content_derive = match self.kind {
EventKind::StateEvent => quote! {
#[derive(::ruma_events_macros::StateEventContent)]
#[ruma_event(type = #event_type)]
},
EventKind::RoomEvent | EventKind::Event => TokenStream::new(),
};
quote! {
#[doc = #content_docstring]
#[derive(Clone, Debug, ::serde::Serialize, ::ruma_events_macros::FromRaw)]
#event_content_derive
pub struct #content_name {
#(#fields),*
}
@ -160,24 +145,6 @@ fn populate_room_event_fields(content_name: Ident, fields: Vec<Field>) -> Vec<Fi
fields
}
/// Fills in the event's struct definition with fields common to all state events.
fn populate_state_fields(content_name: Ident, fields: Vec<Field>) -> Vec<Field> {
let mut fields = populate_room_event_fields(content_name.clone(), fields);
let punctuated_fields: Punctuated<ParsableNamedField, Token![,]> = parse_quote! {
/// The previous content for this state key, if any.
#[serde(skip_serializing_if = "Option::is_none")]
pub prev_content: Option<#content_name>,
/// A key that determines which piece of room state the event represents.
pub state_key: String,
};
fields.extend(punctuated_fields.into_iter().map(|p| p.field));
fields
}
/// A wrapper around `syn::Field` that makes it possible to parse `Punctuated<Field, Token![,]>`
/// from a `TokenStream`.
///

View File

@ -82,10 +82,8 @@ impl Parse for RumaEventInput {
EventKind::Event
} else if expr_path.path.is_ident("RoomEvent") {
EventKind::RoomEvent
} else if expr_path.path.is_ident("StateEvent") {
EventKind::StateEvent
} else {
panic!("value of field `kind` must be one of `Event`, `RoomEvent`, or `StateEvent`");
panic!("value of field `kind` must be one of `Event` or `RoomEvent`");
}
}
_ => panic!(
@ -140,9 +138,6 @@ pub enum EventKind {
/// A room event.
RoomEvent,
/// A state event.
StateEvent,
}
/// Information for generating the type used for the event's `content` field.

View File

@ -12,150 +12,6 @@ use ruma_events_macros::ruma_event;
use ruma_identifiers::{RoomAliasId, RoomId, UserId};
use serde_json::{from_value as from_json_value, json};
// See note about wrapping macro expansion in a module from `src/lib.rs`
mod common_case {
use super::*;
ruma_event! {
/// Informs the room about what room aliases it has been given.
AliasesEvent {
kind: StateEvent,
event_type: "m.room.aliases",
content: {
/// A list of room aliases.
pub aliases: Vec<ruma_identifiers::RoomAliasId>,
}
}
}
#[test]
fn optional_fields_as_none() {
let json = json!({
"content": {
"aliases": []
},
"event_id": "$h29iv0s8:example.com",
"origin_server_ts": 1,
"sender": "@carl:example.com",
"state_key": "example.com",
"type": "m.room.aliases"
});
assert_matches!(
from_json_value::<EventJson<AliasesEvent>>(json)
.unwrap()
.deserialize()
.unwrap(),
AliasesEvent {
content: AliasesEventContent { aliases },
event_id,
origin_server_ts,
prev_content: None,
room_id: None,
sender,
state_key,
unsigned,
} if aliases.is_empty()
&& event_id == "$h29iv0s8:example.com"
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
&& sender == "@carl:example.com"
&& state_key == "example.com"
&& unsigned.is_empty()
)
}
#[test]
fn some_optional_fields_as_some() {
let json = json!({
"content": {
"aliases": ["#room:example.org"]
},
"event_id": "$h29iv0s8:example.com",
"origin_server_ts": 1,
"prev_content": {
"aliases": []
},
"room_id": "!n8f893n9:example.com",
"sender": "@carl:example.com",
"state_key": "example.com",
"type": "m.room.aliases"
});
assert_matches!(
from_json_value::<EventJson<AliasesEvent>>(json)
.unwrap()
.deserialize()
.unwrap(),
AliasesEvent {
content: AliasesEventContent { aliases, },
event_id,
origin_server_ts,
prev_content: Some(AliasesEventContent { aliases: prev_aliases }),
room_id: Some(room_id),
sender,
state_key,
unsigned,
} if aliases == vec![RoomAliasId::try_from("#room:example.org").unwrap()]
&& event_id == "$h29iv0s8:example.com"
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
&& prev_aliases.is_empty()
&& room_id == "!n8f893n9:example.com"
&& sender == "@carl:example.com"
&& state_key == "example.com"
&& unsigned.is_empty()
);
}
#[test]
fn all_optional_fields_as_some() {
let json = json!({
"content": {
"aliases": ["#room:example.org"]
},
"event_id": "$h29iv0s8:example.com",
"origin_server_ts": 1,
"prev_content": {
"aliases": []
},
"room_id": "!n8f893n9:example.com",
"sender": "@carl:example.com",
"state_key": "example.com",
"unsigned": {
"age": 100
},
"type": "m.room.aliases"
});
assert_matches!(
from_json_value::<EventJson<AliasesEvent>>(json)
.unwrap()
.deserialize()
.unwrap(),
AliasesEvent {
content: AliasesEventContent { aliases },
event_id,
origin_server_ts,
prev_content: Some(AliasesEventContent { aliases: prev_aliases }),
room_id: Some(room_id),
sender,
state_key,
unsigned: UnsignedData {
age: Some(age),
redacted_because: None,
transaction_id: None,
},
} if aliases == vec![RoomAliasId::try_from("#room:example.org").unwrap()]
&& event_id == "$h29iv0s8:example.com"
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
&& prev_aliases.is_empty()
&& room_id == "!n8f893n9:example.com"
&& sender == "@carl:example.com"
&& state_key == "example.com"
&& age == Int::from(100)
);
}
}
mod extra_fields {
use super::*;