Implement custom event deserialization

This commit is contained in:
Jonas Platte 2019-10-27 21:01:28 +01:00
parent cbe22a84eb
commit 3b78391fbc
4 changed files with 37 additions and 16 deletions

View File

@ -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),
}
}
}

View File

@ -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),
}
}
}

View File

@ -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")),
}
}

View File

@ -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")),
}
}