From 9a993fa7c1597a56d289378ab3ad7f766d3e8a57 Mon Sep 17 00:00:00 2001 From: Devin R Date: Sat, 18 Jul 2020 08:22:30 -0400 Subject: [PATCH] Convert Pdu to a validated ruma type when calling to_*_event --- src/pdu.rs | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/pdu.rs b/src/pdu.rs index 403e699..f55acaf 100644 --- a/src/pdu.rs +++ b/src/pdu.rs @@ -79,34 +79,40 @@ impl PduEvent { Ok(()) } - pub fn to_room_event_stub(&self) -> EventJson { + pub fn to_sync_room_event(&self) -> EventJson { let json = serde_json::to_string(&self).expect("PDUs are always valid"); - serde_json::from_str::>(&json) - .expect("EventJson::from_str always works") + serde_json::from_str::(&json) + .map(EventJson::from) + .expect("AnySyncRoomEvent can always be built from a full PDU event") } pub fn to_room_event(&self) -> EventJson { let json = serde_json::to_string(&self).expect("PDUs are always valid"); - serde_json::from_str::>(&json) - .expect("EventJson::from_str always works") + serde_json::from_str::(&json) + .map(EventJson::from) + .expect("AnyRoomEvent can always be built from a full PDU event") } pub fn to_state_event(&self) -> EventJson { let json = serde_json::to_string(&self).expect("PDUs are always valid"); - serde_json::from_str::>(&json) - .expect("EventJson::from_str always works") + serde_json::from_str::(&json) + .map(EventJson::from) + .expect("AnyStateEvent can always be built from a full PDU event") } - pub fn to_state_event_stub(&self) -> EventJson { + pub fn to_sync_state_event(&self) -> EventJson { let json = serde_json::to_string(&self).expect("PDUs are always valid"); - serde_json::from_str::>(&json) - .expect("EventJson::from_str always works") + serde_json::from_str::(&json) + .map(EventJson::from) + .expect("AnySyncStateEvent can always be built from a full PDU event") } pub fn to_stripped_state_event(&self) -> EventJson { let json = serde_json::to_string(&self).expect("PDUs are always valid"); - serde_json::from_str::>(&json) - .expect("EventJson::from_str always works") + serde_json::from_str::(&json) + .map(EventJson::from) + .expect("AnyStrippedStateEvent can always be built from a full PDU event") } pub fn to_member_event(&self) -> EventJson> { let json = serde_json::to_string(&self).expect("PDUs are always valid"); - serde_json::from_str::>>(&json) - .expect("EventJson::from_str always works") + serde_json::from_str::>(&json) + .map(EventJson::from) + .expect("StateEvent can always be built from a full PDU event") } }