From 3b78391fbcd351c5fce68ecf76a8f1f3c9ed91cd Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 27 Oct 2019 21:01:28 +0100 Subject: [PATCH] Implement custom event deserialization --- src/collections/all.rs | 12 ++++++------ src/collections/only.rs | 4 ++-- src/collections/raw/all.rs | 29 ++++++++++++++++++++++++----- src/collections/raw/only.rs | 8 +++++--- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/collections/all.rs b/src/collections/all.rs index 6482f42c..00ca26da 100644 --- a/src/collections/all.rs +++ b/src/collections/all.rs @@ -394,9 +394,9 @@ impl TryFromRaw for Event { Sticker(c) => conv(Sticker, Event::Sticker, c), Tag(c) => conv(Tag, Event::Tag, c), Typing(c) => conv(Typing, Event::Typing, c), - Custom(c) => Ok(Event::Custom(c)), - CustomRoom(c) => Ok(Event::CustomRoom(c)), - CustomState(c) => Ok(Event::CustomState(c)), + Custom(c) => conv(Custom, Event::Custom, c), + CustomRoom(c) => conv(CustomRoom, Event::CustomRoom, c), + CustomState(c) => conv(CustomState, Event::CustomState, c), } } } @@ -439,8 +439,8 @@ impl TryFromRaw for RoomEvent { RoomTombstone(c) => conv(RoomTombstone, RoomEvent::RoomTombstone, c), RoomTopic(c) => conv(RoomTopic, RoomEvent::RoomTopic, c), Sticker(c) => conv(Sticker, RoomEvent::Sticker, c), - CustomRoom(c) => Ok(RoomEvent::CustomRoom(c)), - CustomState(c) => Ok(RoomEvent::CustomState(c)), + CustomRoom(c) => conv(CustomRoom, RoomEvent::CustomRoom, c), + CustomState(c) => conv(CustomState, RoomEvent::CustomState, c), } } } @@ -474,7 +474,7 @@ impl TryFromRaw for StateEvent { } RoomTombstone(c) => conv(RoomTombstone, StateEvent::RoomTombstone, c), RoomTopic(c) => conv(RoomTopic, StateEvent::RoomTopic, c), - CustomState(c) => Ok(StateEvent::CustomState(c)), + CustomState(c) => conv(CustomState, StateEvent::CustomState, c), } } } diff --git a/src/collections/only.rs b/src/collections/only.rs index e4741abd..a532090b 100644 --- a/src/collections/only.rs +++ b/src/collections/only.rs @@ -163,7 +163,7 @@ impl TryFromRaw for Event { Receipt(c) => conv(Receipt, Event::Receipt, c), Tag(c) => conv(Tag, Event::Tag, c), Typing(c) => conv(Typing, Event::Typing, c), - Custom(c) => Ok(Event::Custom(c)), + Custom(c) => conv(Custom, Event::Custom, c), } } } @@ -186,7 +186,7 @@ impl TryFromRaw for RoomEvent { RoomMessageFeedback(c) => conv(RoomMessageFeedback, RoomEvent::RoomMessageFeedback, c), RoomRedaction(c) => conv(RoomRedaction, RoomEvent::RoomRedaction, c), Sticker(c) => conv(Sticker, RoomEvent::Sticker, c), - CustomRoom(c) => Ok(RoomEvent::CustomRoom(c)), + CustomRoom(c) => conv(CustomRoom, RoomEvent::CustomRoom, c), } } } diff --git a/src/collections/raw/all.rs b/src/collections/raw/all.rs index 126758a5..0f4ba82f 100644 --- a/src/collections/raw/all.rs +++ b/src/collections/raw/all.rs @@ -9,6 +9,9 @@ use crate::{ answer::raw::AnswerEvent, candidates::raw::CandidatesEvent, hangup::raw::HangupEvent, invite::raw::InviteEvent, }, + custom::raw::CustomEvent, + custom_room::raw::CustomRoomEvent, + custom_state::raw::CustomStateEvent, direct::raw::DirectEvent, dummy::raw::DummyEvent, forwarded_room_key::raw::ForwardedRoomKeyEvent, @@ -48,7 +51,7 @@ use crate::{ tag::raw::TagEvent, typing::raw::TypingEvent, util::get_field, - CustomEvent, CustomRoomEvent, CustomStateEvent, EventType, + EventType, }; /// A basic event, room event, or state event. @@ -391,8 +394,18 @@ impl<'de> Deserialize<'de> for Event { Sticker => from_value(value, Event::Sticker), Tag => from_value(value, Event::Tag), Typing => from_value(value, Event::Typing), - // TODO - Custom(_event_type_name) => Err(D::Error::custom("invalid event type")), + Custom(_event_type_name) => { + if value.get("state_key").is_some() { + from_value(value, Event::CustomState) + } else if value.get("event_id").is_some() + && value.get("room_id").is_some() + && value.get("sender").is_some() + { + from_value(value, Event::CustomRoom) + } else { + from_value(value, Event::Custom) + } + } __Nonexhaustive => { unreachable!("__Nonexhaustive variant should be impossible to obtain.") } @@ -437,7 +450,13 @@ impl<'de> Deserialize<'de> for RoomEvent { RoomTombstone => from_value(value, RoomEvent::RoomTombstone), RoomTopic => from_value(value, RoomEvent::RoomTopic), Sticker => from_value(value, RoomEvent::Sticker), - //Custom(_event_type_name) => unimplemented!("todo"), + Custom(_event_type_name) => { + if value.get("state_key").is_some() { + from_value(value, RoomEvent::CustomState) + } else { + from_value(value, RoomEvent::CustomRoom) + } + } _ => Err(D::Error::custom("invalid event type")), } } @@ -471,7 +490,7 @@ impl<'de> Deserialize<'de> for StateEvent { RoomThirdPartyInvite => from_value(value, StateEvent::RoomThirdPartyInvite), RoomTombstone => from_value(value, StateEvent::RoomTombstone), RoomTopic => from_value(value, StateEvent::RoomTopic), - //Custom(_event_type_name) => unimplemented!("todo"), + Custom(_event_type_name) => from_value(value, StateEvent::CustomState), _ => Err(D::Error::custom("invalid event type")), } } diff --git a/src/collections/raw/only.rs b/src/collections/raw/only.rs index 75d5a184..fd4fce70 100644 --- a/src/collections/raw/only.rs +++ b/src/collections/raw/only.rs @@ -10,6 +10,8 @@ use crate::{ answer::raw::AnswerEvent, candidates::raw::CandidatesEvent, hangup::raw::HangupEvent, invite::raw::InviteEvent, }, + custom::raw::CustomEvent, + custom_room::raw::CustomRoomEvent, direct::raw::DirectEvent, dummy::raw::DummyEvent, forwarded_room_key::raw::ForwardedRoomKeyEvent, @@ -33,7 +35,7 @@ use crate::{ tag::raw::TagEvent, typing::raw::TypingEvent, util::get_field, - CustomEvent, CustomRoomEvent, EventType, + EventType, }; /// A basic event. @@ -173,7 +175,7 @@ impl<'de> Deserialize<'de> for Event { Receipt => from_value(value, Event::Receipt), Tag => from_value(value, Event::Tag), Typing => from_value(value, Event::Typing), - //Custom(_event_type_name) => unimplemented!("todo"), + Custom(_event_type_name) => from_value(value, Event::Custom), _ => Err(D::Error::custom("invalid event type")), } } @@ -200,7 +202,7 @@ impl<'de> Deserialize<'de> for RoomEvent { RoomMessageFeedback => from_value(value, RoomEvent::RoomMessageFeedback), RoomRedaction => from_value(value, RoomEvent::RoomRedaction), Sticker => from_value(value, RoomEvent::Sticker), - //Custom(_event_type_name) => unimplemented!("todo"), + Custom(_event_type_name) => from_value(value, RoomEvent::CustomRoom), _ => Err(D::Error::custom("invalid event type")), } }