events: Make the json_object macro syntax easier to read
This commit is contained in:
parent
32901da35f
commit
f29bbebf08
@ -24,22 +24,19 @@ use ruma_common::{
|
|||||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||||
|
|
||||||
macro_rules! json_object {
|
macro_rules! json_object {
|
||||||
( $($key:expr => $value:expr),* $(,)? ) => {
|
( $($tt:tt)+ ) => {
|
||||||
{
|
match serde_json::json!({ $($tt)+ }) {
|
||||||
let mut _map = serde_json::Map::<String, serde_json::Value>::new();
|
serde_json::value::Value::Object(map) => map,
|
||||||
$(
|
_ => panic!("Not a JSON object"),
|
||||||
let _ = _map.insert($key, $value);
|
}
|
||||||
)*
|
|
||||||
_map
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn custom_msgtype_serialization() {
|
fn custom_msgtype_serialization() {
|
||||||
let json_data = json_object! {
|
let json_data = json_object! {
|
||||||
"custom_field".into() => json!("baba"),
|
"custom_field": "baba",
|
||||||
"another_one".into() => json!("abab"),
|
"another_one": "abab",
|
||||||
};
|
};
|
||||||
let custom_msgtype =
|
let custom_msgtype =
|
||||||
MessageType::new("my_custom_msgtype", "my message body".into(), json_data).unwrap();
|
MessageType::new("my_custom_msgtype", "my message body".into(), json_data).unwrap();
|
||||||
@ -65,8 +62,8 @@ fn custom_msgtype_deserialization() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let expected_json_data = json_object! {
|
let expected_json_data = json_object! {
|
||||||
"custom_field".into() => json!("baba"),
|
"custom_field": "baba",
|
||||||
"another_one".into() => json!("abab"),
|
"another_one": "abab",
|
||||||
};
|
};
|
||||||
|
|
||||||
let custom_event: MessageType = from_json_value(json_data).unwrap();
|
let custom_event: MessageType = from_json_value(json_data).unwrap();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user