Add missing TryFromRaw implementations

This commit is contained in:
Jonas Platte 2019-10-15 23:45:45 +02:00
parent 09b8de5b6e
commit 1aaee389ec
5 changed files with 221 additions and 78 deletions

View File

@ -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<Self, (Self::Err, Self::Raw)> {
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<Self, (Self::Err, Self::Raw)> {
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<Self, (Self::Err, Self::Raw)> {
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)),
}
}
}

View File

@ -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<Self, (Self::Err, Self::Raw)> {
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<Self, (Self::Err, Self::Raw)> {
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)),
}
}
}

View File

@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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) => {

View File

@ -272,6 +272,16 @@ impl<T: FromRaw> TryFromRaw for T {
}
}
fn try_convert_variant<Enum: TryFromRaw, Content: TryFromRaw>(
raw_variant: fn(Content::Raw) -> Enum::Raw,
variant: fn(Content) -> Enum,
raw: Content::Raw,
) -> Result<Enum, (String, Enum::Raw)> {
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)]

View File

@ -121,33 +121,22 @@ impl TryFromRaw for StrippedState {
type Err = String;
fn try_from_raw(raw: raw::StrippedState) -> Result<Self, (Self::Err, Self::Raw)> {
use crate::try_convert_variant as conv;
use raw::StrippedState::*;
fn convert<T: TryFromRaw>(
raw_variant: fn(T::Raw) -> raw::StrippedState,
variant: fn(T) -> StrippedState,
raw: T::Raw,
) -> Result<StrippedState, (String, raw::StrippedState)> {
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),
}
}
}