events: Make remaining types in room::* non-exhaustive

This commit is contained in:
Jonas Platte 2021-05-16 22:45:48 +02:00
parent 504c7ad06e
commit 55df2aa26a
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
7 changed files with 100 additions and 3 deletions

View File

@ -17,6 +17,7 @@ pub type PowerLevelsEvent = StateEvent<PowerLevelsEventContent>;
/// The payload for `PowerLevelsEvent`.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.room.power_levels", kind = State)]
pub struct PowerLevelsEventContent {
/// The level required to ban a user.
@ -116,8 +117,9 @@ pub struct PowerLevelsEventContent {
pub notifications: NotificationPowerLevels,
}
impl Default for PowerLevelsEventContent {
fn default() -> Self {
impl PowerLevelsEventContent {
/// Creates a `PowerLevelsEventContent` with all-default values.
pub fn new() -> Self {
// events_default and users_default having a default of 0 while the others have a default
// of 50 is not an oversight, these defaults are from the Matrix specification.
Self {
@ -135,6 +137,12 @@ impl Default for PowerLevelsEventContent {
}
}
impl Default for PowerLevelsEventContent {
fn default() -> Self {
Self::new()
}
}
/// 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 {

View File

@ -55,7 +55,8 @@ pub struct SyncRedactionEvent {
}
/// A redaction of an event.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.room.redaction", kind = Message)]
pub struct RedactionEventContent {
/// The reason for the redaction, if any.
@ -63,4 +64,16 @@ pub struct RedactionEventContent {
pub reason: Option<String>,
}
impl RedactionEventContent {
/// Creates an empty `RedactionEventContent`.
pub fn new() -> Self {
Self::default()
}
/// Creates a `RedactionEventContent` with the given reason.
pub fn with_reason(reason: String) -> Self {
Self { reason: Some(reason) }
}
}
impl RedactedStateEventContent for RedactedRedactionEventContent {}

View File

@ -51,22 +51,40 @@ pub(crate) enum RelationJsonRepr {
/// Information about the event a "rich reply" is replying to.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct InReplyTo {
/// The event being replied to.
pub event_id: EventId,
}
impl InReplyTo {
/// Creates a new `InReplyTo` with the given event ID.
pub fn new(event_id: EventId) -> Self {
Self { event_id }
}
}
/// A reference to another event.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg(feature = "unstable-pre-spec")]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Reference {
/// The event we are referencing.
pub event_id: EventId,
}
#[cfg(feature = "unstable-pre-spec")]
impl Reference {
/// Creates a new `Reference` with the given event ID.
pub fn new(event_id: EventId) -> Self {
Self { event_id }
}
}
/// An annotation for an event.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Annotation {
/// The event that is being annotated.
pub event_id: EventId,
@ -75,15 +93,31 @@ pub struct Annotation {
pub key: String,
}
impl Annotation {
/// Creates a new `Annotation` with the given event ID and key.
pub fn new(event_id: EventId, key: String) -> Self {
Self { event_id, key }
}
}
/// An event replacing another event.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg(feature = "unstable-pre-spec")]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Replacement {
/// The event this event is replacing.
pub event_id: EventId,
}
#[cfg(feature = "unstable-pre-spec")]
impl Replacement {
/// Creates a new `Replacement` with the given event ID.
pub fn new(event_id: EventId) -> Self {
Self { event_id }
}
}
#[cfg(test)]
mod tests {
use matches::assert_matches;

View File

@ -10,6 +10,7 @@ pub type ServerAclEvent = StateEvent<ServerAclEventContent>;
/// The payload for `ServerAclEvent`.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.room.server_acl", kind = State)]
pub struct ServerAclEventContent {
/// True to allow server names that are IP address literals. False to deny.
@ -38,6 +39,14 @@ pub struct ServerAclEventContent {
pub deny: Vec<String>,
}
impl ServerAclEventContent {
/// Creates a new `ServerAclEventContent` with the given IP literal allowance flag, allowed and
/// denied servers.
pub fn new(allow_ip_literals: bool, allow: Vec<String>, deny: Vec<String>) -> Self {
Self { allow_ip_literals, allow, deny }
}
}
#[cfg(test)]
mod tests {
use ruma_serde::Raw;

View File

@ -14,6 +14,7 @@ pub type ThirdPartyInviteEvent = StateEvent<ThirdPartyInviteEventContent>;
/// The payload for `ThirdPartyInviteEvent`.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.room.third_party_invite", kind = State)]
pub struct ThirdPartyInviteEventContent {
/// A user-readable string which represents the user who has been invited.
@ -42,8 +43,17 @@ pub struct ThirdPartyInviteEventContent {
pub public_keys: Option<Vec<PublicKey>>,
}
impl ThirdPartyInviteEventContent {
/// Creates a new `ThirdPartyInviteEventContent` with the given display name, key validity url
/// and public key.
pub fn new(display_name: String, key_validity_url: String, public_key: String) -> Self {
Self { display_name, key_validity_url, public_key, public_keys }
}
}
/// A public key for signing a third party invite token.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct PublicKey {
/// An optional URL which can be fetched to validate whether the key has been revoked.
///
@ -55,3 +65,10 @@ pub struct PublicKey {
/// A Base64-encoded Ed25519 key with which the token must be signed.
pub public_key: String,
}
impl PublicKey {
/// Creates a new `PublicKey` with the given base64-encoded ed25519 key.
pub fn new(public_key: String) -> Self {
Self { key_validity_url: None, public_key }
}
}

View File

@ -13,6 +13,7 @@ pub type TombstoneEvent = StateEvent<TombstoneEventContent>;
/// The payload for `TombstoneEvent`.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[ruma_event(type = "m.room.tombstone", kind = State)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct TombstoneEventContent {
/// A server-defined message.
///
@ -24,3 +25,10 @@ pub struct TombstoneEventContent {
/// The new room the client should be visiting.
pub replacement_room: RoomId,
}
impl TombstoneEventContent {
/// Creates a new `TombstoneEventContent` with the given body and replacement room ID.
pub fn new(body: String, replacement_room: RoomId) -> Self {
Self { body, replacement_room }
}
}

View File

@ -10,8 +10,16 @@ pub type TopicEvent = StateEvent<TopicEventContent>;
/// The payload for `TopicEvent`.
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.room.topic", kind = State)]
pub struct TopicEventContent {
/// The topic text.
pub topic: String,
}
impl TopicEventContent {
/// Creates a new `TopicEventContent` with the given topic.
pub fn new(topic: String) -> Self {
Self { topic }
}
}