diff --git a/ruma-events-macros/src/gen.rs b/ruma-events-macros/src/gen.rs index 2673e6f6..6aed837c 100644 --- a/ruma-events-macros/src/gen.rs +++ b/ruma-events-macros/src/gen.rs @@ -327,7 +327,7 @@ impl ToTokens for RumaEvent { } quote! { - impl crate::EventResultCompatible for #content_name { + impl crate::TryFromRaw for #content_name { type Raw = raw::#content_name; type Err = crate::Void; @@ -353,7 +353,7 @@ impl ToTokens for RumaEvent { #content - impl crate::EventResultCompatible for #name { + impl crate::TryFromRaw for #name { type Raw = raw::#name; type Err = crate::Void; diff --git a/ruma-events-macros/tests/ruma_events_macros.rs b/ruma-events-macros/tests/ruma_events_macros.rs index ac5117c7..3a9e0fe4 100644 --- a/ruma-events-macros/tests/ruma_events_macros.rs +++ b/ruma-events-macros/tests/ruma_events_macros.rs @@ -85,7 +85,7 @@ impl<'de> Deserialize<'de> for EventType { /// The result of deserializing an event, which may or may not be valid. #[derive(Debug)] -pub enum EventResult { +pub enum EventResult { /// `T` deserialized and validated successfully. Ok(T), @@ -95,7 +95,7 @@ pub enum EventResult { Err(InvalidEvent), } -impl EventResult { +impl EventResult { /// Convert `EventResult` into the equivalent `std::result::Result>`. pub fn into_result(self) -> Result> { match self { @@ -105,7 +105,7 @@ impl EventResult { } } -pub trait EventResultCompatible { +pub trait TryFromRaw { /// The raw form of this event that deserialization falls back to if deserializing `Self` fails. type Raw; type Err: Into; @@ -115,7 +115,7 @@ pub trait EventResultCompatible { fn from_raw(raw: T::Raw) -> T where - T: EventResultCompatible, + T: TryFromRaw, { match T::try_from_raw(raw) { Ok(c) => c, @@ -132,7 +132,7 @@ impl From for String { } /// A basic event. -pub trait Event: Debug + Serialize + EventResultCompatible { +pub trait Event: Debug + Serialize + TryFromRaw { /// The type of this event's `content` field. type Content: Debug + Serialize; diff --git a/src/collections/all.rs b/src/collections/all.rs index ee933486..f3be59d1 100644 --- a/src/collections/all.rs +++ b/src/collections/all.rs @@ -46,7 +46,7 @@ use crate::{ sticker::StickerEvent, tag::TagEvent, typing::TypingEvent, - CustomEvent, CustomRoomEvent, CustomStateEvent, EventResultCompatible, Void, + CustomEvent, CustomRoomEvent, CustomStateEvent, TryFromRaw, Void, }; /// A basic event, room event, or state event. @@ -334,7 +334,7 @@ pub enum StateEvent { CustomState(CustomStateEvent), } -impl EventResultCompatible for Event { +impl TryFromRaw for Event { type Raw = raw::Event; type Err = Void; @@ -343,7 +343,7 @@ impl EventResultCompatible for Event { } } -impl EventResultCompatible for RoomEvent { +impl TryFromRaw for RoomEvent { type Raw = raw::RoomEvent; type Err = Void; @@ -352,7 +352,7 @@ impl EventResultCompatible for RoomEvent { } } -impl EventResultCompatible for StateEvent { +impl TryFromRaw for StateEvent { type Raw = raw::StateEvent; type Err = Void; diff --git a/src/collections/only.rs b/src/collections/only.rs index 043ed96a..b535b6a3 100644 --- a/src/collections/only.rs +++ b/src/collections/only.rs @@ -30,7 +30,7 @@ use crate::{ sticker::StickerEvent, tag::TagEvent, typing::TypingEvent, - CustomEvent, CustomRoomEvent, EventResultCompatible, Void, + CustomEvent, CustomRoomEvent, TryFromRaw, Void, }; /// A basic event. @@ -130,7 +130,7 @@ pub enum RoomEvent { CustomRoom(CustomRoomEvent), } -impl EventResultCompatible for Event { +impl TryFromRaw for Event { type Raw = raw::Event; type Err = Void; @@ -139,7 +139,7 @@ impl EventResultCompatible for Event { } } -impl EventResultCompatible for RoomEvent { +impl TryFromRaw for RoomEvent { type Raw = raw::RoomEvent; type Err = Void; diff --git a/src/ignored_user_list.rs b/src/ignored_user_list.rs index 8c1cf153..af7482dc 100644 --- a/src/ignored_user_list.rs +++ b/src/ignored_user_list.rs @@ -3,7 +3,7 @@ use ruma_identifiers::UserId; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; -use crate::{vec_as_map_of_empty, Event as _, EventResultCompatible, EventType, Void}; +use crate::{vec_as_map_of_empty, Event as _, EventType, TryFromRaw, Void}; /// A list of users to ignore. #[derive(Clone, Debug, PartialEq)] @@ -12,7 +12,7 @@ pub struct IgnoredUserListEvent { pub content: IgnoredUserListEventContent, } -impl EventResultCompatible for IgnoredUserListEvent { +impl TryFromRaw for IgnoredUserListEvent { type Raw = raw::IgnoredUserListEvent; type Err = Void; @@ -44,7 +44,7 @@ pub struct IgnoredUserListEventContent { pub ignored_users: Vec, } -impl EventResultCompatible for IgnoredUserListEventContent { +impl TryFromRaw for IgnoredUserListEventContent { type Raw = raw::IgnoredUserListEventContent; type Err = Void; diff --git a/src/key/verification/start.rs b/src/key/verification/start.rs index 71f2f6b4..16f67bd7 100644 --- a/src/key/verification/start.rs +++ b/src/key/verification/start.rs @@ -8,7 +8,7 @@ use super::{ HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString, VerificationMethod, }; -use crate::{Event, EventResultCompatible, EventType, InvalidInput}; +use crate::{Event, EventType, InvalidInput, TryFromRaw}; /// Begins an SAS key verification process. /// @@ -31,7 +31,7 @@ pub enum StartEventContent { __Nonexhaustive, } -impl EventResultCompatible for StartEvent { +impl TryFromRaw for StartEvent { type Raw = raw::StartEvent; type Err = &'static str; @@ -63,7 +63,7 @@ impl_event!( EventType::KeyVerificationStart ); -impl EventResultCompatible for StartEventContent { +impl TryFromRaw for StartEventContent { type Raw = raw::StartEventContent; type Err = &'static str; diff --git a/src/lib.rs b/src/lib.rs index bca2816c..fdefbbed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -246,7 +246,7 @@ impl Display for InvalidInput { impl Error for InvalidInput {} /// Marks types that can be deserialized as EventResult -pub trait EventResultCompatible: Sized { +pub trait TryFromRaw: Sized { /// The raw form of this event that deserialization falls back to if deserializing `Self` fails. type Raw: DeserializeOwned; type Err: Into; @@ -267,7 +267,7 @@ impl From for String { fn from_raw(raw: T::Raw) -> T where - T: EventResultCompatible, + T: TryFromRaw, { match T::try_from_raw(raw) { Ok(c) => c, @@ -282,7 +282,7 @@ where /// this structure will contain an `InvalidEvent`. See the documentation for `InvalidEvent` for /// more details. #[derive(Clone, Debug)] -pub enum EventResult { +pub enum EventResult { /// `T` deserialized and validated successfully. Ok(T), @@ -292,7 +292,7 @@ pub enum EventResult { Err(InvalidEvent), } -impl EventResult { +impl EventResult { /// Convert `EventResult` into the equivalent `std::result::Result`. pub fn into_result(self) -> Result> { match self { @@ -304,7 +304,7 @@ impl EventResult { impl<'de, T> Deserialize<'de> for EventResult where - T: EventResultCompatible, + T: TryFromRaw, { fn deserialize(deserializer: D) -> Result where @@ -338,7 +338,7 @@ where // For now, we don't support serialization of EventResult. // This is going to be added in a future version. -impl Serialize for EventResult { +impl Serialize for EventResult { fn serialize(&self, _serializer: S) -> Result where S: Serializer, @@ -546,7 +546,7 @@ pub enum EventType { } /// A basic event. -pub trait Event: Debug + Serialize + Sized + EventResultCompatible { +pub trait Event: Debug + Serialize + Sized + TryFromRaw { /// The type of this event's `content` field. type Content: Debug + Serialize; diff --git a/src/room/canonical_alias.rs b/src/room/canonical_alias.rs index da98e350..8e6ea1e4 100644 --- a/src/room/canonical_alias.rs +++ b/src/room/canonical_alias.rs @@ -5,7 +5,7 @@ use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId}; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; use serde_json::Value; -use crate::{empty_string_as_none, Event, EventResultCompatible, EventType, Void}; +use crate::{empty_string_as_none, Event, EventType, TryFromRaw, Void}; /// Informs the room as to which alias is the canonical one. #[derive(Clone, Debug, PartialEq)] @@ -45,7 +45,7 @@ pub struct CanonicalAliasEventContent { pub alias: Option, } -impl EventResultCompatible for CanonicalAliasEvent { +impl TryFromRaw for CanonicalAliasEvent { type Raw = raw::CanonicalAliasEvent; type Err = Void; @@ -63,7 +63,7 @@ impl EventResultCompatible for CanonicalAliasEvent { } } -impl EventResultCompatible for CanonicalAliasEventContent { +impl TryFromRaw for CanonicalAliasEventContent { type Raw = raw::CanonicalAliasEventContent; type Err = Void; diff --git a/src/room/encrypted.rs b/src/room/encrypted.rs index 192b821c..bd4d07db 100644 --- a/src/room/encrypted.rs +++ b/src/room/encrypted.rs @@ -5,7 +5,7 @@ use ruma_identifiers::{DeviceId, EventId, RoomId, UserId}; use serde::{de::Error, ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer}; use serde_json::{from_value, Value}; -use crate::{Algorithm, Event, EventResultCompatible, EventType, Void}; +use crate::{Algorithm, Event, EventType, TryFromRaw, Void}; /// This event type is used when sending encrypted events. /// @@ -48,7 +48,7 @@ pub enum EncryptedEventContent { __Nonexhaustive, } -impl EventResultCompatible for EncryptedEvent { +impl TryFromRaw for EncryptedEvent { type Raw = raw::EncryptedEvent; type Err = Void; @@ -64,7 +64,7 @@ impl EventResultCompatible for EncryptedEvent { } } -impl EventResultCompatible for EncryptedEventContent { +impl TryFromRaw for EncryptedEventContent { type Raw = raw::EncryptedEventContent; type Err = Void; diff --git a/src/room/message.rs b/src/room/message.rs index 947fae7c..b057e8ad 100644 --- a/src/room/message.rs +++ b/src/room/message.rs @@ -10,7 +10,7 @@ use serde::{ use serde_json::{from_value, Value}; use super::{EncryptedFile, ImageInfo, ThumbnailInfo}; -use crate::{Event, EventResultCompatible, EventType, Void}; +use crate::{Event, EventType, TryFromRaw, Void}; pub mod feedback; @@ -74,7 +74,7 @@ pub enum MessageEventContent { __Nonexhaustive, } -impl EventResultCompatible for MessageEvent { +impl TryFromRaw for MessageEvent { type Raw = raw::MessageEvent; type Err = Void; @@ -90,7 +90,7 @@ impl EventResultCompatible for MessageEvent { } } -impl EventResultCompatible for MessageEventContent { +impl TryFromRaw for MessageEventContent { type Raw = raw::MessageEventContent; type Err = Void; diff --git a/src/room/name.rs b/src/room/name.rs index 7bb8c2e8..f7d49a61 100644 --- a/src/room/name.rs +++ b/src/room/name.rs @@ -5,9 +5,7 @@ use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; use serde_json::Value; -use crate::{ - empty_string_as_none, Event as _, EventResultCompatible, EventType, InvalidInput, Void, -}; +use crate::{empty_string_as_none, Event as _, EventType, InvalidInput, TryFromRaw, Void}; /// A human-friendly room name designed to be displayed to the end-user. #[derive(Clone, Debug, PartialEq)] @@ -45,7 +43,7 @@ pub struct NameEventContent { pub(crate) name: Option, } -impl EventResultCompatible for NameEvent { +impl TryFromRaw for NameEvent { type Raw = raw::NameEvent; type Err = Void; @@ -63,7 +61,7 @@ impl EventResultCompatible for NameEvent { } } -impl EventResultCompatible for NameEventContent { +impl TryFromRaw for NameEventContent { type Raw = raw::NameEventContent; type Err = Void; diff --git a/src/room/power_levels.rs b/src/room/power_levels.rs index e6c34ed9..69fbfd7b 100644 --- a/src/room/power_levels.rs +++ b/src/room/power_levels.rs @@ -7,7 +7,7 @@ use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; use serde_json::Value; -use crate::{Event as _, EventResultCompatible, EventType, Void}; +use crate::{Event as _, EventType, TryFromRaw, Void}; /// Defines the power levels (privileges) of users in the room. #[derive(Clone, Debug, PartialEq)] @@ -85,7 +85,7 @@ pub struct PowerLevelsEventContent { pub notifications: NotificationPowerLevels, } -impl EventResultCompatible for PowerLevelsEvent { +impl TryFromRaw for PowerLevelsEvent { type Raw = raw::PowerLevelsEvent; type Err = Void; @@ -103,7 +103,7 @@ impl EventResultCompatible for PowerLevelsEvent { } } -impl EventResultCompatible for PowerLevelsEventContent { +impl TryFromRaw for PowerLevelsEventContent { type Raw = raw::PowerLevelsEventContent; type Err = Void; diff --git a/src/room/server_acl.rs b/src/room/server_acl.rs index 3ad333a3..a430165b 100644 --- a/src/room/server_acl.rs +++ b/src/room/server_acl.rs @@ -5,7 +5,7 @@ use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; use serde_json::Value; -use crate::{default_true, Event as _, EventResultCompatible, EventType, Void}; +use crate::{default_true, Event as _, EventType, TryFromRaw, Void}; /// An event to indicate which servers are permitted to participate in the room. #[derive(Clone, Debug, PartialEq)] @@ -65,7 +65,7 @@ pub struct ServerAclEventContent { pub deny: Vec, } -impl EventResultCompatible for ServerAclEvent { +impl TryFromRaw for ServerAclEvent { type Raw = raw::ServerAclEvent; type Err = Void; @@ -83,7 +83,7 @@ impl EventResultCompatible for ServerAclEvent { } } -impl EventResultCompatible for ServerAclEventContent { +impl TryFromRaw for ServerAclEventContent { type Raw = raw::ServerAclEventContent; type Err = Void; diff --git a/src/stripped.rs b/src/stripped.rs index 6d5b3a17..81949112 100644 --- a/src/stripped.rs +++ b/src/stripped.rs @@ -18,7 +18,7 @@ use crate::{ power_levels::PowerLevelsEventContent, third_party_invite::ThirdPartyInviteEventContent, topic::TopicEventContent, }, - EventResultCompatible, EventType, + EventType, TryFromRaw, }; /// A stripped-down version of a state event that is included along with some other events. @@ -116,14 +116,14 @@ pub type StrippedRoomThirdPartyInvite = StrippedStateContent; -impl EventResultCompatible for StrippedState { +impl TryFromRaw for StrippedState { type Raw = raw::StrippedState; type Err = String; fn try_from_raw(raw: raw::StrippedState) -> Result { use raw::StrippedState::*; - fn convert( + fn convert( raw_variant: fn(T::Raw) -> raw::StrippedState, variant: fn(T) -> StrippedState, raw: T::Raw, @@ -152,9 +152,9 @@ impl EventResultCompatible for StrippedState { } } -impl EventResultCompatible for StrippedStateContent +impl TryFromRaw for StrippedStateContent where - C: EventResultCompatible, + C: TryFromRaw, { type Raw = StrippedStateContent; type Err = C::Err;