Clean up previous commit, update tests
This commit is contained in:
parent
dd16d0f2f0
commit
c7d7212b00
@ -25,6 +25,9 @@ features = ["serde"]
|
|||||||
version = "1.0.101"
|
version = "1.0.101"
|
||||||
features = ["derive"]
|
features = ["derive"]
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
maplit = "1.0.2"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
"ruma-events-macros",
|
"ruma-events-macros",
|
||||||
|
@ -42,49 +42,49 @@ pub struct PowerLevelsEvent {
|
|||||||
#[derive(Clone, Debug, PartialEq, Serialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize)]
|
||||||
pub struct PowerLevelsEventContent {
|
pub struct PowerLevelsEventContent {
|
||||||
/// The level required to ban a user.
|
/// The level required to ban a user.
|
||||||
#[serde(default = "default_power_level")]
|
#[serde(skip_serializing_if = "is_default_power_level")]
|
||||||
pub ban: Int,
|
pub ban: Int,
|
||||||
|
|
||||||
/// The level required to send specific event types.
|
/// The level required to send specific event types.
|
||||||
///
|
///
|
||||||
/// This is a mapping from event type to power level required.
|
/// This is a mapping from event type to power level required.
|
||||||
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
|
#[serde(skip_serializing_if = "HashMap::is_empty")]
|
||||||
pub events: HashMap<EventType, Int>,
|
pub events: HashMap<EventType, Int>,
|
||||||
|
|
||||||
/// The default level required to send message events.
|
/// The default level required to send message events.
|
||||||
#[serde(default)]
|
#[serde(skip_serializing_if = "is_power_level_zero")]
|
||||||
pub events_default: Int,
|
pub events_default: Int,
|
||||||
|
|
||||||
/// The level required to invite a user.
|
/// The level required to invite a user.
|
||||||
#[serde(default = "default_power_level")]
|
#[serde(skip_serializing_if = "is_default_power_level")]
|
||||||
pub invite: Int,
|
pub invite: Int,
|
||||||
|
|
||||||
/// The level required to kick a user.
|
/// The level required to kick a user.
|
||||||
#[serde(default = "default_power_level")]
|
#[serde(skip_serializing_if = "is_default_power_level")]
|
||||||
pub kick: Int,
|
pub kick: Int,
|
||||||
|
|
||||||
/// The level required to redact an event.
|
/// The level required to redact an event.
|
||||||
#[serde(default = "default_power_level")]
|
#[serde(skip_serializing_if = "is_default_power_level")]
|
||||||
pub redact: Int,
|
pub redact: Int,
|
||||||
|
|
||||||
/// The default level required to send state events.
|
/// The default level required to send state events.
|
||||||
#[serde(default = "default_power_level")]
|
#[serde(skip_serializing_if = "is_default_power_level")]
|
||||||
pub state_default: Int,
|
pub state_default: Int,
|
||||||
|
|
||||||
/// The power levels for specific users.
|
/// The power levels for specific users.
|
||||||
///
|
///
|
||||||
/// This is a mapping from `user_id` to power level for that user.
|
/// This is a mapping from `user_id` to power level for that user.
|
||||||
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
|
#[serde(skip_serializing_if = "HashMap::is_empty")]
|
||||||
pub users: HashMap<UserId, Int>,
|
pub users: HashMap<UserId, Int>,
|
||||||
|
|
||||||
/// The default power level for every user in the room.
|
/// The default power level for every user in the room.
|
||||||
#[serde(default)]
|
#[serde(skip_serializing_if = "is_power_level_zero")]
|
||||||
pub users_default: Int,
|
pub users_default: Int,
|
||||||
|
|
||||||
/// The power level requirements for specific notification types.
|
/// The power level requirements for specific notification types.
|
||||||
///
|
///
|
||||||
/// This is a mapping from `key` to power level for that notifications key.
|
/// This is a mapping from `key` to power level for that notifications key.
|
||||||
#[serde(default, skip_serializing_if = "NotificationPowerLevels::is_default")]
|
#[serde(skip_serializing_if = "NotificationPowerLevels::is_default")]
|
||||||
pub notifications: NotificationPowerLevels,
|
pub notifications: NotificationPowerLevels,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,7 +217,7 @@ pub(crate) mod raw {
|
|||||||
/// The level required to send specific event types.
|
/// The level required to send specific event types.
|
||||||
///
|
///
|
||||||
/// This is a mapping from event type to power level required.
|
/// This is a mapping from event type to power level required.
|
||||||
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
|
#[serde(default)]
|
||||||
pub events: HashMap<EventType, Int>,
|
pub events: HashMap<EventType, Int>,
|
||||||
|
|
||||||
/// The default level required to send message events.
|
/// The default level required to send message events.
|
||||||
@ -243,7 +243,7 @@ pub(crate) mod raw {
|
|||||||
/// The power levels for specific users.
|
/// The power levels for specific users.
|
||||||
///
|
///
|
||||||
/// This is a mapping from `user_id` to power level for that user.
|
/// This is a mapping from `user_id` to power level for that user.
|
||||||
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
|
#[serde(default)]
|
||||||
pub users: HashMap<UserId, Int>,
|
pub users: HashMap<UserId, Int>,
|
||||||
|
|
||||||
/// The default power level for every user in the room.
|
/// The default power level for every user in the room.
|
||||||
@ -253,7 +253,7 @@ pub(crate) mod raw {
|
|||||||
/// The power level requirements for specific notification types.
|
/// The power level requirements for specific notification types.
|
||||||
///
|
///
|
||||||
/// This is a mapping from `key` to power level for that notifications key.
|
/// This is a mapping from `key` to power level for that notifications key.
|
||||||
#[serde(default, skip_serializing_if = "NotificationPowerLevels::is_default")]
|
#[serde(default)]
|
||||||
pub notifications: NotificationPowerLevels,
|
pub notifications: NotificationPowerLevels,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -288,35 +288,51 @@ fn default_power_level() -> Int {
|
|||||||
Int::from(50)
|
Int::from(50)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Used with #[serde(skip_serializing_if)] to omit default power levels.
|
||||||
|
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||||
|
fn is_default_power_level(l: &Int) -> bool {
|
||||||
|
*l == Int::from(50)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Used with #[serde(skip_serializing_if)] to omit default power levels.
|
||||||
|
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||||
|
fn is_power_level_zero(l: &Int) -> bool {
|
||||||
|
*l == Int::from(0)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::{collections::HashMap, convert::TryFrom};
|
use std::{collections::HashMap, convert::TryFrom};
|
||||||
|
|
||||||
use js_int::{Int, UInt};
|
use js_int::{Int, UInt};
|
||||||
|
use maplit::hashmap;
|
||||||
use ruma_identifiers::{EventId, RoomId, UserId};
|
use ruma_identifiers::{EventId, RoomId, UserId};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use super::{NotificationPowerLevels, PowerLevelsEvent, PowerLevelsEventContent};
|
use super::{
|
||||||
|
default_power_level, NotificationPowerLevels, PowerLevelsEvent, PowerLevelsEventContent,
|
||||||
|
};
|
||||||
|
use crate::EventType;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn serialization_with_optional_fields_as_none() {
|
fn serialization_with_optional_fields_as_none() {
|
||||||
let default = Int::try_from(50).unwrap();
|
let default = default_power_level();
|
||||||
|
|
||||||
let power_levels_event = PowerLevelsEvent {
|
let power_levels_event = PowerLevelsEvent {
|
||||||
content: PowerLevelsEventContent {
|
content: PowerLevelsEventContent {
|
||||||
ban: default,
|
ban: default,
|
||||||
events: HashMap::new(),
|
events: HashMap::new(),
|
||||||
events_default: default,
|
events_default: Int::from(0),
|
||||||
invite: default,
|
invite: default,
|
||||||
kick: default,
|
kick: default,
|
||||||
redact: default,
|
redact: default,
|
||||||
state_default: default,
|
state_default: default,
|
||||||
users: HashMap::new(),
|
users: HashMap::new(),
|
||||||
users_default: default,
|
users_default: Int::from(0),
|
||||||
notifications: NotificationPowerLevels { room: default },
|
notifications: NotificationPowerLevels::default(),
|
||||||
},
|
},
|
||||||
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
|
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
|
||||||
origin_server_ts: UInt::try_from(1).unwrap(),
|
origin_server_ts: UInt::from(1u32),
|
||||||
prev_content: None,
|
prev_content: None,
|
||||||
room_id: None,
|
room_id: None,
|
||||||
unsigned: None,
|
unsigned: None,
|
||||||
@ -325,51 +341,62 @@ mod tests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let actual = serde_json::to_string(&power_levels_event).unwrap();
|
let actual = serde_json::to_string(&power_levels_event).unwrap();
|
||||||
let expected = r#"{"content":{"ban":50,"events":{},"events_default":50,"invite":50,"kick":50,"redact":50,"state_default":50,"users":{},"users_default":50,"notifications":{"room":50}},"event_id":"$h29iv0s8:example.com","origin_server_ts":1,"sender":"@carl:example.com","state_key":"","type":"m.room.power_levels"}"#;
|
let expected = r#"{"content":{},"event_id":"$h29iv0s8:example.com","origin_server_ts":1,"sender":"@carl:example.com","state_key":"","type":"m.room.power_levels"}"#;
|
||||||
|
|
||||||
assert_eq!(actual, expected);
|
assert_eq!(actual, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn serialization_with_all_fields() {
|
fn serialization_with_all_fields() {
|
||||||
let default = Int::try_from(50).unwrap();
|
let user = UserId::try_from("@carl:example.com").unwrap();
|
||||||
|
|
||||||
let power_levels_event = PowerLevelsEvent {
|
let power_levels_event = PowerLevelsEvent {
|
||||||
content: PowerLevelsEventContent {
|
content: PowerLevelsEventContent {
|
||||||
ban: default,
|
ban: Int::from(23),
|
||||||
events: HashMap::new(),
|
events: hashmap! {
|
||||||
events_default: default,
|
EventType::Dummy => Int::from(23)
|
||||||
invite: default,
|
},
|
||||||
kick: default,
|
events_default: Int::from(23),
|
||||||
redact: default,
|
invite: Int::from(23),
|
||||||
state_default: default,
|
kick: Int::from(23),
|
||||||
users: HashMap::new(),
|
redact: Int::from(23),
|
||||||
users_default: default,
|
state_default: Int::from(23),
|
||||||
notifications: NotificationPowerLevels { room: default },
|
users: hashmap! {
|
||||||
|
user.clone() => Int::from(23)
|
||||||
|
},
|
||||||
|
users_default: Int::from(23),
|
||||||
|
notifications: NotificationPowerLevels {
|
||||||
|
room: Int::from(23),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
|
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
|
||||||
origin_server_ts: UInt::try_from(1).unwrap(),
|
origin_server_ts: UInt::from(1u32),
|
||||||
prev_content: Some(PowerLevelsEventContent {
|
prev_content: Some(PowerLevelsEventContent {
|
||||||
// Make just one field different so we at least know they're two different objects.
|
// Make just one field different so we at least know they're two different objects.
|
||||||
ban: Int::try_from(75).unwrap(),
|
ban: Int::from(42),
|
||||||
events: HashMap::new(),
|
events: hashmap! {
|
||||||
events_default: default,
|
EventType::Dummy => Int::from(42)
|
||||||
invite: default,
|
},
|
||||||
kick: default,
|
events_default: Int::from(42),
|
||||||
redact: default,
|
invite: Int::from(42),
|
||||||
state_default: default,
|
kick: Int::from(42),
|
||||||
users: HashMap::new(),
|
redact: Int::from(42),
|
||||||
users_default: default,
|
state_default: Int::from(42),
|
||||||
notifications: NotificationPowerLevels { room: default },
|
users: hashmap! {
|
||||||
|
user.clone() => Int::from(42)
|
||||||
|
},
|
||||||
|
users_default: Int::from(42),
|
||||||
|
notifications: NotificationPowerLevels {
|
||||||
|
room: Int::from(42),
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()),
|
room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()),
|
||||||
unsigned: Some(serde_json::from_str::<Value>(r#"{"foo":"bar"}"#).unwrap()),
|
unsigned: Some(serde_json::from_str::<Value>(r#"{"foo":"bar"}"#).unwrap()),
|
||||||
sender: UserId::try_from("@carl:example.com").unwrap(),
|
sender: user,
|
||||||
state_key: "".to_string(),
|
state_key: "".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let actual = serde_json::to_string(&power_levels_event).unwrap();
|
let actual = serde_json::to_string(&power_levels_event).unwrap();
|
||||||
let expected = r#"{"content":{"ban":50,"events":{},"events_default":50,"invite":50,"kick":50,"redact":50,"state_default":50,"users":{},"users_default":50,"notifications":{"room":50}},"event_id":"$h29iv0s8:example.com","origin_server_ts":1,"prev_content":{"ban":75,"events":{},"events_default":50,"invite":50,"kick":50,"redact":50,"state_default":50,"users":{},"users_default":50,"notifications":{"room":50}},"room_id":"!n8f893n9:example.com","sender":"@carl:example.com","state_key":"","type":"m.room.power_levels","unsigned":{"foo":"bar"}}"#;
|
let expected = r#"{"content":{"ban":23,"events":{"m.dummy":23},"events_default":23,"invite":23,"kick":23,"redact":23,"state_default":23,"users":{"@carl:example.com":23},"users_default":23,"notifications":{"room":23}},"event_id":"$h29iv0s8:example.com","origin_server_ts":1,"prev_content":{"ban":42,"events":{"m.dummy":42},"events_default":42,"invite":42,"kick":42,"redact":42,"state_default":42,"users":{"@carl:example.com":42},"users_default":42,"notifications":{"room":42}},"room_id":"!n8f893n9:example.com","sender":"@carl:example.com","state_key":"","type":"m.room.power_levels","unsigned":{"foo":"bar"}}"#;
|
||||||
|
|
||||||
assert_eq!(actual, expected);
|
assert_eq!(actual, expected);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user