Convert Pdu to a validated ruma type when calling to_*_event

next
Devin R 2020-07-18 08:22:30 -04:00
parent 75959cdc10
commit 9a993fa7c1
1 changed files with 20 additions and 14 deletions

View File

@ -79,34 +79,40 @@ impl PduEvent {
Ok(()) Ok(())
} }
pub fn to_room_event_stub(&self) -> EventJson<AnySyncRoomEvent> { pub fn to_sync_room_event(&self) -> EventJson<AnySyncRoomEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid"); let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<AnySyncRoomEvent>>(&json) serde_json::from_str::<AnySyncRoomEvent>(&json)
.expect("EventJson::from_str always works") .map(EventJson::from)
.expect("AnySyncRoomEvent can always be built from a full PDU event")
} }
pub fn to_room_event(&self) -> EventJson<AnyRoomEvent> { pub fn to_room_event(&self) -> EventJson<AnyRoomEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid"); let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<AnyRoomEvent>>(&json) serde_json::from_str::<AnyRoomEvent>(&json)
.expect("EventJson::from_str always works") .map(EventJson::from)
.expect("AnyRoomEvent can always be built from a full PDU event")
} }
pub fn to_state_event(&self) -> EventJson<AnyStateEvent> { pub fn to_state_event(&self) -> EventJson<AnyStateEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid"); let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<AnyStateEvent>>(&json) serde_json::from_str::<AnyStateEvent>(&json)
.expect("EventJson::from_str always works") .map(EventJson::from)
.expect("AnyStateEvent can always be built from a full PDU event")
} }
pub fn to_state_event_stub(&self) -> EventJson<AnySyncStateEvent> { pub fn to_sync_state_event(&self) -> EventJson<AnySyncStateEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid"); let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<AnySyncStateEvent>>(&json) serde_json::from_str::<AnySyncStateEvent>(&json)
.expect("EventJson::from_str always works") .map(EventJson::from)
.expect("AnySyncStateEvent can always be built from a full PDU event")
} }
pub fn to_stripped_state_event(&self) -> EventJson<AnyStrippedStateEvent> { pub fn to_stripped_state_event(&self) -> EventJson<AnyStrippedStateEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid"); let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<AnyStrippedStateEvent>>(&json) serde_json::from_str::<AnyStrippedStateEvent>(&json)
.expect("EventJson::from_str always works") .map(EventJson::from)
.expect("AnyStrippedStateEvent can always be built from a full PDU event")
} }
pub fn to_member_event(&self) -> EventJson<StateEvent<MemberEventContent>> { pub fn to_member_event(&self) -> EventJson<StateEvent<MemberEventContent>> {
let json = serde_json::to_string(&self).expect("PDUs are always valid"); let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<StateEvent<MemberEventContent>>>(&json) serde_json::from_str::<StateEvent<MemberEventContent>>(&json)
.expect("EventJson::from_str always works") .map(EventJson::from)
.expect("StateEvent<MemberEventContent> can always be built from a full PDU event")
} }
} }