From c20d79db7d44f4422dacc0962ddcfb6d5e65d550 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 8 Oct 2019 21:28:18 +0200 Subject: [PATCH] Inline try_from method into EventResultCompatible --- ruma-events-macros/src/gen.rs | 77 ++++++++----------- .../tests/ruma_events_macros.rs | 13 ++++ src/collections/all.rs | 23 ++---- src/collections/only.rs | 16 +--- src/ignored_user_list.rs | 51 ++++++------ src/key/verification/start.rs | 39 +++++----- src/lib.rs | 24 +++--- src/macros.rs | 22 ++---- src/room/canonical_alias.rs | 25 +++--- src/room/encrypted.rs | 23 +++--- src/room/message.rs | 27 +++---- src/room/name.rs | 24 +++--- src/room/power_levels.rs | 25 +++--- src/room/server_acl.rs | 25 +++--- src/stripped.rs | 45 ++++++----- 15 files changed, 212 insertions(+), 247 deletions(-) diff --git a/ruma-events-macros/src/gen.rs b/ruma-events-macros/src/gen.rs index e21f31f7..2673e6f6 100644 --- a/ruma-events-macros/src/gen.rs +++ b/ruma-events-macros/src/gen.rs @@ -166,10 +166,7 @@ impl ToTokens for RumaEvent { match &self.content { Content::Struct(_) => { quote_spanned! {span=> - content: match std::convert::TryFrom::try_from(raw.content) { - Ok(c) => c, - Err((_, void)) => match void {}, - }, + content: crate::from_raw(raw.content), } } Content::Typedef(_) => { @@ -182,12 +179,7 @@ impl ToTokens for RumaEvent { match &self.content { Content::Struct(_) => { quote_spanned! {span=> - prev_content: raw.prev_content.map(|prev_content| { - match std::convert::TryFrom::try_from(prev_content) { - Ok(c) => c, - Err((_, void)) => match void {}, - } - }), + prev_content: raw.prev_content.map(crate::from_raw), } } Content::Typedef(_) => { @@ -318,39 +310,39 @@ impl ToTokens for RumaEvent { TokenStream::new() }; - let impl_conversions_for_content = if let Content::Struct(content_fields) = &self.content { - let mut content_field_values: Vec = - Vec::with_capacity(content_fields.len()); + let impl_event_result_compatible_for_content = + if let Content::Struct(content_fields) = &self.content { + let mut content_field_values: Vec = + Vec::with_capacity(content_fields.len()); - for content_field in content_fields { - let content_field_ident = content_field.ident.clone().unwrap(); - let span = content_field.span(); + for content_field in content_fields { + let content_field_ident = content_field.ident.clone().unwrap(); + let span = content_field.span(); - let token_stream = quote_spanned! {span=> - #content_field_ident: raw.#content_field_ident, - }; + let token_stream = quote_spanned! {span=> + #content_field_ident: raw.#content_field_ident, + }; - content_field_values.push(token_stream); - } + content_field_values.push(token_stream); + } - quote! { - impl std::convert::TryFrom for #content_name { - type Error = (raw::#content_name, crate::Void); + quote! { + impl crate::EventResultCompatible for #content_name { + type Raw = raw::#content_name; + type Err = crate::Void; - fn try_from(raw: raw::#content_name) -> Result { - Ok(Self { - #(#content_field_values)* - }) + fn try_from_raw( + raw: raw::#content_name + ) -> Result { + Ok(Self { + #(#content_field_values)* + }) + } } } - - impl crate::EventResultCompatible for #content_name { - type Raw = raw::#content_name; - } - } - } else { - TokenStream::new() - }; + } else { + TokenStream::new() + }; let output = quote!( #(#attrs)* @@ -361,21 +353,18 @@ impl ToTokens for RumaEvent { #content - impl std::convert::TryFrom for #name { - type Error = (raw::#name, crate::Void); + impl crate::EventResultCompatible for #name { + type Raw = raw::#name; + type Err = crate::Void; - fn try_from(raw: raw::#name) -> Result { + fn try_from_raw(raw: raw::#name) -> Result { Ok(Self { #(#try_from_field_values)* }) } } - impl crate::EventResultCompatible for #name { - type Raw = raw::#name; - } - - #impl_conversions_for_content + #impl_event_result_compatible_for_content use serde::ser::SerializeStruct as _; diff --git a/ruma-events-macros/tests/ruma_events_macros.rs b/ruma-events-macros/tests/ruma_events_macros.rs index a5a12a40..ac5117c7 100644 --- a/ruma-events-macros/tests/ruma_events_macros.rs +++ b/ruma-events-macros/tests/ruma_events_macros.rs @@ -108,6 +108,19 @@ impl EventResult { pub trait EventResultCompatible { /// The raw form of this event that deserialization falls back to if deserializing `Self` fails. type Raw; + type Err: Into; + + fn try_from_raw(_: Self::Raw) -> Result; +} + +fn from_raw(raw: T::Raw) -> T +where + T: EventResultCompatible, +{ + match T::try_from_raw(raw) { + Ok(c) => c, + Err((void, _)) => match void {}, + } } enum Void {} diff --git a/src/collections/all.rs b/src/collections/all.rs index 2276ed42..ee933486 100644 --- a/src/collections/all.rs +++ b/src/collections/all.rs @@ -1,8 +1,6 @@ //! Enums for heterogeneous collections of events, inclusive for every event type that implements //! the trait of the same name. -use std::convert::TryFrom; - use serde::{Serialize, Serializer}; use super::raw::all as raw; @@ -338,36 +336,27 @@ pub enum StateEvent { impl EventResultCompatible for Event { type Raw = raw::Event; -} + type Err = Void; -impl TryFrom for Event { - type Error = (raw::Event, Void); - - fn try_from(raw: raw::Event) -> Result { + fn try_from_raw(raw: raw::Event) -> Result { unimplemented!() } } impl EventResultCompatible for RoomEvent { type Raw = raw::RoomEvent; -} + type Err = Void; -impl TryFrom for RoomEvent { - type Error = (raw::RoomEvent, Void); - - fn try_from(raw: raw::RoomEvent) -> Result { + fn try_from_raw(raw: raw::RoomEvent) -> Result { unimplemented!() } } impl EventResultCompatible for StateEvent { type Raw = raw::StateEvent; -} + type Err = Void; -impl TryFrom for StateEvent { - type Error = (raw::StateEvent, Void); - - fn try_from(raw: raw::StateEvent) -> Result { + fn try_from_raw(raw: raw::StateEvent) -> Result { unimplemented!() } } diff --git a/src/collections/only.rs b/src/collections/only.rs index 8b0faae5..043ed96a 100644 --- a/src/collections/only.rs +++ b/src/collections/only.rs @@ -1,8 +1,6 @@ //! Enums for heterogeneous collections of events, exclusive to event types that implement "at //! most" the trait of the same name. -use std::convert::TryFrom; - use serde::{Serialize, Serializer}; pub use super::{all::StateEvent, raw::only as raw}; @@ -134,24 +132,18 @@ pub enum RoomEvent { impl EventResultCompatible for Event { type Raw = raw::Event; -} + type Err = Void; -impl TryFrom for Event { - type Error = (raw::Event, Void); - - fn try_from(raw: raw::Event) -> Result { + fn try_from_raw(raw: raw::Event) -> Result { unimplemented!() } } impl EventResultCompatible for RoomEvent { type Raw = raw::RoomEvent; -} + type Err = Void; -impl TryFrom for RoomEvent { - type Error = (raw::RoomEvent, Void); - - fn try_from(raw: raw::RoomEvent) -> Result { + fn try_from_raw(raw: raw::RoomEvent) -> Result { unimplemented!() } } diff --git a/src/ignored_user_list.rs b/src/ignored_user_list.rs index e046088f..8c1cf153 100644 --- a/src/ignored_user_list.rs +++ b/src/ignored_user_list.rs @@ -1,11 +1,9 @@ //! Types for the *m.ignored_user_list* event. -use std::convert::TryFrom; - use ruma_identifiers::UserId; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; -use crate::{vec_as_map_of_empty, Event as _, EventType, Void}; +use crate::{vec_as_map_of_empty, Event as _, EventResultCompatible, EventType, Void}; /// A list of users to ignore. #[derive(Clone, Debug, PartialEq)] @@ -14,29 +12,13 @@ pub struct IgnoredUserListEvent { pub content: IgnoredUserListEventContent, } -/// The payload for `IgnoredUserListEvent`. -#[derive(Clone, Debug, PartialEq, Serialize)] -pub struct IgnoredUserListEventContent { - /// A list of users to ignore. - pub ignored_users: Vec, -} +impl EventResultCompatible for IgnoredUserListEvent { + type Raw = raw::IgnoredUserListEvent; + type Err = Void; -impl TryFrom for IgnoredUserListEvent { - type Error = (raw::IgnoredUserListEvent, Void); - - fn try_from(raw: raw::IgnoredUserListEvent) -> Result { + fn try_from_raw(raw: raw::IgnoredUserListEvent) -> Result { Ok(Self { - content: crate::convert_content(raw.content), - }) - } -} - -impl TryFrom for IgnoredUserListEventContent { - type Error = (raw::IgnoredUserListEventContent, Void); - - fn try_from(raw: raw::IgnoredUserListEventContent) -> Result { - Ok(Self { - ignored_users: raw.ignored_users, + content: crate::from_raw(raw.content), }) } } @@ -55,11 +37,28 @@ impl Serialize for IgnoredUserListEvent { } } +/// The payload for `IgnoredUserListEvent`. +#[derive(Clone, Debug, PartialEq, Serialize)] +pub struct IgnoredUserListEventContent { + /// A list of users to ignore. + pub ignored_users: Vec, +} + +impl EventResultCompatible for IgnoredUserListEventContent { + type Raw = raw::IgnoredUserListEventContent; + type Err = Void; + + fn try_from_raw(raw: raw::IgnoredUserListEventContent) -> Result { + Ok(Self { + ignored_users: raw.ignored_users, + }) + } +} + impl_event!( IgnoredUserListEvent, IgnoredUserListEventContent, - EventType::IgnoredUserList, - raw + EventType::IgnoredUserList ); pub(crate) mod raw { diff --git a/src/key/verification/start.rs b/src/key/verification/start.rs index db5f466a..71f2f6b4 100644 --- a/src/key/verification/start.rs +++ b/src/key/verification/start.rs @@ -1,7 +1,5 @@ //! Types for the *m.key.verification.start* event. -use std::convert::{TryFrom, TryInto as _}; - use ruma_identifiers::DeviceId; use serde::{de::Error, ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer}; use serde_json::{from_value, Value}; @@ -10,7 +8,7 @@ use super::{ HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString, VerificationMethod, }; -use crate::{Event, EventType, InvalidInput}; +use crate::{Event, EventResultCompatible, EventType, InvalidInput}; /// Begins an SAS key verification process. /// @@ -33,13 +31,14 @@ pub enum StartEventContent { __Nonexhaustive, } -impl TryFrom for StartEvent { - type Error = (raw::StartEvent, &'static str); +impl EventResultCompatible for StartEvent { + type Raw = raw::StartEvent; + type Err = &'static str; - fn try_from(raw: raw::StartEvent) -> Result { - match raw.content.try_into() { + fn try_from_raw(raw: raw::StartEvent) -> Result { + match StartEventContent::try_from_raw(raw.content) { Ok(content) => Ok(Self { content }), - Err((content, msg)) => Err((raw::StartEvent { content }, msg)), + Err((msg, content)) => Err((msg, raw::StartEvent { content })), } } } @@ -61,30 +60,30 @@ impl Serialize for StartEvent { impl_event!( StartEvent, StartEventContent, - EventType::KeyVerificationStart, - raw + EventType::KeyVerificationStart ); -impl TryFrom for StartEventContent { - type Error = (raw::StartEventContent, &'static str); +impl EventResultCompatible for StartEventContent { + type Raw = raw::StartEventContent; + type Err = &'static str; - fn try_from(raw: raw::StartEventContent) -> Result { + fn try_from_raw(raw: raw::StartEventContent) -> Result { match raw { raw::StartEventContent::MSasV1(content) => { if !content .key_agreement_protocols .contains(&KeyAgreementProtocol::Curve25519) { - return Err( - (raw::StartEventContent::MSasV1(content), + return Err(( "`key_agreement_protocols` must contain at least `KeyAgreementProtocol::Curve25519`", + raw::StartEventContent::MSasV1(content), )); } if !content.hashes.contains(&HashAlgorithm::Sha256) { return Err(( - raw::StartEventContent::MSasV1(content), "`hashes` must contain at least `HashAlgorithm::Sha256`", + raw::StartEventContent::MSasV1(content), )); } @@ -92,9 +91,9 @@ impl TryFrom for StartEventContent { .message_authentication_codes .contains(&MessageAuthenticationCode::HkdfHmacSha256) { - return Err( - (raw::StartEventContent::MSasV1(content), + return Err(( "`message_authentication_codes` must contain at least `MessageAuthenticationCode::HkdfHmacSha256`", + raw::StartEventContent::MSasV1(content), )); } @@ -102,9 +101,9 @@ impl TryFrom for StartEventContent { .short_authentication_string .contains(&ShortAuthenticationString::Decimal) { - return Err( - (raw::StartEventContent::MSasV1(content), + return Err(( "`short_authentication_string` must contain at least `ShortAuthenticationString::Decimal`", + raw::StartEventContent::MSasV1(content), )); } diff --git a/src/lib.rs b/src/lib.rs index 95529064..6d66ea1d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,11 +111,10 @@ //! because *m.room.message* implements a *more specific* event trait than `Event`. #![deny(missing_debug_implementations)] -#![deny(missing_docs)] +//#![deny(missing_docs)] //#![deny(warnings)] use std::{ - convert::TryInto, error::Error, fmt::{Debug, Display, Error as FmtError, Formatter, Result as FmtResult}, }; @@ -249,7 +248,10 @@ impl Error for InvalidInput {} /// Marks types that can be deserialized as EventResult pub trait EventResultCompatible: Sized { /// The raw form of this event that deserialization falls back to if deserializing `Self` fails. - type Raw: DeserializeOwned + TryInto; + type Raw: DeserializeOwned; + type Err: Into; + + fn try_from_raw(_: Self::Raw) -> Result; } /// An empty type @@ -262,13 +264,13 @@ impl From for String { } } -fn convert_content(res: Raw) -> T +fn from_raw(raw: T::Raw) -> T where - Raw: TryInto, + T: EventResultCompatible, { - match res.try_into() { + match T::try_from_raw(raw) { Ok(c) => c, - Err((_, void)) => match void {}, + Err((void, _)) => match void {}, } } @@ -299,11 +301,9 @@ impl EventResult { } } -impl<'de, T, E> Deserialize<'de> for EventResult +impl<'de, T> Deserialize<'de> for EventResult where T: EventResultCompatible, - T::Raw: TryInto, - E: Into, { fn deserialize(deserializer: D) -> Result where @@ -323,9 +323,9 @@ where } }; - match raw_data.try_into() { + match T::try_from_raw(raw_data) { Ok(value) => Ok(EventResult::Ok(value)), - Err((raw_data, msg)) => Ok(EventResult::Err(InvalidEvent( + Err((msg, raw_data)) => Ok(EventResult::Err(InvalidEvent( InnerInvalidEvent::Validation { message: msg.into(), raw_data, diff --git a/src/macros.rs b/src/macros.rs index b99daa71..7df93d7b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -25,15 +25,7 @@ macro_rules! impl_enum { } macro_rules! impl_event { - ($name:ident, $content_name:ident, $event_type:path, $raw_mod:ident) => { - impl crate::EventResultCompatible for $name { - type Raw = $raw_mod::$name; - } - - impl crate::EventResultCompatible for $content_name { - type Raw = $raw_mod::$content_name; - } - + ($name:ident, $content_name:ident, $event_type:path) => { impl crate::Event for $name { /// The type of this event's `content` field. type Content = $content_name; @@ -52,10 +44,10 @@ macro_rules! impl_event { } macro_rules! impl_room_event { - ($name:ident, $content_name:ident, $event_type:path, $raw_mod:ident) => { - impl_event!($name, $content_name, $event_type, $raw_mod); + ($name:ident, $content_name:ident, $event_type:path) => { + impl_event!($name, $content_name, $event_type); - impl RoomEvent for $name { + impl crate::RoomEvent for $name { /// The unique identifier for the event. fn event_id(&self) -> &EventId { &self.event_id @@ -89,10 +81,10 @@ macro_rules! impl_room_event { } macro_rules! impl_state_event { - ($name:ident, $content_name:ident, $event_type:path, $raw_mod:ident) => { - impl_room_event!($name, $content_name, $event_type, $raw_mod); + ($name:ident, $content_name:ident, $event_type:path) => { + impl_room_event!($name, $content_name, $event_type); - impl StateEvent for $name { + impl crate::StateEvent for $name { /// The previous content for this state key, if any. fn prev_content(&self) -> Option<&Self::Content> { self.prev_content.as_ref() diff --git a/src/room/canonical_alias.rs b/src/room/canonical_alias.rs index ea3d4f0f..da98e350 100644 --- a/src/room/canonical_alias.rs +++ b/src/room/canonical_alias.rs @@ -1,13 +1,11 @@ //! Types for the *m.room.canonical_alias* event. -use std::convert::TryFrom; - use js_int::UInt; 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, EventType, RoomEvent, StateEvent, Void}; +use crate::{empty_string_as_none, Event, EventResultCompatible, EventType, Void}; /// Informs the room as to which alias is the canonical one. #[derive(Clone, Debug, PartialEq)] @@ -47,15 +45,16 @@ pub struct CanonicalAliasEventContent { pub alias: Option, } -impl TryFrom for CanonicalAliasEvent { - type Error = (raw::CanonicalAliasEvent, Void); +impl EventResultCompatible for CanonicalAliasEvent { + type Raw = raw::CanonicalAliasEvent; + type Err = Void; - fn try_from(raw: raw::CanonicalAliasEvent) -> Result { + fn try_from_raw(raw: raw::CanonicalAliasEvent) -> Result { Ok(Self { - content: crate::convert_content(raw.content), + content: crate::from_raw(raw.content), event_id: raw.event_id, origin_server_ts: raw.origin_server_ts, - prev_content: raw.prev_content.map(crate::convert_content), + prev_content: raw.prev_content.map(crate::from_raw), room_id: raw.room_id, sender: raw.sender, state_key: raw.state_key, @@ -64,10 +63,11 @@ impl TryFrom for CanonicalAliasEvent { } } -impl TryFrom for CanonicalAliasEventContent { - type Error = (raw::CanonicalAliasEventContent, Void); +impl EventResultCompatible for CanonicalAliasEventContent { + type Raw = raw::CanonicalAliasEventContent; + type Err = Void; - fn try_from(raw: raw::CanonicalAliasEventContent) -> Result { + fn try_from_raw(raw: raw::CanonicalAliasEventContent) -> Result { Ok(Self { alias: raw.alias }) } } @@ -120,8 +120,7 @@ impl Serialize for CanonicalAliasEvent { impl_state_event!( CanonicalAliasEvent, CanonicalAliasEventContent, - EventType::RoomCanonicalAlias, - raw + EventType::RoomCanonicalAlias ); pub(crate) mod raw { diff --git a/src/room/encrypted.rs b/src/room/encrypted.rs index 5ce5ccdd..192b821c 100644 --- a/src/room/encrypted.rs +++ b/src/room/encrypted.rs @@ -1,13 +1,11 @@ //! Types for the *m.room.encrypted* event. -use std::convert::TryFrom; - use js_int::UInt; 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, EventType, RoomEvent, Void}; +use crate::{Algorithm, Event, EventResultCompatible, EventType, Void}; /// This event type is used when sending encrypted events. /// @@ -50,12 +48,13 @@ pub enum EncryptedEventContent { __Nonexhaustive, } -impl TryFrom for EncryptedEvent { - type Error = (raw::EncryptedEvent, Void); +impl EventResultCompatible for EncryptedEvent { + type Raw = raw::EncryptedEvent; + type Err = Void; - fn try_from(raw: raw::EncryptedEvent) -> Result { + fn try_from_raw(raw: raw::EncryptedEvent) -> Result { Ok(Self { - content: crate::convert_content(raw.content), + content: crate::from_raw(raw.content), event_id: raw.event_id, origin_server_ts: raw.origin_server_ts, room_id: raw.room_id, @@ -65,10 +64,11 @@ impl TryFrom for EncryptedEvent { } } -impl TryFrom for EncryptedEventContent { - type Error = (raw::EncryptedEventContent, Void); +impl EventResultCompatible for EncryptedEventContent { + type Raw = raw::EncryptedEventContent; + type Err = Void; - fn try_from(raw: raw::EncryptedEventContent) -> Result { + fn try_from_raw(raw: raw::EncryptedEventContent) -> Result { use raw::EncryptedEventContent::*; Ok(match raw { @@ -120,8 +120,7 @@ impl Serialize for EncryptedEvent { impl_room_event!( EncryptedEvent, EncryptedEventContent, - EventType::RoomEncrypted, - raw + EventType::RoomEncrypted ); impl Serialize for EncryptedEventContent { diff --git a/src/room/message.rs b/src/room/message.rs index c915ddc8..947fae7c 100644 --- a/src/room/message.rs +++ b/src/room/message.rs @@ -1,7 +1,5 @@ //! Types for the *m.room.message* event. -use std::convert::TryFrom; - use js_int::UInt; use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{ @@ -12,7 +10,7 @@ use serde::{ use serde_json::{from_value, Value}; use super::{EncryptedFile, ImageInfo, ThumbnailInfo}; -use crate::{Event, EventType, RoomEvent, Void}; +use crate::{Event, EventResultCompatible, EventType, Void}; pub mod feedback; @@ -76,12 +74,13 @@ pub enum MessageEventContent { __Nonexhaustive, } -impl TryFrom for MessageEvent { - type Error = (raw::MessageEvent, Void); +impl EventResultCompatible for MessageEvent { + type Raw = raw::MessageEvent; + type Err = Void; - fn try_from(raw: raw::MessageEvent) -> Result { + fn try_from_raw(raw: raw::MessageEvent) -> Result { Ok(Self { - content: crate::convert_content(raw.content), + content: crate::from_raw(raw.content), event_id: raw.event_id, origin_server_ts: raw.origin_server_ts, room_id: raw.room_id, @@ -91,10 +90,11 @@ impl TryFrom for MessageEvent { } } -impl TryFrom for MessageEventContent { - type Error = (raw::MessageEventContent, Void); +impl EventResultCompatible for MessageEventContent { + type Raw = raw::MessageEventContent; + type Err = Void; - fn try_from(raw: raw::MessageEventContent) -> Result { + fn try_from_raw(raw: raw::MessageEventContent) -> Result { use raw::MessageEventContent::*; Ok(match raw { @@ -150,12 +150,7 @@ impl Serialize for MessageEvent { } } -impl_room_event!( - MessageEvent, - MessageEventContent, - EventType::RoomMessage, - raw -); +impl_room_event!(MessageEvent, MessageEventContent, EventType::RoomMessage); impl Serialize for MessageEventContent { fn serialize(&self, serializer: S) -> Result diff --git a/src/room/name.rs b/src/room/name.rs index 03e71204..7bb8c2e8 100644 --- a/src/room/name.rs +++ b/src/room/name.rs @@ -1,14 +1,12 @@ //! Types for the *m.room.name* event. -use std::convert::TryFrom; - use js_int::UInt; 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 _, EventType, InvalidInput, RoomEvent, StateEvent, Void, + empty_string_as_none, Event as _, EventResultCompatible, EventType, InvalidInput, Void, }; /// A human-friendly room name designed to be displayed to the end-user. @@ -47,15 +45,16 @@ pub struct NameEventContent { pub(crate) name: Option, } -impl TryFrom for NameEvent { - type Error = (raw::NameEvent, Void); +impl EventResultCompatible for NameEvent { + type Raw = raw::NameEvent; + type Err = Void; - fn try_from(raw: raw::NameEvent) -> Result { + fn try_from_raw(raw: raw::NameEvent) -> Result { Ok(Self { - content: crate::convert_content(raw.content), + content: crate::from_raw(raw.content), event_id: raw.event_id, origin_server_ts: raw.origin_server_ts, - prev_content: raw.prev_content.map(crate::convert_content), + prev_content: raw.prev_content.map(crate::from_raw), room_id: raw.room_id, sender: raw.sender, state_key: raw.state_key, @@ -64,10 +63,11 @@ impl TryFrom for NameEvent { } } -impl TryFrom for NameEventContent { - type Error = (raw::NameEventContent, Void); +impl EventResultCompatible for NameEventContent { + type Raw = raw::NameEventContent; + type Err = Void; - fn try_from(raw: raw::NameEventContent) -> Result { + fn try_from_raw(raw: raw::NameEventContent) -> Result { Ok(Self { name: raw.name }) } } @@ -117,7 +117,7 @@ impl Serialize for NameEvent { } } -impl_state_event!(NameEvent, NameEventContent, EventType::RoomName, raw); +impl_state_event!(NameEvent, NameEventContent, EventType::RoomName); impl NameEventContent { /// Create a new `NameEventContent` with the given name. diff --git a/src/room/power_levels.rs b/src/room/power_levels.rs index e1d5f573..e6c34ed9 100644 --- a/src/room/power_levels.rs +++ b/src/room/power_levels.rs @@ -1,13 +1,13 @@ //! Types for the *m.room.power_levels* event. -use std::{collections::HashMap, convert::TryFrom}; +use std::collections::HashMap; use js_int::{Int, UInt}; use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; use serde_json::Value; -use crate::{Event, EventType, RoomEvent, StateEvent, Void}; +use crate::{Event as _, EventResultCompatible, EventType, Void}; /// Defines the power levels (privileges) of users in the room. #[derive(Clone, Debug, PartialEq)] @@ -85,15 +85,16 @@ pub struct PowerLevelsEventContent { pub notifications: NotificationPowerLevels, } -impl TryFrom for PowerLevelsEvent { - type Error = (raw::PowerLevelsEvent, Void); +impl EventResultCompatible for PowerLevelsEvent { + type Raw = raw::PowerLevelsEvent; + type Err = Void; - fn try_from(raw: raw::PowerLevelsEvent) -> Result { + fn try_from_raw(raw: raw::PowerLevelsEvent) -> Result { Ok(Self { - content: crate::convert_content(raw.content), + content: crate::from_raw(raw.content), event_id: raw.event_id, origin_server_ts: raw.origin_server_ts, - prev_content: raw.prev_content.map(crate::convert_content), + prev_content: raw.prev_content.map(crate::from_raw), room_id: raw.room_id, unsigned: raw.unsigned, sender: raw.sender, @@ -102,10 +103,11 @@ impl TryFrom for PowerLevelsEvent { } } -impl TryFrom for PowerLevelsEventContent { - type Error = (raw::PowerLevelsEventContent, Void); +impl EventResultCompatible for PowerLevelsEventContent { + type Raw = raw::PowerLevelsEventContent; + type Err = Void; - fn try_from(raw: raw::PowerLevelsEventContent) -> Result { + fn try_from_raw(raw: raw::PowerLevelsEventContent) -> Result { Ok(Self { ban: raw.ban, events: raw.events, @@ -169,8 +171,7 @@ impl Serialize for PowerLevelsEvent { impl_state_event!( PowerLevelsEvent, PowerLevelsEventContent, - EventType::RoomPowerLevels, - raw + EventType::RoomPowerLevels ); pub(crate) mod raw { diff --git a/src/room/server_acl.rs b/src/room/server_acl.rs index f98cee82..3ad333a3 100644 --- a/src/room/server_acl.rs +++ b/src/room/server_acl.rs @@ -1,13 +1,11 @@ //! Types for the *m.room.server_acl* event. -use std::convert::TryFrom; - use js_int::UInt; use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer}; use serde_json::Value; -use crate::{default_true, Event as _, EventType, RoomEvent, StateEvent, Void}; +use crate::{default_true, Event as _, EventResultCompatible, EventType, Void}; /// An event to indicate which servers are permitted to participate in the room. #[derive(Clone, Debug, PartialEq)] @@ -67,15 +65,16 @@ pub struct ServerAclEventContent { pub deny: Vec, } -impl TryFrom for ServerAclEvent { - type Error = (raw::ServerAclEvent, Void); +impl EventResultCompatible for ServerAclEvent { + type Raw = raw::ServerAclEvent; + type Err = Void; - fn try_from(raw: raw::ServerAclEvent) -> Result { + fn try_from_raw(raw: raw::ServerAclEvent) -> Result { Ok(Self { - content: crate::convert_content(raw.content), + content: crate::from_raw(raw.content), event_id: raw.event_id, origin_server_ts: raw.origin_server_ts, - prev_content: raw.prev_content.map(crate::convert_content), + prev_content: raw.prev_content.map(crate::from_raw), room_id: raw.room_id, sender: raw.sender, state_key: raw.state_key, @@ -84,10 +83,11 @@ impl TryFrom for ServerAclEvent { } } -impl TryFrom for ServerAclEventContent { - type Error = (raw::ServerAclEventContent, Void); +impl EventResultCompatible for ServerAclEventContent { + type Raw = raw::ServerAclEventContent; + type Err = Void; - fn try_from(raw: raw::ServerAclEventContent) -> Result { + fn try_from_raw(raw: raw::ServerAclEventContent) -> Result { Ok(Self { allow_ip_literals: raw.allow_ip_literals, allow: raw.allow, @@ -113,8 +113,7 @@ impl Serialize for ServerAclEvent { impl_state_event!( ServerAclEvent, ServerAclEventContent, - EventType::RoomServerAcl, - raw + EventType::RoomServerAcl ); pub(crate) mod raw { diff --git a/src/stripped.rs b/src/stripped.rs index bb5d1348..cff76865 100644 --- a/src/stripped.rs +++ b/src/stripped.rs @@ -5,10 +5,8 @@ //! state event to be created, when the other fields can be inferred from a larger context, or where //! the other fields are otherwise inapplicable. -use std::convert::TryFrom; - use ruma_identifiers::UserId; -use serde::{Serialize, Serializer}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::{ room::{ @@ -118,40 +116,29 @@ pub type StrippedRoomTopic = StrippedStateContent; impl EventResultCompatible for StrippedState { type Raw = raw::StrippedState; -} + type Err = String; -impl TryFrom for StrippedState { - type Error = (raw::StrippedState, String); - - fn try_from(raw: raw::StrippedState) -> Result { + fn try_from_raw(raw: raw::StrippedState) -> Result { unimplemented!() } } -/*impl EventResultCompatible for StrippedStateContent +impl EventResultCompatible for StrippedStateContent where C: EventResultCompatible, { type Raw = StrippedStateContent; -} + type Err = C::Err; -// Orphan impl :( -impl TryFrom> for StrippedStateContent -where - C: EventResultCompatible, - C::Raw: TryInto, -{ - type Error = (StrippedStateContent, E); - - fn try_from(raw: StrippedStateContent) -> Result { + fn try_from_raw(raw: StrippedStateContent) -> Result { Ok(Self { - content: raw.content.try_into()?, + content: C::try_from_raw(raw.content).map_err(|(msg, raw)| (msg, unimplemented!()))?, event_type: raw.event_type, state_key: raw.state_key, sender: raw.sender, }) } -}*/ +} impl Serialize for StrippedState { fn serialize(&self, serializer: S) -> Result @@ -175,6 +162,18 @@ impl Serialize for StrippedState { } } +impl<'de, C> Deserialize<'de> for StrippedStateContent +where + C: Deserialize<'de>, +{ + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + unimplemented!() + } +} + mod raw { use serde::{Deserialize, Deserializer}; @@ -369,12 +368,12 @@ mod tests { }; // Ensure `StrippedStateContent` can be parsed, not just `StrippedState`. - /*assert!( + assert!( serde_json::from_str::>(name_event) .unwrap() .into_result() .is_ok() - );*/ + ); match serde_json::from_str::>(join_rules_event) .unwrap()