From 297bae4cbb8610aa41f12d7d495dbc2e8d5f36e1 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 29 Sep 2019 10:56:15 +0200 Subject: [PATCH] Remove FromStr, TryFrom<&'_ str> implementations --- src/collections/all.rs | 804 ---------------------------------- src/collections/only.rs | 337 -------------- src/ignored_user_list.rs | 74 +--- src/key/verification/start.rs | 120 ----- src/room/canonical_alias.rs | 81 ---- src/room/encrypted.rs | 97 ---- src/room/message.rs | 109 ----- src/room/name.rs | 81 ---- src/room/power_levels.rs | 110 +---- src/room/server_acl.rs | 89 ---- src/stripped.rs | 179 -------- 11 files changed, 2 insertions(+), 2079 deletions(-) diff --git a/src/collections/all.rs b/src/collections/all.rs index c26d3e06..65125111 100644 --- a/src/collections/all.rs +++ b/src/collections/all.rs @@ -1,8 +1,6 @@ //! Enums for heterogeneous collections of events, inclusive for every event type that implements //! the trait of the same name. -use std::str::FromStr; - use serde::{Serialize, Serializer}; use serde_json::{from_value, Value}; @@ -392,373 +390,6 @@ impl Serialize for Event { } } -impl FromStr for Event { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - #[allow(clippy::cognitive_complexity)] - fn from_str(json: &str) -> Result { - let value: Value = serde_json::from_str(json)?; - - let event_type_value = match value.get("type") { - Some(value) => value.clone(), - None => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: "missing field `type`".to_string(), - })) - } - }; - - let event_type = match from_value::(event_type_value.clone()) { - Ok(event_type) => event_type, - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })) - } - }; - - match event_type { - EventType::CallAnswer => match json.parse() { - Ok(event) => Ok(Event::CallAnswer(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::CallCandidates => match json.parse() { - Ok(event) => Ok(Event::CallCandidates(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::CallHangup => match json.parse() { - Ok(event) => Ok(Event::CallHangup(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::CallInvite => match json.parse() { - Ok(event) => Ok(Event::CallInvite(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Direct => match json.parse() { - Ok(event) => Ok(Event::Direct(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Dummy => match json.parse() { - Ok(event) => Ok(Event::Dummy(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::ForwardedRoomKey => match json.parse() { - Ok(event) => Ok(Event::ForwardedRoomKey(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::FullyRead => match json.parse() { - Ok(event) => Ok(Event::FullyRead(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::KeyVerificationAccept => match json.parse() { - Ok(event) => Ok(Event::KeyVerificationAccept(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::KeyVerificationCancel => match json.parse() { - Ok(event) => Ok(Event::KeyVerificationCancel(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::KeyVerificationKey => match json.parse() { - Ok(event) => Ok(Event::KeyVerificationKey(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::KeyVerificationMac => match json.parse() { - Ok(event) => Ok(Event::KeyVerificationMac(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::KeyVerificationRequest => match json.parse() { - Ok(event) => Ok(Event::KeyVerificationRequest(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::KeyVerificationStart => match json.parse() { - Ok(event) => Ok(Event::KeyVerificationStart(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::IgnoredUserList => match json.parse() { - Ok(event) => Ok(Event::IgnoredUserList(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Presence => match json.parse() { - Ok(event) => Ok(Event::Presence(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::PushRules => match json.parse() { - Ok(event) => Ok(Event::PushRules(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Receipt => match json.parse() { - Ok(event) => Ok(Event::Receipt(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomAliases => match json.parse() { - Ok(event) => Ok(Event::RoomAliases(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomAvatar => match json.parse() { - Ok(event) => Ok(Event::RoomAvatar(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomCanonicalAlias => match json.parse() { - Ok(event) => Ok(Event::RoomCanonicalAlias(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomCreate => match json.parse() { - Ok(event) => Ok(Event::RoomCreate(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomEncrypted => match json.parse() { - Ok(event) => Ok(Event::RoomEncrypted(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomEncryption => match json.parse() { - Ok(event) => Ok(Event::RoomEncryption(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomGuestAccess => match json.parse() { - Ok(event) => Ok(Event::RoomGuestAccess(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomHistoryVisibility => match json.parse() { - Ok(event) => Ok(Event::RoomHistoryVisibility(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomJoinRules => match json.parse() { - Ok(event) => Ok(Event::RoomJoinRules(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomMember => match json.parse() { - Ok(event) => Ok(Event::RoomMember(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomMessage => match json.parse() { - Ok(event) => Ok(Event::RoomMessage(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomMessageFeedback => match json.parse() { - Ok(event) => Ok(Event::RoomMessageFeedback(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomName => match json.parse() { - Ok(event) => Ok(Event::RoomName(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomPinnedEvents => match json.parse() { - Ok(event) => Ok(Event::RoomPinnedEvents(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomPowerLevels => match json.parse() { - Ok(event) => Ok(Event::RoomPowerLevels(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomRedaction => match json.parse() { - Ok(event) => Ok(Event::RoomRedaction(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomServerAcl => match json.parse() { - Ok(event) => Ok(Event::RoomServerAcl(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomThirdPartyInvite => match json.parse() { - Ok(event) => Ok(Event::RoomThirdPartyInvite(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomTombstone => match json.parse() { - Ok(event) => Ok(Event::RoomTombstone(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomTopic => match json.parse() { - Ok(event) => Ok(Event::RoomTopic(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomKey => match json.parse() { - Ok(event) => Ok(Event::RoomKey(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomKeyRequest => match json.parse() { - Ok(event) => Ok(Event::RoomKeyRequest(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Sticker => match json.parse() { - Ok(event) => Ok(Event::Sticker(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Tag => match json.parse() { - Ok(event) => Ok(Event::Tag(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Typing => match json.parse() { - Ok(event) => Ok(Event::Typing(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Custom(_) => { - if value.get("state_key").is_some() { - match json.parse() { - Ok(event) => Ok(Event::CustomState(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - } - } else if value.get("event_id").is_some() - && value.get("room_id").is_some() - && value.get("sender").is_some() - { - match json.parse() { - Ok(event) => Ok(Event::CustomRoom(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - } - } else { - match json.parse() { - Ok(event) => Ok(Event::Custom(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - } - } - } - EventType::__Nonexhaustive => { - panic!("__Nonexhaustive enum variant is not intended for use.") - } - } - } -} - impl Serialize for RoomEvent { fn serialize(&self, serializer: S) -> Result where @@ -796,257 +427,6 @@ impl Serialize for RoomEvent { } } -impl FromStr for RoomEvent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - #[allow(clippy::cognitive_complexity)] - fn from_str(json: &str) -> Result { - let value: Value = serde_json::from_str(json)?; - - let event_type_value = match value.get("type") { - Some(value) => value.clone(), - None => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: "missing field `type`".to_string(), - })) - } - }; - - let event_type = match from_value::(event_type_value.clone()) { - Ok(event_type) => event_type, - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })) - } - }; - - match event_type { - EventType::CallAnswer => match json.parse() { - Ok(event) => Ok(RoomEvent::CallAnswer(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::CallCandidates => match json.parse() { - Ok(event) => Ok(RoomEvent::CallCandidates(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::CallHangup => match json.parse() { - Ok(event) => Ok(RoomEvent::CallHangup(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::CallInvite => match json.parse() { - Ok(event) => Ok(RoomEvent::CallInvite(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomAliases => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomAliases(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomAvatar => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomAvatar(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomCanonicalAlias => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomCanonicalAlias(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomCreate => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomCreate(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomEncrypted => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomEncrypted(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomEncryption => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomEncryption(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomGuestAccess => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomGuestAccess(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomHistoryVisibility => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomHistoryVisibility(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomJoinRules => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomJoinRules(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomMember => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomMember(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomMessage => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomMessage(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomMessageFeedback => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomMessageFeedback(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomName => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomName(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomPinnedEvents => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomPinnedEvents(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomPowerLevels => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomPowerLevels(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomRedaction => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomRedaction(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomServerAcl => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomServerAcl(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomThirdPartyInvite => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomThirdPartyInvite(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomTombstone => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomTombstone(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomTopic => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomTopic(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Sticker => match json.parse() { - Ok(event) => Ok(RoomEvent::Sticker(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Custom(_) => { - if value.get("state_key").is_some() { - match json.parse() { - Ok(event) => Ok(RoomEvent::CustomState(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - } - } else { - match json.parse() { - Ok(event) => Ok(RoomEvent::CustomRoom(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - } - } - } - EventType::Direct - | EventType::Dummy - | EventType::ForwardedRoomKey - | EventType::FullyRead - | EventType::KeyVerificationAccept - | EventType::KeyVerificationCancel - | EventType::KeyVerificationKey - | EventType::KeyVerificationMac - | EventType::KeyVerificationRequest - | EventType::KeyVerificationStart - | EventType::IgnoredUserList - | EventType::Presence - | EventType::PushRules - | EventType::Receipt - | EventType::RoomKey - | EventType::RoomKeyRequest - | EventType::Tag - | EventType::Typing => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: "not a room event".to_string(), - })), - EventType::__Nonexhaustive => { - panic!("__Nonexhaustive enum variant is not intended for use.") - } - } - } -} - impl Serialize for StateEvent { fn serialize(&self, serializer: S) -> Result where @@ -1074,190 +454,6 @@ impl Serialize for StateEvent { } } -impl FromStr for StateEvent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let value: Value = serde_json::from_str(json)?; - - let event_type_value = match value.get("type") { - Some(value) => value.clone(), - None => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: "missing field `type`".to_string(), - })) - } - }; - - let event_type = match from_value::(event_type_value.clone()) { - Ok(event_type) => event_type, - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })) - } - }; - - match event_type { - EventType::RoomAliases => match json.parse() { - Ok(event) => Ok(StateEvent::RoomAliases(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomAvatar => match json.parse() { - Ok(event) => Ok(StateEvent::RoomAvatar(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomCanonicalAlias => match json.parse() { - Ok(event) => Ok(StateEvent::RoomCanonicalAlias(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomCreate => match json.parse() { - Ok(event) => Ok(StateEvent::RoomCreate(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomEncryption => match json.parse() { - Ok(event) => Ok(StateEvent::RoomEncryption(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomGuestAccess => match json.parse() { - Ok(event) => Ok(StateEvent::RoomGuestAccess(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomHistoryVisibility => match json.parse() { - Ok(event) => Ok(StateEvent::RoomHistoryVisibility(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomJoinRules => match json.parse() { - Ok(event) => Ok(StateEvent::RoomJoinRules(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomMember => match json.parse() { - Ok(event) => Ok(StateEvent::RoomMember(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomName => match json.parse() { - Ok(event) => Ok(StateEvent::RoomName(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomPinnedEvents => match json.parse() { - Ok(event) => Ok(StateEvent::RoomPinnedEvents(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomPowerLevels => match json.parse() { - Ok(event) => Ok(StateEvent::RoomPowerLevels(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomServerAcl => match json.parse() { - Ok(event) => Ok(StateEvent::RoomServerAcl(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomThirdPartyInvite => match json.parse() { - Ok(event) => Ok(StateEvent::RoomThirdPartyInvite(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomTombstone => match json.parse() { - Ok(event) => Ok(StateEvent::RoomTombstone(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomTopic => match json.parse() { - Ok(event) => Ok(StateEvent::RoomTopic(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Custom(_) => match json.parse() { - Ok(event) => Ok(StateEvent::CustomState(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::CallAnswer - | EventType::CallCandidates - | EventType::CallHangup - | EventType::CallInvite - | EventType::Direct - | EventType::Dummy - | EventType::ForwardedRoomKey - | EventType::FullyRead - | EventType::KeyVerificationAccept - | EventType::KeyVerificationCancel - | EventType::KeyVerificationKey - | EventType::KeyVerificationMac - | EventType::KeyVerificationRequest - | EventType::KeyVerificationStart - | EventType::IgnoredUserList - | EventType::Presence - | EventType::PushRules - | EventType::Receipt - | EventType::RoomEncrypted - | EventType::RoomMessage - | EventType::RoomMessageFeedback - | EventType::RoomRedaction - | EventType::RoomKey - | EventType::RoomKeyRequest - | EventType::Sticker - | EventType::Tag - | EventType::Typing => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: "not a state event".to_string(), - })), - EventType::__Nonexhaustive => { - panic!("__Nonexhaustive enum variant is not intended for use.") - } - } - } -} - macro_rules! impl_from_t_for_event { ($ty:ty, $variant:ident) => { impl From<$ty> for Event { diff --git a/src/collections/only.rs b/src/collections/only.rs index b9a39355..f32666e1 100644 --- a/src/collections/only.rs +++ b/src/collections/only.rs @@ -162,202 +162,6 @@ impl Serialize for Event { } } -impl FromStr for Event { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let value: Value = serde_json::from_str(json)?; - - let event_type_value = match value.get("type") { - Some(value) => value.clone(), - None => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: "missing field `type`".to_string(), - })) - } - }; - - let event_type = match from_value::(event_type_value.clone()) { - Ok(event_type) => event_type, - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })) - } - }; - - match event_type { - EventType::Direct => match json.parse() { - Ok(event) => Ok(Event::Direct(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Dummy => match json.parse() { - Ok(event) => Ok(Event::Dummy(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::ForwardedRoomKey => match json.parse() { - Ok(event) => Ok(Event::ForwardedRoomKey(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::FullyRead => match json.parse() { - Ok(event) => Ok(Event::FullyRead(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::KeyVerificationAccept => match json.parse() { - Ok(event) => Ok(Event::KeyVerificationAccept(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::KeyVerificationCancel => match json.parse() { - Ok(event) => Ok(Event::KeyVerificationCancel(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::KeyVerificationKey => match json.parse() { - Ok(event) => Ok(Event::KeyVerificationKey(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::KeyVerificationMac => match json.parse() { - Ok(event) => Ok(Event::KeyVerificationMac(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::KeyVerificationRequest => match json.parse() { - Ok(event) => Ok(Event::KeyVerificationRequest(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::KeyVerificationStart => match json.parse() { - Ok(event) => Ok(Event::KeyVerificationStart(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::IgnoredUserList => match json.parse() { - Ok(event) => Ok(Event::IgnoredUserList(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Presence => match json.parse() { - Ok(event) => Ok(Event::Presence(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::PushRules => match json.parse() { - Ok(event) => Ok(Event::PushRules(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Receipt => match json.parse() { - Ok(event) => Ok(Event::Receipt(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomKey => match json.parse() { - Ok(event) => Ok(Event::RoomKey(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomKeyRequest => match json.parse() { - Ok(event) => Ok(Event::RoomKeyRequest(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Tag => match json.parse() { - Ok(event) => Ok(Event::Tag(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Typing => match json.parse() { - Ok(event) => Ok(Event::Typing(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Custom(_) => match json.parse() { - Ok(event) => Ok(Event::Custom(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::CallAnswer - | EventType::CallCandidates - | EventType::CallHangup - | EventType::CallInvite - | EventType::RoomAliases - | EventType::RoomAvatar - | EventType::RoomCanonicalAlias - | EventType::RoomCreate - | EventType::RoomEncrypted - | EventType::RoomEncryption - | EventType::RoomGuestAccess - | EventType::RoomHistoryVisibility - | EventType::RoomJoinRules - | EventType::RoomMember - | EventType::RoomMessage - | EventType::RoomMessageFeedback - | EventType::RoomName - | EventType::RoomPinnedEvents - | EventType::RoomPowerLevels - | EventType::RoomRedaction - | EventType::RoomServerAcl - | EventType::RoomThirdPartyInvite - | EventType::RoomTombstone - | EventType::RoomTopic - | EventType::Sticker => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: "not exclusively a basic event".to_string(), - })), - EventType::__Nonexhaustive => { - panic!("__Nonexhaustive enum variant is not intended for use.") - } - } - } -} - impl Serialize for RoomEvent { fn serialize(&self, serializer: S) -> Result where @@ -378,147 +182,6 @@ impl Serialize for RoomEvent { } } -impl FromStr for RoomEvent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let value: Value = serde_json::from_str(json)?; - - let event_type_value = match value.get("type") { - Some(value) => value.clone(), - None => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: "missing field `type`".to_string(), - })) - } - }; - - let event_type = match from_value::(event_type_value.clone()) { - Ok(event_type) => event_type, - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })) - } - }; - - match event_type { - EventType::CallAnswer => match json.parse() { - Ok(event) => Ok(RoomEvent::CallAnswer(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::CallCandidates => match json.parse() { - Ok(event) => Ok(RoomEvent::CallCandidates(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::CallHangup => match json.parse() { - Ok(event) => Ok(RoomEvent::CallHangup(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::CallInvite => match json.parse() { - Ok(event) => Ok(RoomEvent::CallInvite(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomEncrypted => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomEncrypted(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomMessage => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomMessage(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomMessageFeedback => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomMessageFeedback(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::RoomRedaction => match json.parse() { - Ok(event) => Ok(RoomEvent::RoomRedaction(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Sticker => match json.parse() { - Ok(event) => Ok(RoomEvent::Sticker(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Custom(_) => match json.parse() { - Ok(event) => Ok(RoomEvent::CustomRoom(event)), - Err(error) => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })), - }, - EventType::Direct - | EventType::Dummy - | EventType::ForwardedRoomKey - | EventType::FullyRead - | EventType::KeyVerificationAccept - | EventType::KeyVerificationCancel - | EventType::KeyVerificationKey - | EventType::KeyVerificationMac - | EventType::KeyVerificationRequest - | EventType::KeyVerificationStart - | EventType::IgnoredUserList - | EventType::Presence - | EventType::PushRules - | EventType::Receipt - | EventType::RoomAliases - | EventType::RoomAvatar - | EventType::RoomCanonicalAlias - | EventType::RoomCreate - | EventType::RoomEncryption - | EventType::RoomGuestAccess - | EventType::RoomHistoryVisibility - | EventType::RoomJoinRules - | EventType::RoomMember - | EventType::RoomName - | EventType::RoomPinnedEvents - | EventType::RoomPowerLevels - | EventType::RoomServerAcl - | EventType::RoomThirdPartyInvite - | EventType::RoomTombstone - | EventType::RoomTopic - | EventType::RoomKey - | EventType::RoomKeyRequest - | EventType::Tag - | EventType::Typing => Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: "not exclusively a room event".to_string(), - })), - EventType::__Nonexhaustive => { - panic!("__Nonexhaustive enum variant is not intended for use.") - } - } - } -} macro_rules! impl_from_t_for_event { ($ty:ty, $variant:ident) => { impl From<$ty> for Event { diff --git a/src/ignored_user_list.rs b/src/ignored_user_list.rs index 9da415ed..822fbbab 100644 --- a/src/ignored_user_list.rs +++ b/src/ignored_user_list.rs @@ -1,6 +1,6 @@ //! Types for the *m.ignored_user_list* event. -use std::{collections::HashMap, convert::TryFrom, str::FromStr}; +use std::collections::HashMap; use ruma_identifiers::UserId; use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer}; @@ -48,43 +48,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for IgnoredUserListEvent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - Ok(Self { - content: IgnoredUserListEventContent { - ignored_users: raw.content.ignored_users.keys().cloned().collect(), - }, - }) - } -} - -impl<'a> TryFrom<&'a str> for IgnoredUserListEvent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - impl Serialize for IgnoredUserListEvent { fn serialize(&self, serializer: S) -> Result where @@ -130,41 +93,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for IgnoredUserListEventContent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - Ok(Self { - ignored_users: raw.ignored_users.keys().cloned().collect(), - }) - } -} - -impl<'a> TryFrom<&'a str> for IgnoredUserListEventContent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - impl Serialize for IgnoredUserListEventContent { fn serialize(&self, serializer: S) -> Result where diff --git a/src/key/verification/start.rs b/src/key/verification/start.rs index f7c4c019..b90cf36f 100644 --- a/src/key/verification/start.rs +++ b/src/key/verification/start.rs @@ -1,7 +1,5 @@ //! Types for the *m.key.verification.start* event. -use std::{convert::TryFrom, str::FromStr}; - use ruma_identifiers::DeviceId; use serde::{de::Error, ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer}; use serde_json::{from_value, Value}; @@ -68,46 +66,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for StartEvent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - let content = match raw.content { - raw::StartEventContent::MSasV1(content) => StartEventContent::MSasV1(content), - raw::StartEventContent::__Nonexhaustive => { - panic!("__Nonexhaustive enum variant is not intended for use."); - } - }; - - Ok(Self { content }) - } -} - -impl<'a> TryFrom<&'a str> for StartEvent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - impl Serialize for StartEvent { fn serialize(&self, serializer: S) -> Result where @@ -197,84 +155,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for StartEventContent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - match raw { - raw::StartEventContent::MSasV1(content) => { - if !content - .key_agreement_protocols - .contains(&KeyAgreementProtocol::Curve25519) - { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: serde_json::from_str::(json)?, - message: "`key_agreement_protocols` must contain at least `KeyAgreementProtocol::Curve25519`".to_string(), - })); - } - - if !content.hashes.contains(&HashAlgorithm::Sha256) { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: serde_json::from_str::(json)?, - message: "`hashes` must contain at least `HashAlgorithm::Sha256`" - .to_string(), - })); - } - - if !content - .message_authentication_codes - .contains(&MessageAuthenticationCode::HkdfHmacSha256) - { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: serde_json::from_str::(json)?, - message: "`message_authentication_codes` must contain at least `MessageAuthenticationCode::HkdfHmacSha256`".to_string(), - })); - } - - if !content - .short_authentication_string - .contains(&ShortAuthenticationString::Decimal) - { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: serde_json::from_str::(json)?, - message: "`short_authentication_string` must contain at least `ShortAuthenticationString::Decimal`".to_string(), - })); - } - - Ok(StartEventContent::MSasV1(content)) - } - raw::StartEventContent::__Nonexhaustive => { - panic!("__Nonexhaustive enum variant is not intended for use."); - } - } - } -} - -impl<'a> TryFrom<&'a str> for StartEventContent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - impl Serialize for StartEventContent { fn serialize(&self, serializer: S) -> Result where diff --git a/src/room/canonical_alias.rs b/src/room/canonical_alias.rs index b8c34ccb..690fbd89 100644 --- a/src/room/canonical_alias.rs +++ b/src/room/canonical_alias.rs @@ -1,7 +1,5 @@ //! Types for the *m.room.canonical_alias* event. -use std::{convert::TryFrom, str::FromStr}; - use js_int::UInt; use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId}; use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer}; @@ -86,52 +84,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for CanonicalAliasEvent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - Ok(Self { - content: CanonicalAliasEventContent { - alias: raw.content.alias, - }, - event_id: raw.event_id, - origin_server_ts: raw.origin_server_ts, - prev_content: raw - .prev_content - .map(|prev| CanonicalAliasEventContent { alias: prev.alias }), - room_id: raw.room_id, - sender: raw.sender, - state_key: raw.state_key, - unsigned: raw.unsigned, - }) - } -} - -impl<'a> TryFrom<&'a str> for CanonicalAliasEvent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - impl Serialize for CanonicalAliasEvent { fn serialize(&self, serializer: S) -> Result where @@ -208,39 +160,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for CanonicalAliasEventContent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - Ok(Self { alias: raw.alias }) - } -} - -impl<'a> TryFrom<&'a str> for CanonicalAliasEventContent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - mod raw { use super::*; diff --git a/src/room/encrypted.rs b/src/room/encrypted.rs index 88ff077b..b17c0d60 100644 --- a/src/room/encrypted.rs +++ b/src/room/encrypted.rs @@ -1,7 +1,5 @@ //! Types for the *m.room.encrypted* event. -use std::{convert::TryFrom, str::FromStr}; - use js_int::UInt; use ruma_identifiers::{DeviceId, EventId, RoomId, UserId}; use serde::{de::Error, ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer}; @@ -92,58 +90,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for EncryptedEvent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - let content = match raw.content { - raw::EncryptedEventContent::OlmV1Curve25519AesSha2(content) => { - EncryptedEventContent::OlmV1Curve25519AesSha2(content) - } - raw::EncryptedEventContent::MegolmV1AesSha2(content) => { - EncryptedEventContent::MegolmV1AesSha2(content) - } - raw::EncryptedEventContent::__Nonexhaustive => { - panic!("__Nonexhaustive enum variant is not intended for use."); - } - }; - - Ok(Self { - content, - event_id: raw.event_id, - origin_server_ts: raw.origin_server_ts, - room_id: raw.room_id, - sender: raw.sender, - unsigned: raw.unsigned, - }) - } -} - -impl<'a> TryFrom<&'a str> for EncryptedEvent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - impl Serialize for EncryptedEvent { fn serialize(&self, serializer: S) -> Result where @@ -219,49 +165,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for EncryptedEventContent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - match raw { - raw::EncryptedEventContent::OlmV1Curve25519AesSha2(content) => { - Ok(EncryptedEventContent::OlmV1Curve25519AesSha2(content)) - } - raw::EncryptedEventContent::MegolmV1AesSha2(content) => { - Ok(EncryptedEventContent::MegolmV1AesSha2(content)) - } - raw::EncryptedEventContent::__Nonexhaustive => { - panic!("__Nonexhaustive enum variant is not intended for use."); - } - } - } -} - -impl<'a> TryFrom<&'a str> for EncryptedEventContent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - impl Serialize for EncryptedEventContent { fn serialize(&self, serializer: S) -> Result where diff --git a/src/room/message.rs b/src/room/message.rs index 357ebf20..85da80b1 100644 --- a/src/room/message.rs +++ b/src/room/message.rs @@ -1,7 +1,5 @@ //! Types for the *m.room.message* event. -use std::{convert::TryFrom, str::FromStr}; - use js_int::UInt; use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{ @@ -123,63 +121,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for MessageEvent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - Ok(Self { - content: match raw.content { - raw::MessageEventContent::Audio(content) => MessageEventContent::Audio(content), - raw::MessageEventContent::Emote(content) => MessageEventContent::Emote(content), - raw::MessageEventContent::File(content) => MessageEventContent::File(content), - raw::MessageEventContent::Image(content) => MessageEventContent::Image(content), - raw::MessageEventContent::Location(content) => { - MessageEventContent::Location(content) - } - raw::MessageEventContent::Notice(content) => MessageEventContent::Notice(content), - raw::MessageEventContent::ServerNotice(content) => { - MessageEventContent::ServerNotice(content) - } - raw::MessageEventContent::Text(content) => MessageEventContent::Text(content), - raw::MessageEventContent::Video(content) => MessageEventContent::Video(content), - raw::MessageEventContent::__Nonexhaustive => { - panic!("__Nonexhaustive enum variant is not intended for use.") - } - }, - event_id: raw.event_id, - origin_server_ts: raw.origin_server_ts, - room_id: raw.room_id, - sender: raw.sender, - unsigned: raw.unsigned, - }) - } -} - -impl<'a> TryFrom<&'a str> for MessageEvent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - impl Serialize for MessageEvent { fn serialize(&self, serializer: S) -> Result where @@ -280,56 +221,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for MessageEventContent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - match raw { - raw::MessageEventContent::Audio(content) => Ok(MessageEventContent::Audio(content)), - raw::MessageEventContent::Emote(content) => Ok(MessageEventContent::Emote(content)), - raw::MessageEventContent::File(content) => Ok(MessageEventContent::File(content)), - raw::MessageEventContent::Image(content) => Ok(MessageEventContent::Image(content)), - raw::MessageEventContent::Location(content) => { - Ok(MessageEventContent::Location(content)) - } - raw::MessageEventContent::Notice(content) => Ok(MessageEventContent::Notice(content)), - raw::MessageEventContent::ServerNotice(content) => { - Ok(MessageEventContent::ServerNotice(content)) - } - raw::MessageEventContent::Text(content) => Ok(MessageEventContent::Text(content)), - raw::MessageEventContent::Video(content) => Ok(MessageEventContent::Video(content)), - raw::MessageEventContent::__Nonexhaustive => { - panic!("__Nonexhaustive enum variant is not intended for use.") - } - } - } -} - -impl<'a> TryFrom<&'a str> for MessageEventContent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - mod raw { use super::*; diff --git a/src/room/name.rs b/src/room/name.rs index 96f27fa8..28ce50a5 100644 --- a/src/room/name.rs +++ b/src/room/name.rs @@ -1,7 +1,5 @@ //! Types for the *m.room.name* event. -use std::{convert::TryFrom, str::FromStr}; - use js_int::UInt; use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer}; @@ -84,52 +82,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for NameEvent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - Ok(Self { - content: NameEventContent { - name: raw.content.name, - }, - event_id: raw.event_id, - origin_server_ts: raw.origin_server_ts, - prev_content: raw - .prev_content - .map(|prev| NameEventContent { name: prev.name }), - room_id: raw.room_id, - sender: raw.sender, - state_key: raw.state_key, - unsigned: raw.unsigned, - }) - } -} - -impl<'a> TryFrom<&'a str> for NameEvent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - impl Serialize for NameEvent { fn serialize(&self, serializer: S) -> Result where @@ -222,39 +174,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for NameEventContent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - Ok(Self { name: raw.name }) - } -} - -impl<'a> TryFrom<&'a str> for NameEventContent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - mod raw { use super::*; diff --git a/src/room/power_levels.rs b/src/room/power_levels.rs index 582edeb8..f374ffdf 100644 --- a/src/room/power_levels.rs +++ b/src/room/power_levels.rs @@ -1,6 +1,6 @@ //! Types for the *m.room.power_levels* event. -use std::{collections::HashMap, convert::TryFrom, str::FromStr}; +use std::collections::HashMap; use js_int::{Int, UInt}; use ruma_identifiers::{EventId, RoomId, UserId}; @@ -141,70 +141,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for PowerLevelsEvent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - Ok(Self { - content: PowerLevelsEventContent { - ban: raw.content.ban, - events: raw.content.events, - events_default: raw.content.events_default, - invite: raw.content.invite, - kick: raw.content.kick, - redact: raw.content.redact, - state_default: raw.content.state_default, - users: raw.content.users, - users_default: raw.content.users_default, - notifications: raw.content.notifications, - }, - event_id: raw.event_id, - origin_server_ts: raw.origin_server_ts, - prev_content: raw.prev_content.map(|prev| PowerLevelsEventContent { - ban: prev.ban, - events: prev.events, - events_default: prev.events_default, - invite: prev.invite, - kick: prev.kick, - redact: prev.redact, - state_default: prev.state_default, - users: prev.users, - users_default: prev.users_default, - notifications: prev.notifications, - }), - room_id: raw.room_id, - sender: raw.sender, - state_key: raw.state_key, - unsigned: raw.unsigned, - }) - } -} - -impl<'a> TryFrom<&'a str> for PowerLevelsEvent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - impl Serialize for PowerLevelsEvent { fn serialize(&self, serializer: S) -> Result where @@ -290,50 +226,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for PowerLevelsEventContent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - Ok(Self { - ban: raw.ban, - events: raw.events, - events_default: raw.events_default, - invite: raw.invite, - kick: raw.kick, - redact: raw.redact, - state_default: raw.state_default, - users: raw.users, - users_default: raw.users_default, - notifications: raw.notifications, - }) - } -} - -impl<'a> TryFrom<&'a str> for PowerLevelsEventContent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - mod raw { use super::*; diff --git a/src/room/server_acl.rs b/src/room/server_acl.rs index 62c64ef5..34f82611 100644 --- a/src/room/server_acl.rs +++ b/src/room/server_acl.rs @@ -1,7 +1,5 @@ //! Types for the *m.room.server_acl* event. -use std::{convert::TryFrom, str::FromStr}; - use js_int::UInt; use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer}; @@ -110,56 +108,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for ServerAclEvent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - Ok(Self { - content: ServerAclEventContent { - allow_ip_literals: raw.content.allow_ip_literals, - allow: raw.content.allow, - deny: raw.content.deny, - }, - event_id: raw.event_id, - origin_server_ts: raw.origin_server_ts, - prev_content: raw.prev_content.map(|prev| ServerAclEventContent { - allow_ip_literals: prev.allow_ip_literals, - allow: prev.allow, - deny: prev.deny, - }), - room_id: raw.room_id, - unsigned: raw.unsigned, - sender: raw.sender, - state_key: raw.state_key, - }) - } -} - -impl<'a> TryFrom<&'a str> for ServerAclEvent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - impl Serialize for ServerAclEvent { fn serialize(&self, serializer: S) -> Result where @@ -207,43 +155,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -impl FromStr for ServerAclEventContent { - type Err = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn from_str(json: &str) -> Result { - let raw = match serde_json::from_str::(json) { - Ok(raw) => raw, - Err(error) => match serde_json::from_str::(json) { - Ok(value) => { - return Err(InvalidEvent(InnerInvalidEvent::Validation { - json: value, - message: error.to_string(), - })); - } - Err(error) => { - return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); - } - }, - }; - - Ok(Self { - allow_ip_literals: raw.allow_ip_literals, - allow: raw.allow, - deny: raw.deny, - }) - } -} - -impl<'a> TryFrom<&'a str> for ServerAclEventContent { - type Error = InvalidEvent; - - /// Attempt to create `Self` from parsing a string of JSON data. - fn try_from(json: &'a str) -> Result { - FromStr::from_str(json) - } -} - mod raw { use super::*; diff --git a/src/stripped.rs b/src/stripped.rs index 5e9851c2..dcfadb28 100644 --- a/src/stripped.rs +++ b/src/stripped.rs @@ -305,80 +305,6 @@ impl<'de> Deserialize<'de> for EventResult { } } -// impl FromStr for StrippedState { -// type Err = InvalidEvent; - -// /// Attempt to create `Self` from parsing a string of JSON data. -// fn from_str(json: &str) -> Result { -// let value = match serde_json::from_str::(json) { -// Ok(value) => value, -// Err(error) => match serde_json::from_str::(json) { -// Ok(value) => { -// return Err(InvalidEvent(InnerInvalidEvent::Validation { -// json: value, -// message: error.to_string(), -// })); -// } -// Err(error) => { -// return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); -// } -// }, -// }; - -// let event_type_value = match value.get("type") { -// Some(value) => value.clone(), -// None => { -// return Err(InvalidEvent(InnerInvalidEvent::Validation { -// json: value, -// message: "missing field `type`".to_string(), -// })) -// } -// }; - -// let event_type = match from_value::(event_type_value.clone()) { -// Ok(event_type) => event_type, -// Err(error) => { -// return Err(InvalidEvent(InnerInvalidEvent::Validation { -// json: value, -// message: error.to_string(), -// })) -// } -// }; - -// match event_type { -// EventType::RoomAliases => Ok(StrippedState::RoomAliases(json.parse()?)), -// EventType::RoomAvatar => Ok(StrippedState::RoomAvatar(json.parse()?)), -// EventType::RoomCanonicalAlias => Ok(StrippedState::RoomCanonicalAlias(json.parse()?)), -// EventType::RoomCreate => Ok(StrippedState::RoomCreate(json.parse()?)), -// EventType::RoomGuestAccess => Ok(StrippedState::RoomGuestAccess(json.parse()?)), -// EventType::RoomHistoryVisibility => { -// Ok(StrippedState::RoomHistoryVisibility(json.parse()?)) -// } -// EventType::RoomJoinRules => Ok(StrippedState::RoomJoinRules(json.parse()?)), -// EventType::RoomMember => Ok(StrippedState::RoomMember(json.parse()?)), -// EventType::RoomName => Ok(StrippedState::RoomName(json.parse()?)), -// EventType::RoomPowerLevels => Ok(StrippedState::RoomPowerLevels(json.parse()?)), -// EventType::RoomThirdPartyInvite => { -// Ok(StrippedState::RoomThirdPartyInvite(json.parse()?)) -// } -// EventType::RoomTopic => Ok(StrippedState::RoomTopic(json.parse()?)), -// _ => Err(InvalidEvent(InnerInvalidEvent::Validation { -// json: value, -// message: "not a state event".to_string(), -// })), -// } -// } -// } - -// impl<'a> TryFrom<&'a str> for StrippedState { -// type Error = InvalidEvent; - -// /// Attempt to create `Self` from parsing a string of JSON data. -// fn try_from(json: &'a str) -> Result { -// FromStr::from_str(json) -// } -// } - impl Serialize for StrippedState { fn serialize(&self, serializer: S) -> Result where @@ -461,111 +387,6 @@ impl Serialize for StrippedState { // } // } -// impl FromStr for StrippedStateContent -// where -// C: FromStr, -// ::Err: ToString, -// { -// type Err = InvalidEvent; - -// /// Attempt to create `Self` from parsing a string of JSON data. -// fn from_str(json: &str) -> Result { -// let value = match serde_json::from_str::(json) { -// Ok(value) => value, -// Err(error) => match serde_json::from_str::(json) { -// Ok(value) => { -// return Err(InvalidEvent(InnerInvalidEvent::Validation { -// json: value, -// message: error.to_string(), -// })); -// } -// Err(error) => { -// return Err(InvalidEvent(InnerInvalidEvent::Deserialization { error })); -// } -// }, -// }; - -// let event_type_value = match value.get("type") { -// Some(value) => value.clone(), -// None => { -// return Err(InvalidEvent(InnerInvalidEvent::Validation { -// json: value, -// message: "missing field `type`".to_string(), -// })) -// } -// }; - -// let event_type = match from_value::(event_type_value.clone()) { -// Ok(event_type) => event_type, -// Err(error) => { -// return Err(InvalidEvent(InnerInvalidEvent::Validation { -// json: value, -// message: error.to_string(), -// })) -// } -// }; - -// let content = match value.get("content") { -// Some(content_value) => match content_value.as_object() { -// Some(content) => content, -// None => { -// return Err(InvalidEvent(InnerInvalidEvent::Validation { -// json: value, -// message: "field `content` must be an object".to_string(), -// })) -// } -// }, -// None => { -// return Err(InvalidEvent(InnerInvalidEvent::Validation { -// json: value, -// message: "missing field `content`".to_string(), -// })) -// } -// }; - -// // Unwrap is safe because we already know this can deserialize to a `Value`. -// let json_string = to_string(content).unwrap(); - -// match event_type { -// EventType::RoomAliases => stripped_state_content(&json_string, event_type, value), -// EventType::RoomAvatar => stripped_state_content(&json_string, event_type, value), -// EventType::RoomCanonicalAlias => { -// stripped_state_content(&json_string, event_type, value) -// } -// EventType::RoomCreate => stripped_state_content(&json_string, event_type, value), -// EventType::RoomGuestAccess => stripped_state_content(&json_string, event_type, value), -// EventType::RoomHistoryVisibility => { -// stripped_state_content(&json_string, event_type, value) -// } -// EventType::RoomJoinRules => stripped_state_content(&json_string, event_type, value), -// EventType::RoomMember => stripped_state_content(&json_string, event_type, value), -// EventType::RoomName => stripped_state_content(&json_string, event_type, value), -// EventType::RoomPowerLevels => stripped_state_content(&json_string, event_type, value), -// EventType::RoomThirdPartyInvite => { -// stripped_state_content(&json_string, event_type, value) -// } -// EventType::RoomTopic => stripped_state_content(&json_string, event_type, value), -// _ => Err(InvalidEvent(InnerInvalidEvent::Validation { -// json: value, -// message: "not a state event".to_string(), -// })), -// } -// } -// } - -// impl<'a, C> TryFrom<&'a str> for StrippedStateContent -// where -// C: FromStr, -// ::Err: ToString, -// { -// type Error = InvalidEvent; - -// /// Attempt to create `Self` from parsing a string of JSON data. -// fn try_from(json: &'a str) -> Result { -// FromStr::from_str(json) -// } -// } - // /// Reduces the boilerplate in the match arms of `impl Deserialize for StrippedState`. // #[inline] // fn create_stripped_state(