diff --git a/src/collections/all.rs b/src/collections/all.rs index d51bcabd..a094fefe 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, TryFromRaw, Void, + CustomEvent, CustomRoomEvent, CustomStateEvent, TryFromRaw, }; /// A basic event, room event, or state event. @@ -155,7 +155,7 @@ pub enum Event { /// m.room.redaction RoomRedaction(RedactionEvent), - /// m.room.server_acl, + /// m.room.server_acl RoomServerAcl(ServerAclEvent), /// m.room.third_party_invite @@ -256,7 +256,7 @@ pub enum RoomEvent { /// m.room.redaction RoomRedaction(RedactionEvent), - /// m.room.server_acl, + /// m.room.server_acl RoomServerAcl(ServerAclEvent), /// m.room.third_party_invite @@ -318,7 +318,7 @@ pub enum StateEvent { /// m.room.power_levels RoomPowerLevels(PowerLevelsEvent), - /// m.room.server_acl, + /// m.room.server_acl RoomServerAcl(ServerAclEvent), /// m.room.third_party_invite @@ -339,7 +339,59 @@ impl TryFromRaw for Event { type Err = String; fn try_from_raw(raw: raw::Event) -> Result { - unimplemented!() + use crate::try_convert_variant as conv; + use raw::Event::*; + + match raw { + CallAnswer(c) => conv(CallAnswer, Self::CallAnswer, c), + CallCandidates(c) => conv(CallCandidates, Self::CallCandidates, c), + CallHangup(c) => conv(CallHangup, Self::CallHangup, c), + CallInvite(c) => conv(CallInvite, Self::CallInvite, c), + Direct(c) => conv(Direct, Self::Direct, c), + Dummy(c) => conv(Dummy, Self::Dummy, c), + ForwardedRoomKey(c) => conv(ForwardedRoomKey, Self::ForwardedRoomKey, c), + FullyRead(c) => conv(FullyRead, Self::FullyRead, c), + IgnoredUserList(c) => conv(IgnoredUserList, Self::IgnoredUserList, c), + KeyVerificationAccept(c) => conv(KeyVerificationAccept, Self::KeyVerificationAccept, c), + KeyVerificationCancel(c) => conv(KeyVerificationCancel, Self::KeyVerificationCancel, c), + KeyVerificationKey(c) => conv(KeyVerificationKey, Self::KeyVerificationKey, c), + KeyVerificationMac(c) => conv(KeyVerificationMac, Self::KeyVerificationMac, c), + KeyVerificationRequest(c) => { + conv(KeyVerificationRequest, Self::KeyVerificationRequest, c) + } + KeyVerificationStart(c) => conv(KeyVerificationStart, Self::KeyVerificationStart, c), + Presence(c) => conv(Presence, Self::Presence, c), + PushRules(c) => conv(PushRules, Self::PushRules, c), + Receipt(c) => conv(Receipt, Self::Receipt, c), + RoomAliases(c) => conv(RoomAliases, Self::RoomAliases, c), + RoomAvatar(c) => conv(RoomAvatar, Self::RoomAvatar, c), + RoomCanonicalAlias(c) => conv(RoomCanonicalAlias, Self::RoomCanonicalAlias, c), + RoomCreate(c) => conv(RoomCreate, Self::RoomCreate, c), + RoomEncrypted(c) => conv(RoomEncrypted, Self::RoomEncrypted, c), + RoomEncryption(c) => conv(RoomEncryption, Self::RoomEncryption, c), + RoomGuestAccess(c) => conv(RoomGuestAccess, Self::RoomGuestAccess, c), + RoomHistoryVisibility(c) => conv(RoomHistoryVisibility, Self::RoomHistoryVisibility, c), + RoomJoinRules(c) => conv(RoomJoinRules, Self::RoomJoinRules, c), + RoomMember(c) => conv(RoomMember, Self::RoomMember, c), + RoomMessage(c) => conv(RoomMessage, Self::RoomMessage, c), + RoomMessageFeedback(c) => conv(RoomMessageFeedback, Self::RoomMessageFeedback, c), + RoomName(c) => conv(RoomName, Self::RoomName, c), + RoomPinnedEvents(c) => conv(RoomPinnedEvents, Self::RoomPinnedEvents, c), + RoomPowerLevels(c) => conv(RoomPowerLevels, Self::RoomPowerLevels, c), + RoomRedaction(c) => conv(RoomRedaction, Self::RoomRedaction, c), + RoomServerAcl(c) => conv(RoomServerAcl, Self::RoomServerAcl, c), + RoomThirdPartyInvite(c) => conv(RoomThirdPartyInvite, Self::RoomThirdPartyInvite, c), + RoomTombstone(c) => conv(RoomTombstone, Self::RoomTombstone, c), + RoomTopic(c) => conv(RoomTopic, Self::RoomTopic, c), + RoomKey(c) => conv(RoomKey, Self::RoomKey, c), + RoomKeyRequest(c) => conv(RoomKeyRequest, Self::RoomKeyRequest, c), + Sticker(c) => conv(Sticker, Self::Sticker, c), + Tag(c) => conv(Tag, Self::Tag, c), + Typing(c) => conv(Typing, Self::Typing, c), + Custom(c) => Ok(Self::Custom(c)), + CustomRoom(c) => Ok(Self::CustomRoom(c)), + CustomState(c) => Ok(Self::CustomState(c)), + } } } @@ -348,16 +400,68 @@ impl TryFromRaw for RoomEvent { type Err = String; fn try_from_raw(raw: raw::RoomEvent) -> Result { - unimplemented!() + use crate::try_convert_variant as conv; + use raw::RoomEvent::*; + + match raw { + CallAnswer(c) => conv(CallAnswer, Self::CallAnswer, c), + CallCandidates(c) => conv(CallCandidates, Self::CallCandidates, c), + CallHangup(c) => conv(CallHangup, Self::CallHangup, c), + CallInvite(c) => conv(CallInvite, Self::CallInvite, c), + RoomAliases(c) => conv(RoomAliases, Self::RoomAliases, c), + RoomAvatar(c) => conv(RoomAvatar, Self::RoomAvatar, c), + RoomCanonicalAlias(c) => conv(RoomCanonicalAlias, Self::RoomCanonicalAlias, c), + RoomCreate(c) => conv(RoomCreate, Self::RoomCreate, c), + RoomEncrypted(c) => conv(RoomEncrypted, Self::RoomEncrypted, c), + RoomEncryption(c) => conv(RoomEncryption, Self::RoomEncryption, c), + RoomGuestAccess(c) => conv(RoomGuestAccess, Self::RoomGuestAccess, c), + RoomHistoryVisibility(c) => conv(RoomHistoryVisibility, Self::RoomHistoryVisibility, c), + RoomJoinRules(c) => conv(RoomJoinRules, Self::RoomJoinRules, c), + RoomMember(c) => conv(RoomMember, Self::RoomMember, c), + RoomMessage(c) => conv(RoomMessage, Self::RoomMessage, c), + RoomMessageFeedback(c) => conv(RoomMessageFeedback, Self::RoomMessageFeedback, c), + RoomName(c) => conv(RoomName, Self::RoomName, c), + RoomPinnedEvents(c) => conv(RoomPinnedEvents, Self::RoomPinnedEvents, c), + RoomPowerLevels(c) => conv(RoomPowerLevels, Self::RoomPowerLevels, c), + RoomRedaction(c) => conv(RoomRedaction, Self::RoomRedaction, c), + RoomServerAcl(c) => conv(RoomServerAcl, Self::RoomServerAcl, c), + RoomThirdPartyInvite(c) => conv(RoomThirdPartyInvite, Self::RoomThirdPartyInvite, c), + RoomTombstone(c) => conv(RoomTombstone, Self::RoomTombstone, c), + RoomTopic(c) => conv(RoomTopic, Self::RoomTopic, c), + Sticker(c) => conv(Sticker, Self::Sticker, c), + CustomRoom(c) => Ok(Self::CustomRoom(c)), + CustomState(c) => Ok(Self::CustomState(c)), + } } } impl TryFromRaw for StateEvent { type Raw = raw::StateEvent; - type Err = Void; + type Err = String; fn try_from_raw(raw: raw::StateEvent) -> Result { - unimplemented!() + use crate::try_convert_variant as conv; + use raw::StateEvent::*; + + match raw { + RoomAliases(c) => conv(RoomAliases, Self::RoomAliases, c), + RoomAvatar(c) => conv(RoomAvatar, Self::RoomAvatar, c), + RoomCanonicalAlias(c) => conv(RoomCanonicalAlias, Self::RoomCanonicalAlias, c), + RoomCreate(c) => conv(RoomCreate, Self::RoomCreate, c), + RoomEncryption(c) => conv(RoomEncryption, Self::RoomEncryption, c), + RoomGuestAccess(c) => conv(RoomGuestAccess, Self::RoomGuestAccess, c), + RoomHistoryVisibility(c) => conv(RoomHistoryVisibility, Self::RoomHistoryVisibility, c), + RoomJoinRules(c) => conv(RoomJoinRules, Self::RoomJoinRules, c), + RoomMember(c) => conv(RoomMember, Self::RoomMember, c), + RoomName(c) => conv(RoomName, Self::RoomName, c), + RoomPinnedEvents(c) => conv(RoomPinnedEvents, Self::RoomPinnedEvents, c), + RoomPowerLevels(c) => conv(RoomPowerLevels, Self::RoomPowerLevels, c), + RoomServerAcl(c) => conv(RoomServerAcl, Self::RoomServerAcl, c), + RoomThirdPartyInvite(c) => conv(RoomThirdPartyInvite, Self::RoomThirdPartyInvite, c), + RoomTombstone(c) => conv(RoomTombstone, Self::RoomTombstone, c), + RoomTopic(c) => conv(RoomTopic, Self::RoomTopic, c), + CustomState(c) => Ok(Self::CustomState(c)), + } } } diff --git a/src/collections/only.rs b/src/collections/only.rs index b535b6a3..a673aedc 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, TryFromRaw, Void, + CustomEvent, CustomRoomEvent, TryFromRaw, }; /// A basic event. @@ -132,19 +132,58 @@ pub enum RoomEvent { impl TryFromRaw for Event { type Raw = raw::Event; - type Err = Void; + type Err = String; fn try_from_raw(raw: raw::Event) -> Result { - unimplemented!() + use crate::try_convert_variant as conv; + use raw::Event::*; + + match raw { + Direct(c) => conv(Direct, Self::Direct, c), + Dummy(c) => conv(Dummy, Self::Dummy, c), + ForwardedRoomKey(c) => conv(ForwardedRoomKey, Self::ForwardedRoomKey, c), + FullyRead(c) => conv(FullyRead, Self::FullyRead, c), + KeyVerificationAccept(c) => conv(KeyVerificationAccept, Self::KeyVerificationAccept, c), + KeyVerificationCancel(c) => conv(KeyVerificationCancel, Self::KeyVerificationCancel, c), + KeyVerificationKey(c) => conv(KeyVerificationKey, Self::KeyVerificationKey, c), + KeyVerificationMac(c) => conv(KeyVerificationMac, Self::KeyVerificationMac, c), + KeyVerificationRequest(c) => { + conv(KeyVerificationRequest, Self::KeyVerificationRequest, c) + } + KeyVerificationStart(c) => conv(KeyVerificationStart, Self::KeyVerificationStart, c), + IgnoredUserList(c) => conv(IgnoredUserList, Self::IgnoredUserList, c), + Presence(c) => conv(Presence, Self::Presence, c), + PushRules(c) => conv(PushRules, Self::PushRules, c), + RoomKey(c) => conv(RoomKey, Self::RoomKey, c), + RoomKeyRequest(c) => conv(RoomKeyRequest, Self::RoomKeyRequest, c), + Receipt(c) => conv(Receipt, Self::Receipt, c), + Tag(c) => conv(Tag, Self::Tag, c), + Typing(c) => conv(Typing, Self::Typing, c), + Custom(c) => Ok(Self::Custom(c)), + } } } impl TryFromRaw for RoomEvent { type Raw = raw::RoomEvent; - type Err = Void; + type Err = String; fn try_from_raw(raw: raw::RoomEvent) -> Result { - unimplemented!() + use crate::try_convert_variant as conv; + use raw::RoomEvent::*; + + match raw { + CallAnswer(c) => conv(CallAnswer, Self::CallAnswer, c), + CallCandidates(c) => conv(CallCandidates, Self::CallCandidates, c), + CallHangup(c) => conv(CallHangup, Self::CallHangup, c), + CallInvite(c) => conv(CallInvite, Self::CallInvite, c), + RoomEncrypted(c) => conv(RoomEncrypted, Self::RoomEncrypted, c), + RoomMessage(c) => conv(RoomMessage, Self::RoomMessage, c), + RoomMessageFeedback(c) => conv(RoomMessageFeedback, Self::RoomMessageFeedback, c), + RoomRedaction(c) => conv(RoomRedaction, Self::RoomRedaction, c), + Sticker(c) => conv(Sticker, Self::Sticker, c), + CustomRoom(c) => Ok(Self::CustomRoom(c)), + } } } diff --git a/src/collections/raw/all.rs b/src/collections/raw/all.rs index e457df24..cb4a1057 100644 --- a/src/collections/raw/all.rs +++ b/src/collections/raw/all.rs @@ -1,50 +1,51 @@ //! Enums for heterogeneous collections of events, inclusive for every event type that implements //! the trait of the same name. -use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use serde::{Deserialize, Deserializer}; use crate::{ call::{ - answer::AnswerEvent, candidates::CandidatesEvent, hangup::HangupEvent, invite::InviteEvent, + answer::raw::AnswerEvent, candidates::raw::CandidatesEvent, hangup::raw::HangupEvent, + invite::raw::InviteEvent, }, - direct::DirectEvent, - dummy::DummyEvent, - forwarded_room_key::ForwardedRoomKeyEvent, - fully_read::FullyReadEvent, - ignored_user_list::IgnoredUserListEvent, + direct::raw::DirectEvent, + dummy::raw::DummyEvent, + forwarded_room_key::raw::ForwardedRoomKeyEvent, + fully_read::raw::FullyReadEvent, + ignored_user_list::raw::IgnoredUserListEvent, key::verification::{ - accept::AcceptEvent, cancel::CancelEvent, key::KeyEvent, mac::MacEvent, - request::RequestEvent, start::StartEvent, + accept::raw::AcceptEvent, cancel::raw::CancelEvent, key::raw::KeyEvent, mac::raw::MacEvent, + request::raw::RequestEvent, start::raw::StartEvent, }, - presence::PresenceEvent, - push_rules::PushRulesEvent, - receipt::ReceiptEvent, + presence::raw::PresenceEvent, + push_rules::raw::PushRulesEvent, + receipt::raw::ReceiptEvent, room::{ - aliases::AliasesEvent, - avatar::AvatarEvent, - canonical_alias::CanonicalAliasEvent, - create::CreateEvent, - encrypted::EncryptedEvent, - encryption::EncryptionEvent, - guest_access::GuestAccessEvent, - history_visibility::HistoryVisibilityEvent, - join_rules::JoinRulesEvent, - member::MemberEvent, - message::{feedback::FeedbackEvent, MessageEvent}, - name::NameEvent, - pinned_events::PinnedEventsEvent, - power_levels::PowerLevelsEvent, - redaction::RedactionEvent, - server_acl::ServerAclEvent, - third_party_invite::ThirdPartyInviteEvent, - tombstone::TombstoneEvent, - topic::TopicEvent, + aliases::raw::AliasesEvent, + avatar::raw::AvatarEvent, + canonical_alias::raw::CanonicalAliasEvent, + create::raw::CreateEvent, + encrypted::raw::EncryptedEvent, + encryption::raw::EncryptionEvent, + guest_access::raw::GuestAccessEvent, + history_visibility::raw::HistoryVisibilityEvent, + join_rules::raw::JoinRulesEvent, + member::raw::MemberEvent, + message::{feedback::raw::FeedbackEvent, raw::MessageEvent}, + name::raw::NameEvent, + pinned_events::raw::PinnedEventsEvent, + power_levels::raw::PowerLevelsEvent, + redaction::raw::RedactionEvent, + server_acl::raw::ServerAclEvent, + third_party_invite::raw::ThirdPartyInviteEvent, + tombstone::raw::TombstoneEvent, + topic::raw::TopicEvent, }, - room_key::RoomKeyEvent, - room_key_request::RoomKeyRequestEvent, - sticker::StickerEvent, - tag::TagEvent, - typing::TypingEvent, + room_key::raw::RoomKeyEvent, + room_key_request::raw::RoomKeyRequestEvent, + sticker::raw::StickerEvent, + tag::raw::TagEvent, + typing::raw::TypingEvent, CustomEvent, CustomRoomEvent, CustomStateEvent, }; @@ -154,7 +155,7 @@ pub enum Event { /// m.room.redaction RoomRedaction(RedactionEvent), - /// m.room.server_acl, + /// m.room.server_acl RoomServerAcl(ServerAclEvent), /// m.room.third_party_invite @@ -255,7 +256,7 @@ pub enum RoomEvent { /// m.room.redaction RoomRedaction(RedactionEvent), - /// m.room.server_acl, + /// m.room.server_acl RoomServerAcl(ServerAclEvent), /// m.room.third_party_invite @@ -317,7 +318,7 @@ pub enum StateEvent { /// m.room.power_levels RoomPowerLevels(PowerLevelsEvent), - /// m.room.server_acl, + /// m.room.server_acl RoomServerAcl(ServerAclEvent), /// m.room.third_party_invite @@ -360,7 +361,7 @@ impl<'de> Deserialize<'de> for StateEvent { } } -impl Serialize for Event { +/*impl Serialize for Event { fn serialize(&self, serializer: S) -> Result where S: Serializer, @@ -478,7 +479,7 @@ impl Serialize for StateEvent { StateEvent::CustomState(ref event) => event.serialize(serializer), } } -} +}*/ macro_rules! impl_from_t_for_event { ($ty:ty, $variant:ident) => { diff --git a/src/lib.rs b/src/lib.rs index 0178adb9..f8f70c24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -272,6 +272,16 @@ impl TryFromRaw for T { } } +fn try_convert_variant( + raw_variant: fn(Content::Raw) -> Enum::Raw, + variant: fn(Content) -> Enum, + raw: Content::Raw, +) -> Result { + Content::try_from_raw(raw) + .map(variant) + .map_err(|(msg, raw)| (msg.into(), raw_variant(raw))) +} + // TODO: Replace with ! once that is stable /// An empty type #[derive(Debug)] diff --git a/src/stripped.rs b/src/stripped.rs index 81949112..3b2bd003 100644 --- a/src/stripped.rs +++ b/src/stripped.rs @@ -121,33 +121,22 @@ impl TryFromRaw for StrippedState { type Err = String; fn try_from_raw(raw: raw::StrippedState) -> Result { + use crate::try_convert_variant as conv; use raw::StrippedState::*; - fn convert( - raw_variant: fn(T::Raw) -> raw::StrippedState, - variant: fn(T) -> StrippedState, - raw: T::Raw, - ) -> Result { - T::try_from_raw(raw) - .map(variant) - .map_err(|(msg, raw)| (msg.into(), raw_variant(raw))) - } - match raw { - RoomAliases(c) => convert(RoomAliases, Self::RoomAliases, c), - RoomAvatar(c) => convert(RoomAvatar, Self::RoomAvatar, c), - RoomCanonicalAlias(c) => convert(RoomCanonicalAlias, Self::RoomCanonicalAlias, c), - RoomCreate(c) => convert(RoomCreate, Self::RoomCreate, c), - RoomGuestAccess(c) => convert(RoomGuestAccess, Self::RoomGuestAccess, c), - RoomHistoryVisibility(c) => { - convert(RoomHistoryVisibility, Self::RoomHistoryVisibility, c) - } - RoomJoinRules(c) => convert(RoomJoinRules, Self::RoomJoinRules, c), - RoomMember(c) => convert(RoomMember, Self::RoomMember, c), - RoomName(c) => convert(RoomName, Self::RoomName, c), - RoomPowerLevels(c) => convert(RoomPowerLevels, Self::RoomPowerLevels, c), - RoomThirdPartyInvite(c) => convert(RoomThirdPartyInvite, Self::RoomThirdPartyInvite, c), - RoomTopic(c) => convert(RoomTopic, Self::RoomTopic, c), + RoomAliases(c) => conv(RoomAliases, Self::RoomAliases, c), + RoomAvatar(c) => conv(RoomAvatar, Self::RoomAvatar, c), + RoomCanonicalAlias(c) => conv(RoomCanonicalAlias, Self::RoomCanonicalAlias, c), + RoomCreate(c) => conv(RoomCreate, Self::RoomCreate, c), + RoomGuestAccess(c) => conv(RoomGuestAccess, Self::RoomGuestAccess, c), + RoomHistoryVisibility(c) => conv(RoomHistoryVisibility, Self::RoomHistoryVisibility, c), + RoomJoinRules(c) => conv(RoomJoinRules, Self::RoomJoinRules, c), + RoomMember(c) => conv(RoomMember, Self::RoomMember, c), + RoomName(c) => conv(RoomName, Self::RoomName, c), + RoomPowerLevels(c) => conv(RoomPowerLevels, Self::RoomPowerLevels, c), + RoomThirdPartyInvite(c) => conv(RoomThirdPartyInvite, Self::RoomThirdPartyInvite, c), + RoomTopic(c) => conv(RoomTopic, Self::RoomTopic, c), } } }