From 54cf81e9ab6b264b8ab7593d6194099c71beaa1a Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 15 May 2021 18:42:14 +0200 Subject: [PATCH] events: Make room::member types non-exhaustive --- crates/ruma-events/src/room/member.rs | 41 +++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/crates/ruma-events/src/room/member.rs b/crates/ruma-events/src/room/member.rs index cb67336b..d07a0daa 100644 --- a/crates/ruma-events/src/room/member.rs +++ b/crates/ruma-events/src/room/member.rs @@ -38,6 +38,7 @@ pub type MemberEvent = StateEvent; /// The payload for `MemberEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.room.member", kind = State)] pub struct MemberEventContent { /// The avatar URL for this user, if any. This is added by the homeserver. @@ -55,8 +56,8 @@ pub struct MemberEventContent { #[serde(skip_serializing_if = "Option::is_none")] pub displayname: Option, - /// Flag indicating if the room containing this event was created - /// with the intention of being a direct chat. + /// Flag indicating whether the room containing this event was created with the intention of + /// being a direct chat. #[serde(skip_serializing_if = "Option::is_none")] pub is_direct: Option, @@ -70,6 +71,19 @@ pub struct MemberEventContent { pub third_party_invite: Option, } +impl MemberEventContent { + /// Creates a new `MemberEventContent` with the given membership state. + pub fn new(membership: MembershipState) -> Self { + Self { + membership, + avatar_url: None, + displayname: None, + is_direct: None, + third_party_invite: None, + } + } +} + /// The membership state of a user. #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] #[ruma_enum(rename_all = "lowercase")] @@ -95,19 +109,29 @@ pub enum MembershipState { /// Information about a third party invitation. #[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct ThirdPartyInvite { /// A name which can be displayed to represent the user instead of their third party /// identifier. pub display_name: String, /// A block of content which has been signed, which servers can use to verify the event. + /// /// Clients should ignore this. pub signed: SignedContent, } +impl ThirdPartyInvite { + /// Creates a new `ThirdPartyInvite` with the given display name and signed content. + pub fn new(display_name: String, signed: SignedContent) -> Self { + Self { display_name, signed } + } +} + /// A block of content which has been signed, which servers can use to verify a third party /// invitation. #[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct SignedContent { /// The invited Matrix user ID. /// @@ -118,10 +142,21 @@ pub struct SignedContent { /// section of the server-server API. pub signatures: BTreeMap>, - /// The token property of the containing third_party_invite object. + /// The token property of the containing `third_party_invite` object. pub token: String, } +impl SignedContent { + /// Creates a new `SignedContent` with the given mxid, signature and token. + pub fn new( + mxid: UserId, + signatures: BTreeMap>, + token: String, + ) -> Self { + Self { mxid, signatures, token } + } +} + /// Translation of the membership change in `m.room.member` event. #[derive(Clone, Debug, Deserialize, Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]