From 4eb5033dd0d7c5c6e751fd858bc6763519dbd021 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 9 Mar 2023 18:57:28 +0100 Subject: [PATCH] events: Add utility functions for InitialStateEvent --- crates/ruma-common/CHANGELOG.md | 1 + crates/ruma-common/src/events/kinds.rs | 49 ++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index 8d620cb8..1f0f3b4c 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -11,6 +11,7 @@ Improvements: - To update the server-default push rules - To remove a user-defined push rule - Add `AsRef<[u8]>` implementations for identifier types +- Add `InitialStateEvent::{new, to_raw, to_raw_any}` # 0.11.3 diff --git a/crates/ruma-common/src/events/kinds.rs b/crates/ruma-common/src/events/kinds.rs index 6d92fb6e..9e7dd198 100644 --- a/crates/ruma-common/src/events/kinds.rs +++ b/crates/ruma-common/src/events/kinds.rs @@ -5,15 +5,16 @@ use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize}; use serde_json::value::RawValue as RawJsonValue; use super::{ - EphemeralRoomEventContent, EventContent, EventContentFromType, GlobalAccountDataEventContent, - MessageLikeEventContent, MessageLikeEventType, MessageLikeUnsigned, - PossiblyRedactedStateEventContent, RedactContent, RedactedMessageLikeEventContent, - RedactedStateEventContent, RedactedUnsigned, RedactionDeHelper, RoomAccountDataEventContent, - StateEventType, StaticStateEventContent, ToDeviceEventContent, + EmptyStateKey, EphemeralRoomEventContent, EventContent, EventContentFromType, + GlobalAccountDataEventContent, MessageLikeEventContent, MessageLikeEventType, + MessageLikeUnsigned, PossiblyRedactedStateEventContent, RedactContent, + RedactedMessageLikeEventContent, RedactedStateEventContent, RedactedUnsigned, + RedactionDeHelper, RoomAccountDataEventContent, StateEventType, StaticStateEventContent, + ToDeviceEventContent, }; use crate::{ - serde::from_raw_json_value, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, - OwnedUserId, RoomId, UserId, + serde::{from_raw_json_value, Raw}, + EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UserId, }; /// A global account data event. @@ -314,6 +315,40 @@ pub struct InitialStateEvent { pub state_key: C::StateKey, } +impl InitialStateEvent { + /// Create a new `InitialStateEvent` for an event type with an empty state key. + /// + /// For cases where the state key is not empty, use a struct literal to create the event. + pub fn new(content: C) -> Self + where + C: StaticStateEventContent, + { + Self { content, state_key: EmptyStateKey } + } + + /// Shorthand for `Raw::new(self).unwrap()`. + /// + /// Since none of the content types in Ruma ever return an error in serialization, this will + /// never panic with `C` being a type from Ruma. However, if you use a custom content type + /// with a `Serialize` implementation that can error (for example because it contains an + /// `enum` with one or more variants that use the `#[serde(skip)]` attribute), this method + /// can panic. + pub fn to_raw(&self) -> Raw { + Raw::new(self).unwrap() + } + + /// Shorthand for `self.to_raw().cast::()`. + /// + /// Since none of the content types in Ruma ever return an error in serialization, this will + /// never panic with `C` being a type from Ruma. However, if you use a custom content type + /// with a `Serialize` implementation that can error (for example because it contains an + /// `enum` with one or more variants that use the `#[serde(skip)]` attribute), this method + /// can panic. + pub fn to_raw_any(&self) -> Raw { + self.to_raw().cast() + } +} + impl Serialize for InitialStateEvent { fn serialize(&self, serializer: S) -> Result where