Add is_compatible method to event content enums to fix Any*Event deserialization
… and add some benchmarks for it
This commit is contained in:
parent
c40e3974e7
commit
0381190bdb
6 changed files with 466 additions and 6 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,2 +1,2 @@
|
|||
/target
|
||||
target
|
||||
Cargo.lock
|
||||
|
|
|
@ -8,9 +8,12 @@ use syn::{
|
|||
};
|
||||
|
||||
/// Create a content enum from `ContentEnumInput`.
|
||||
///
|
||||
/// This is the internals of the `event_content_enum!` macro.
|
||||
pub fn expand_content_enum(input: ContentEnumInput) -> syn::Result<TokenStream> {
|
||||
let attrs = &input.attrs;
|
||||
let ident = &input.name;
|
||||
|
||||
let event_type_str = &input.events;
|
||||
|
||||
let variants = input.events.iter().map(to_camel_case).collect::<Vec<_>>();
|
||||
|
@ -54,11 +57,24 @@ pub fn expand_content_enum(input: ContentEnumInput) -> syn::Result<TokenStream>
|
|||
}
|
||||
};
|
||||
|
||||
let any_event_variant_impl = quote! {
|
||||
impl #ident {
|
||||
fn is_compatible(event_type: &str) -> bool {
|
||||
match event_type {
|
||||
#( #event_type_str => true, )*
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let marker_trait_impls = marker_traits(ident);
|
||||
|
||||
Ok(quote! {
|
||||
#content_enum
|
||||
|
||||
#any_event_variant_impl
|
||||
|
||||
#event_content_impl
|
||||
|
||||
#marker_trait_impls
|
||||
|
|
|
@ -27,3 +27,8 @@ maplit = "1.0.2"
|
|||
matches = "0.1.8"
|
||||
ruma-identifiers = { version = "0.16.2", path = "../ruma-identifiers", features = ["rand"] }
|
||||
trybuild = "1.0.28"
|
||||
criterion = "0.3.2"
|
||||
|
||||
[[bench]]
|
||||
name = "event_deserialize"
|
||||
harness = false
|
||||
|
|
109
ruma-events/benches/event_deserialize.rs
Normal file
109
ruma-events/benches/event_deserialize.rs
Normal file
|
@ -0,0 +1,109 @@
|
|||
// `cargo bench` works, but if you use `cargo bench -- --save-baseline <name>`
|
||||
// or pass any other args to it, it fails with the error
|
||||
// `cargo bench unknown option --save-baseline`.
|
||||
// To pass args to criterion, use this form
|
||||
// `cargo bench --bench <name of the bench> -- --save-baseline <name>`.
|
||||
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
use ruma_events::{
|
||||
room::power_levels::PowerLevelsEventContent, AnyEvent, AnyRoomEvent, AnyStateEvent, EventJson,
|
||||
StateEvent,
|
||||
};
|
||||
use serde_json::json;
|
||||
|
||||
fn power_levels() -> serde_json::Value {
|
||||
json!({
|
||||
"content": {
|
||||
"ban": 50,
|
||||
"events": {
|
||||
"m.room.avatar": 50,
|
||||
"m.room.canonical_alias": 50,
|
||||
"m.room.history_visibility": 100,
|
||||
"m.room.name": 50,
|
||||
"m.room.power_levels": 100
|
||||
},
|
||||
"events_default": 0,
|
||||
"invite": 0,
|
||||
"kick": 50,
|
||||
"redact": 50,
|
||||
"state_default": 50,
|
||||
"users": {
|
||||
"@example:localhost": 100
|
||||
},
|
||||
"users_default": 0
|
||||
},
|
||||
"event_id": "$15139375512JaHAW:localhost",
|
||||
"origin_server_ts": 45,
|
||||
"sender": "@example:localhost",
|
||||
"room_id": "!room:localhost",
|
||||
"state_key": "",
|
||||
"type": "m.room.power_levels",
|
||||
"unsigned": {
|
||||
"age": 45
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn deserialize_any_event(c: &mut Criterion) {
|
||||
let json_data = power_levels();
|
||||
|
||||
c.bench_function("deserialize to `AnyEvent`", |b| {
|
||||
b.iter(|| {
|
||||
let _ = serde_json::from_value::<EventJson<AnyEvent>>(json_data.clone())
|
||||
.unwrap()
|
||||
.deserialize()
|
||||
.unwrap();
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
fn deserialize_any_room_event(c: &mut Criterion) {
|
||||
let json_data = power_levels();
|
||||
|
||||
c.bench_function("deserialize to `AnyRoomEvent`", |b| {
|
||||
b.iter(|| {
|
||||
let _ = serde_json::from_value::<EventJson<AnyRoomEvent>>(json_data.clone())
|
||||
.unwrap()
|
||||
.deserialize()
|
||||
.unwrap();
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
fn deserialize_any_state_event(c: &mut Criterion) {
|
||||
let json_data = power_levels();
|
||||
|
||||
c.bench_function("deserialize to `AnyStateEvent`", |b| {
|
||||
b.iter(|| {
|
||||
let _ = serde_json::from_value::<EventJson<AnyStateEvent>>(json_data.clone())
|
||||
.unwrap()
|
||||
.deserialize()
|
||||
.unwrap();
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
fn deserialize_specific_event(c: &mut Criterion) {
|
||||
let json_data = power_levels();
|
||||
|
||||
c.bench_function("deserialize to `StateEvent<PowerLevelsEventContent>`", |b| {
|
||||
b.iter(|| {
|
||||
let _ = serde_json::from_value::<EventJson<StateEvent<PowerLevelsEventContent>>>(
|
||||
json_data.clone(),
|
||||
)
|
||||
.unwrap()
|
||||
.deserialize()
|
||||
.unwrap();
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(
|
||||
benches,
|
||||
deserialize_any_event,
|
||||
deserialize_any_room_event,
|
||||
deserialize_any_state_event,
|
||||
deserialize_specific_event
|
||||
);
|
||||
|
||||
criterion_main!(benches);
|
|
@ -1,5 +1,9 @@
|
|||
use ruma_events_macros::event_content_enum;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::{
|
||||
de::{self, Error as _},
|
||||
Serialize,
|
||||
};
|
||||
use serde_json::{from_value as from_json_value, Value as JsonValue};
|
||||
|
||||
use crate::{
|
||||
event_kinds::{
|
||||
|
@ -8,6 +12,7 @@ use crate::{
|
|||
},
|
||||
presence::PresenceEvent,
|
||||
room::redaction::{RedactionEvent, RedactionEventStub},
|
||||
util,
|
||||
};
|
||||
|
||||
event_content_enum! {
|
||||
|
@ -116,7 +121,7 @@ pub type AnyStrippedStateEventStub = StrippedStateEventStub<AnyStateEventContent
|
|||
pub type AnyToDeviceEvent = ToDeviceEvent<AnyToDeviceEventContent>;
|
||||
|
||||
/// Any event.
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum AnyEvent {
|
||||
/// Any basic event.
|
||||
|
@ -134,7 +139,7 @@ pub enum AnyEvent {
|
|||
}
|
||||
|
||||
/// Any room event.
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum AnyRoomEvent {
|
||||
/// Any message event.
|
||||
|
@ -146,7 +151,7 @@ pub enum AnyRoomEvent {
|
|||
}
|
||||
|
||||
/// Any room event stub (room event without a `room_id`, as returned in `/sync` responses)
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum AnyRoomEventStub {
|
||||
/// Any message event stub
|
||||
|
@ -154,5 +159,85 @@ pub enum AnyRoomEventStub {
|
|||
/// `"m.room.redaction"` stub
|
||||
Redaction(RedactionEventStub),
|
||||
/// Any state event stub
|
||||
StateEvent(AnyStateEventStub),
|
||||
State(AnyStateEventStub),
|
||||
}
|
||||
|
||||
// FIXME `#[serde(untagged)]` deserialization fails for these enums which
|
||||
// is odd as we are doing basically the same thing here, investigate?
|
||||
impl<'de> de::Deserialize<'de> for AnyEvent {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: de::Deserializer<'de>,
|
||||
{
|
||||
let json = JsonValue::deserialize(deserializer)?;
|
||||
let ev_type: String = util::get_field(&json, "type")?;
|
||||
|
||||
match ev_type.as_str() {
|
||||
"m.room.redaction" => {
|
||||
Ok(AnyEvent::Redaction(from_json_value(json).map_err(D::Error::custom)?))
|
||||
}
|
||||
"m.presence" => {
|
||||
Ok(AnyEvent::Presence(from_json_value(json).map_err(D::Error::custom)?))
|
||||
}
|
||||
ev_type if AnyBasicEventContent::is_compatible(ev_type) => {
|
||||
Ok(AnyEvent::Basic(from_json_value(json).map_err(D::Error::custom)?))
|
||||
}
|
||||
ev_type if AnyEphemeralRoomEventContent::is_compatible(ev_type) => {
|
||||
Ok(AnyEvent::Ephemeral(from_json_value(json).map_err(D::Error::custom)?))
|
||||
}
|
||||
ev_type if AnyMessageEventContent::is_compatible(ev_type) => {
|
||||
Ok(AnyEvent::Message(from_json_value(json).map_err(D::Error::custom)?))
|
||||
}
|
||||
ev_type if AnyStateEventContent::is_compatible(ev_type) => {
|
||||
Ok(AnyEvent::State(from_json_value(json).map_err(D::Error::custom)?))
|
||||
}
|
||||
_ => Err(D::Error::custom(format!("event type `{}` is not a valid event", ev_type))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> de::Deserialize<'de> for AnyRoomEvent {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: de::Deserializer<'de>,
|
||||
{
|
||||
let json = JsonValue::deserialize(deserializer)?;
|
||||
let ev_type: String = util::get_field(&json, "type")?;
|
||||
|
||||
match ev_type.as_str() {
|
||||
"m.room.redaction" => {
|
||||
Ok(AnyRoomEvent::Redaction(from_json_value(json).map_err(D::Error::custom)?))
|
||||
}
|
||||
ev_type if AnyMessageEventContent::is_compatible(ev_type) => {
|
||||
Ok(AnyRoomEvent::Message(from_json_value(json).map_err(D::Error::custom)?))
|
||||
}
|
||||
ev_type if AnyStateEventContent::is_compatible(ev_type) => {
|
||||
Ok(AnyRoomEvent::State(from_json_value(json).map_err(D::Error::custom)?))
|
||||
}
|
||||
_ => Err(D::Error::custom(format!("event type `{}` is not a valid event", ev_type))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> de::Deserialize<'de> for AnyRoomEventStub {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: de::Deserializer<'de>,
|
||||
{
|
||||
let json = JsonValue::deserialize(deserializer)?;
|
||||
let ev_type: String = util::get_field(&json, "type")?;
|
||||
|
||||
match ev_type.as_str() {
|
||||
"m.room.redaction" => {
|
||||
Ok(AnyRoomEventStub::Redaction(from_json_value(json).map_err(D::Error::custom)?))
|
||||
}
|
||||
ev_type if AnyMessageEventContent::is_compatible(ev_type) => {
|
||||
Ok(AnyRoomEventStub::Message(from_json_value(json).map_err(D::Error::custom)?))
|
||||
}
|
||||
ev_type if AnyStateEventContent::is_compatible(ev_type) => {
|
||||
Ok(AnyRoomEventStub::State(from_json_value(json).map_err(D::Error::custom)?))
|
||||
}
|
||||
_ => Err(D::Error::custom(format!("event type `{}` is not a valid event", ev_type))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
245
ruma-events/tests/enums.rs
Normal file
245
ruma-events/tests/enums.rs
Normal file
|
@ -0,0 +1,245 @@
|
|||
use std::convert::TryFrom;
|
||||
|
||||
use matches::assert_matches;
|
||||
use ruma_identifiers::RoomAliasId;
|
||||
use serde_json::{from_value as from_json_value, json, Value as JsonValue};
|
||||
|
||||
use ruma_events::{
|
||||
room::{
|
||||
aliases::AliasesEventContent,
|
||||
message::{MessageEventContent, TextMessageEventContent},
|
||||
power_levels::PowerLevelsEventContent,
|
||||
},
|
||||
AnyEvent, AnyMessageEventContent, AnyRoomEvent, AnyRoomEventStub, AnyStateEventContent,
|
||||
MessageEvent, MessageEventStub, StateEvent, StateEventStub,
|
||||
};
|
||||
|
||||
fn message_event() -> JsonValue {
|
||||
json!({
|
||||
"content": {
|
||||
"body": "baba",
|
||||
"format": "org.matrix.custom.html",
|
||||
"formatted_body": "<strong>baba</strong>",
|
||||
"msgtype": "m.text"
|
||||
},
|
||||
"event_id": "$152037280074GZeOm:localhost",
|
||||
"origin_server_ts": 1,
|
||||
"sender": "@example:localhost",
|
||||
"room_id": "!room:room.com",
|
||||
"type": "m.room.message",
|
||||
"unsigned": {
|
||||
"age": 1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn message_event_stub() -> JsonValue {
|
||||
json!({
|
||||
"content": {
|
||||
"body": "baba",
|
||||
"format": "org.matrix.custom.html",
|
||||
"formatted_body": "<strong>baba</strong>",
|
||||
"msgtype": "m.text"
|
||||
},
|
||||
"event_id": "$152037280074GZeOm:localhost",
|
||||
"origin_server_ts": 1,
|
||||
"sender": "@example:localhost",
|
||||
"type": "m.room.message",
|
||||
"unsigned": {
|
||||
"age": 1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn aliases_event() -> JsonValue {
|
||||
json!({
|
||||
"content": {
|
||||
"aliases": ["#somewhere:localhost"]
|
||||
},
|
||||
"event_id": "$152037280074GZeOm:localhost",
|
||||
"origin_server_ts": 1,
|
||||
"sender": "@example:localhost",
|
||||
"state_key": "",
|
||||
"room_id": "!room:room.com",
|
||||
"type": "m.room.aliases",
|
||||
"unsigned": {
|
||||
"age": 1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn aliases_event_stub() -> JsonValue {
|
||||
json!({
|
||||
"content": {
|
||||
"aliases": ["#somewhere:localhost"]
|
||||
},
|
||||
"event_id": "$152037280074GZeOm:localhost",
|
||||
"origin_server_ts": 1,
|
||||
"sender": "@example:localhost",
|
||||
"state_key": "",
|
||||
"type": "m.room.aliases",
|
||||
"unsigned": {
|
||||
"age": 1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn power_event_stub_deserialization() {
|
||||
let json_data = json!({
|
||||
"content": {
|
||||
"ban": 50,
|
||||
"events": {
|
||||
"m.room.avatar": 50,
|
||||
"m.room.canonical_alias": 50,
|
||||
"m.room.history_visibility": 100,
|
||||
"m.room.name": 50,
|
||||
"m.room.power_levels": 100
|
||||
},
|
||||
"events_default": 0,
|
||||
"invite": 0,
|
||||
"kick": 50,
|
||||
"redact": 50,
|
||||
"state_default": 50,
|
||||
"users": {
|
||||
"@example:localhost": 100
|
||||
},
|
||||
"users_default": 0
|
||||
},
|
||||
"event_id": "$15139375512JaHAW:localhost",
|
||||
"origin_server_ts": 45,
|
||||
"sender": "@example:localhost",
|
||||
"state_key": "",
|
||||
"type": "m.room.power_levels",
|
||||
"unsigned": {
|
||||
"age": 45
|
||||
}
|
||||
});
|
||||
|
||||
assert_matches!(
|
||||
from_json_value::<AnyRoomEventStub>(json_data),
|
||||
Ok(AnyRoomEventStub::State(
|
||||
StateEventStub {
|
||||
content: AnyStateEventContent::RoomPowerLevels(PowerLevelsEventContent {
|
||||
ban, ..
|
||||
}),
|
||||
..
|
||||
}
|
||||
))
|
||||
if ban == js_int::Int::new(50).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_event_stub_deserialization() {
|
||||
let json_data = message_event_stub();
|
||||
|
||||
assert_matches!(
|
||||
from_json_value::<AnyRoomEventStub>(json_data),
|
||||
Ok(AnyRoomEventStub::Message(
|
||||
MessageEventStub {
|
||||
content: AnyMessageEventContent::RoomMessage(MessageEventContent::Text(TextMessageEventContent {
|
||||
body,
|
||||
formatted: Some(formatted),
|
||||
relates_to: None,
|
||||
})),
|
||||
..
|
||||
}
|
||||
))
|
||||
if body == "baba" && formatted.body == "<strong>baba</strong>"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aliases_event_stub_deserialization() {
|
||||
let json_data = aliases_event_stub();
|
||||
|
||||
assert_matches!(
|
||||
from_json_value::<AnyRoomEventStub>(json_data),
|
||||
Ok(AnyRoomEventStub::State(
|
||||
StateEventStub {
|
||||
content: AnyStateEventContent::RoomAliases(AliasesEventContent {
|
||||
aliases,
|
||||
}),
|
||||
..
|
||||
}
|
||||
))
|
||||
if aliases == vec![ RoomAliasId::try_from("#somewhere:localhost").unwrap() ]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_room_event_deserialization() {
|
||||
let json_data = message_event();
|
||||
|
||||
assert_matches!(
|
||||
from_json_value::<AnyRoomEvent>(json_data),
|
||||
Ok(AnyRoomEvent::Message(
|
||||
MessageEvent {
|
||||
content: AnyMessageEventContent::RoomMessage(MessageEventContent::Text(TextMessageEventContent {
|
||||
body,
|
||||
formatted: Some(formatted),
|
||||
relates_to: None,
|
||||
})),
|
||||
..
|
||||
}
|
||||
))
|
||||
if body == "baba" && formatted.body == "<strong>baba</strong>"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alias_room_event_deserialization() {
|
||||
let json_data = aliases_event();
|
||||
|
||||
assert_matches!(
|
||||
from_json_value::<AnyRoomEvent>(json_data),
|
||||
Ok(AnyRoomEvent::State(
|
||||
StateEvent {
|
||||
content: AnyStateEventContent::RoomAliases(AliasesEventContent {
|
||||
aliases,
|
||||
}),
|
||||
..
|
||||
}
|
||||
))
|
||||
if aliases == vec![ RoomAliasId::try_from("#somewhere:localhost").unwrap() ]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_event_deserialization() {
|
||||
let json_data = message_event();
|
||||
|
||||
assert_matches!(
|
||||
from_json_value::<AnyEvent>(json_data),
|
||||
Ok(AnyEvent::Message(
|
||||
MessageEvent {
|
||||
content: AnyMessageEventContent::RoomMessage(MessageEventContent::Text(TextMessageEventContent {
|
||||
body,
|
||||
formatted: Some(formatted),
|
||||
relates_to: None,
|
||||
})),
|
||||
..
|
||||
}
|
||||
))
|
||||
if body == "baba" && formatted.body == "<strong>baba</strong>"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alias_event_deserialization() {
|
||||
let json_data = aliases_event();
|
||||
|
||||
assert_matches!(
|
||||
from_json_value::<AnyEvent>(json_data),
|
||||
Ok(AnyEvent::State(
|
||||
StateEvent {
|
||||
content: AnyStateEventContent::RoomAliases(AliasesEventContent {
|
||||
aliases,
|
||||
}),
|
||||
..
|
||||
}
|
||||
))
|
||||
if aliases == vec![ RoomAliasId::try_from("#somewhere:localhost").unwrap() ]
|
||||
);
|
||||
}
|
Loading…
Reference in a new issue