events: Rename & hide event enum Custom variant

This commit is contained in:
Jonas Platte 2021-06-17 19:08:19 +02:00
parent ec4b719546
commit 349a57ffc7
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
4 changed files with 39 additions and 124 deletions

View File

@ -254,8 +254,8 @@ fn expand_conversion_impl(
}, },
)* )*
#redaction #redaction
#ident::Custom(event) => { #ident::_Custom(event) => {
Self::Custom(#ruma_events::#sync_struct::from(event)) Self::_Custom(#ruma_events::#sync_struct::from(event))
}, },
} }
} }
@ -297,8 +297,8 @@ fn expand_conversion_impl(
}, },
)* )*
#redaction #redaction
Self::Custom(event) => { Self::_Custom(event) => {
#full::Custom(event.into_full_event(room_id)) #full::_Custom(event.into_full_event(room_id))
}, },
} }
} }
@ -386,7 +386,8 @@ fn expand_content_enum(
#variant_decls(#content), #variant_decls(#content),
)* )*
/// Content of an event not defined by the Matrix specification. /// Content of an event not defined by the Matrix specification.
Custom(#ruma_events::custom::CustomEventContent), #[doc(hidden)]
_Custom(#ruma_events::custom::CustomEventContent),
} }
}; };
@ -403,7 +404,7 @@ fn expand_content_enum(
fn event_type(&self) -> &::std::primitive::str { fn event_type(&self) -> &::std::primitive::str {
match self { match self {
#( #variant_arms(content) => content.event_type(), )* #( #variant_arms(content) => content.event_type(), )*
Self::Custom(content) => content.event_type(), Self::_Custom(content) => content.event_type(),
} }
} }
@ -421,7 +422,7 @@ fn expand_content_enum(
ev_type => { ev_type => {
let content = let content =
#ruma_events::custom::CustomEventContent::from_parts(ev_type, input)?; #ruma_events::custom::CustomEventContent::from_parts(ev_type, input)?;
::std::result::Result::Ok(Self::Custom(content)) ::std::result::Result::Ok(Self::_Custom(content))
}, },
} }
} }
@ -449,7 +450,8 @@ fn expand_content_enum(
#variant_decls(#redacted_content), #variant_decls(#redacted_content),
)* )*
/// Content of a redacted event not defined by the Matrix specification. /// Content of a redacted event not defined by the Matrix specification.
Custom(#ruma_events::custom::RedactedCustomEventContent), #[doc(hidden)]
_Custom(#ruma_events::custom::RedactedCustomEventContent),
} }
impl #ruma_events::RedactContent for #ident { impl #ruma_events::RedactContent for #ident {
@ -468,8 +470,8 @@ fn expand_content_enum(
) )
}, },
)* )*
Self::Custom(content) => { Self::_Custom(content) => {
#redacted_ident::Custom( #redacted_ident::_Custom(
#ruma_events::RedactContent::redact(content, version) #ruma_events::RedactContent::redact(content, version)
) )
}, },
@ -557,9 +559,9 @@ fn expand_redact(
}) })
} }
)* )*
Self::Custom(event) => { Self::_Custom(event) => {
let content = #ruma_events::RedactContent::redact(event.content, version); let content = #ruma_events::RedactContent::redact(event.content, version);
#redacted_enum::Custom(#redacted_type { #redacted_enum::_Custom(#redacted_type {
content, content,
#fields #fields
}) })
@ -661,7 +663,8 @@ fn generate_custom_variant(
( (
quote! { quote! {
/// A redacted event not defined by the Matrix specification /// A redacted event not defined by the Matrix specification
Custom( #[doc(hidden)]
_Custom(
#ruma_events::#event_struct<#ruma_events::custom::RedactedCustomEventContent>, #ruma_events::#event_struct<#ruma_events::custom::RedactedCustomEventContent>,
), ),
}, },
@ -672,7 +675,7 @@ fn generate_custom_variant(
>>(json.get()) >>(json.get())
.map_err(D::Error::custom)?; .map_err(D::Error::custom)?;
Ok(Self::Custom(event)) Ok(Self::_Custom(event))
}, },
}, },
) )
@ -680,7 +683,8 @@ fn generate_custom_variant(
( (
quote! { quote! {
/// An event not defined by the Matrix specification /// An event not defined by the Matrix specification
Custom(#ruma_events::#event_struct<#ruma_events::custom::CustomEventContent>), #[doc(hidden)]
_Custom(#ruma_events::#event_struct<#ruma_events::custom::CustomEventContent>),
}, },
quote! { quote! {
event => { event => {
@ -690,7 +694,7 @@ fn generate_custom_variant(
>(json.get()) >(json.get())
.map_err(D::Error::custom)?; .map_err(D::Error::custom)?;
Ok(Self::Custom(event)) Ok(Self::_Custom(event))
}, },
}, },
) )
@ -745,7 +749,7 @@ fn accessor_methods(
match self { match self {
#( #self_variants(event) => #( #self_variants(event) =>
#ruma_events::EventContent::event_type(&event.content), )* #ruma_events::EventContent::event_type(&event.content), )*
Self::Custom(event) => Self::_Custom(event) =>
#ruma_events::EventContent::event_type(&event.content), #ruma_events::EventContent::event_type(&event.content),
} }
} }
@ -756,7 +760,7 @@ fn accessor_methods(
pub fn content(&self) -> #content_enum { pub fn content(&self) -> #content_enum {
match self { match self {
#( #self_variants(event) => #content_variants(event.content.clone()), )* #( #self_variants(event) => #content_variants(event.content.clone()), )*
Self::Custom(event) => #content_enum::Custom(event.content.clone()), Self::_Custom(event) => #content_enum::_Custom(event.content.clone()),
} }
} }
}; };
@ -771,8 +775,8 @@ fn accessor_methods(
event.prev_content.as_ref().map(|c| #content_variants(c.clone())) event.prev_content.as_ref().map(|c| #content_variants(c.clone()))
}, },
)* )*
Self::Custom(event) => { Self::_Custom(event) => {
event.prev_content.as_ref().map(|c| #content_enum::Custom(c.clone())) event.prev_content.as_ref().map(|c| #content_enum::_Custom(c.clone()))
}, },
} }
} }
@ -955,7 +959,7 @@ fn generate_accessor(
pub fn #ident(&self) -> &#field_type { pub fn #ident(&self) -> &#field_type {
match self { match self {
#( #variants(event) => &event.#ident, )* #( #variants(event) => &event.#ident, )*
Self::Custom(event) => &event.#ident, Self::_Custom(event) => &event.#ident,
} }
} }
} }

View File

@ -7,6 +7,7 @@ Breaking changes:
* Additionally, fix the name of the QR code one (`QrScanShowV1` to `QrCodeScanV1`) * Additionally, fix the name of the QR code one (`QrScanShowV1` to `QrCodeScanV1`)
* Remove `room::power_level::NotificationPowerLevels`, now found in `ruma_common::power_levels` * Remove `room::power_level::NotificationPowerLevels`, now found in `ruma_common::power_levels`
(or `ruma::power_levels`) (or `ruma::power_levels`)
* Remove `Custom` variant from event enums. If you were using this, please get in touch.
# 0.22.2 # 0.22.2

View File

@ -3,12 +3,10 @@ use maplit::btreemap;
use matches::assert_matches; use matches::assert_matches;
use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_common::MilliSecondsSinceUnixEpoch;
use ruma_events::{ use ruma_events::{
custom::CustomEventContent, AnyMessageEvent, AnyStateEvent, AnyStateEventContent, custom::CustomEventContent, AnyStateEvent, AnySyncRoomEvent, AnySyncStateEvent, MessageEvent,
AnySyncMessageEvent, AnySyncRoomEvent, MessageEvent, StateEvent, SyncMessageEvent, StateEvent, Unsigned,
SyncStateEvent, Unsigned,
}; };
use ruma_identifiers::{event_id, room_id, user_id}; use ruma_identifiers::{event_id, room_id, user_id};
use ruma_serde::Raw;
use serde_json::{ use serde_json::{
from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue, from_value as from_json_value, json, to_value as to_json_value, Value as JsonValue,
}; };
@ -36,7 +34,7 @@ fn custom_state_event() -> JsonValue {
#[test] #[test]
fn serialize_custom_message_event() { fn serialize_custom_message_event() {
let aliases_event = AnyMessageEvent::Custom(MessageEvent { let aliases_event = MessageEvent {
content: CustomEventContent { content: CustomEventContent {
data: btreemap! { data: btreemap! {
"body".into() => " * edited message".into(), "body".into() => " * edited message".into(),
@ -57,7 +55,7 @@ fn serialize_custom_message_event() {
room_id: room_id!("!room:room.com"), room_id: room_id!("!room:room.com"),
sender: user_id!("@carl:example.com"), sender: user_id!("@carl:example.com"),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
}); };
let actual = to_json_value(&aliases_event).unwrap(); let actual = to_json_value(&aliases_event).unwrap();
let expected = json!({ let expected = json!({
@ -85,7 +83,7 @@ fn serialize_custom_message_event() {
#[test] #[test]
fn serialize_custom_state_event() { fn serialize_custom_state_event() {
let aliases_event = AnyStateEvent::Custom(StateEvent { let aliases_event = StateEvent {
content: CustomEventContent { content: CustomEventContent {
data: btreemap! { data: btreemap! {
"custom".into() => 10.into() "custom".into() => 10.into()
@ -99,7 +97,7 @@ fn serialize_custom_state_event() {
sender: user_id!("@carl:example.com"), sender: user_id!("@carl:example.com"),
state_key: "".into(), state_key: "".into(),
unsigned: Unsigned::default(), unsigned: Unsigned::default(),
}); };
let actual = to_json_value(&aliases_event).unwrap(); let actual = to_json_value(&aliases_event).unwrap();
let expected = json!({ let expected = json!({
@ -120,74 +118,13 @@ fn serialize_custom_state_event() {
#[test] #[test]
fn deserialize_custom_state_event() { fn deserialize_custom_state_event() {
let json_data = custom_state_event(); let json_data = custom_state_event();
assert_matches!(from_json_value::<AnyStateEvent>(json_data), Ok(_));
let expected_content = btreemap! {
"m.relates_to".into() => json!({
"event_id": "$MDitXXXXXX",
"key": "👍",
"rel_type": "m.annotation"
}),
};
assert_matches!(
from_json_value::<Raw<AnyStateEvent>>(json_data)
.unwrap()
.deserialize()
.unwrap(),
AnyStateEvent::Custom(StateEvent {
content: CustomEventContent {
data, event_type,
},
event_id,
origin_server_ts,
sender,
room_id,
prev_content: None,
state_key,
unsigned,
}) if data == expected_content && event_type == "m.reaction"
&& event_id == event_id!("$h29iv0s8:example.com")
&& origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(10))
&& sender == user_id!("@carl:example.com")
&& room_id == room_id!("!room:room.com")
&& state_key.is_empty()
&& !unsigned.is_empty()
);
} }
#[test] #[test]
fn deserialize_custom_state_sync_event() { fn deserialize_custom_state_sync_event() {
let json_data = custom_state_event(); let json_data = custom_state_event();
assert_matches!(from_json_value::<AnySyncStateEvent>(json_data), Ok(_));
let expected_content = btreemap! {
"m.relates_to".into() => json!({
"event_id": "$MDitXXXXXX",
"key": "👍",
"rel_type": "m.annotation"
}),
};
assert_matches!(
from_json_value::<SyncStateEvent<AnyStateEventContent>>(json_data)
.unwrap(),
SyncStateEvent {
content: AnyStateEventContent::Custom(CustomEventContent {
data,
event_type,
}),
event_id,
origin_server_ts,
sender,
prev_content: None,
state_key,
unsigned,
} if data == expected_content && event_type == "m.reaction"
&& event_id == event_id!("$h29iv0s8:example.com")
&& origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(10))
&& sender == user_id!("@carl:example.com")
&& state_key.is_empty()
&& !unsigned.is_empty()
);
} }
#[test] #[test]
@ -206,23 +143,8 @@ fn deserialize_custom_message_sync_event() {
} }
}); });
let expected_content = btreemap! {
"body".into() => "👍".into(),
};
assert_matches!( assert_matches!(
from_json_value::<AnySyncRoomEvent>(json_data) from_json_value::<AnySyncRoomEvent>(json_data),
.unwrap(), Ok(AnySyncRoomEvent::Message(_))
AnySyncRoomEvent::Message(AnySyncMessageEvent::Custom(SyncMessageEvent {
content: CustomEventContent { data, event_type },
event_id,
origin_server_ts,
sender,
unsigned,
})) if data == expected_content && event_type == "m.ruma_custom"
&& event_id == event_id!("$h29iv0s8:example.com")
&& origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(10))
&& sender == user_id!("@carl:example.com")
&& !unsigned.is_empty()
); );
} }

View File

@ -248,23 +248,11 @@ fn redacted_custom_event_serialize() {
}); });
assert_matches!( assert_matches!(
from_json_value::<Raw<AnySyncRoomEvent>>(redacted.clone()) from_json_value::<AnySyncRoomEvent>(redacted.clone()),
.unwrap() Ok(AnySyncRoomEvent::RedactedState(_))
.deserialize()
.unwrap(),
AnySyncRoomEvent::RedactedState(AnyRedactedSyncStateEvent::Custom(RedactedSyncStateEvent {
content: RedactedCustomEventContent {
event_type,
},
event_id, state_key, unsigned, ..
})) if event_id == event_id!("$h29iv0s8:example.com")
&& unsigned.redacted_because.is_some()
&& state_key == "hello there"
&& event_type == "m.made.up"
); );
let x = let x = from_json_value::<AnyRedactedSyncStateEvent>(redacted).unwrap();
from_json_value::<Raw<AnyRedactedSyncStateEvent>>(redacted).unwrap().deserialize().unwrap();
assert_eq!(x.event_id(), &event_id!("$h29iv0s8:example.com")) assert_eq!(x.event_id(), &event_id!("$h29iv0s8:example.com"))
} }