events: Make fields of doc(hidden) struct private

This commit is contained in:
Jonas Platte 2021-05-16 22:34:24 +02:00
parent 61a033db58
commit 7626135faf
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 14 additions and 21 deletions

View File

@ -828,11 +828,11 @@ pub struct KeyVerificationRequestEventContent {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CustomEventContent {
/// A custom msgtype
pub msgtype: String,
msgtype: String,
/// Remaining event content
#[serde(flatten)]
pub data: JsonObject,
data: JsonObject,
}
fn get_plain_quote_fallback(original_message: &MessageEvent) -> String {

View File

@ -1,3 +1,5 @@
use std::borrow::Cow;
use assign::assign;
use js_int::uint;
use matches::assert_matches;
@ -9,8 +11,8 @@ use ruma_events::{
use ruma_events::{
room::{
message::{
AudioMessageEventContent, CustomEventContent, MessageEvent, MessageEventContent,
MessageType, Relation, TextMessageEventContent,
AudioMessageEventContent, MessageEvent, MessageEventContent, MessageType, Relation,
TextMessageEventContent,
},
relationships::InReplyTo,
},
@ -86,18 +88,15 @@ fn content_serialization() {
}
#[test]
fn custom_content_serialization() {
fn custom_msgtype_serialization() {
let json_data = json_object! {
"custom_field".into() => json!("baba"),
"another_one".into() => json!("abab"),
};
let custom_event_content = MessageType::_Custom(CustomEventContent {
msgtype: "my_custom_msgtype".into(),
data: json_data,
});
let custom_msgtype = MessageType::new("my_custom_msgtype", json_data).unwrap();
assert_eq!(
to_json_value(&custom_event_content).unwrap(),
to_json_value(&custom_msgtype).unwrap(),
json!({
"msgtype": "my_custom_msgtype",
"custom_field": "baba",
@ -119,17 +118,11 @@ fn custom_content_deserialization() {
"another_one".into() => json!("abab"),
};
assert_matches!(
from_json_value::<Raw<MessageType>>(json_data)
.unwrap()
.deserialize()
.unwrap(),
MessageType::_Custom(CustomEventContent {
msgtype,
data
}) if msgtype == "my_custom_msgtype"
&& data == expected_json_data
);
let custom_event =
from_json_value::<Raw<MessageType>>(json_data).unwrap().deserialize().unwrap();
assert_eq!(custom_event.msgtype(), "my_custom_msgtype");
assert_eq!(custom_event.data(), Cow::Owned(expected_json_data));
}
#[test]