From eb3a3e21635c843b7f1d12fe894caa51a292a577 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 22 Apr 2020 21:24:39 +0200 Subject: [PATCH] Replace EventResult with EventJson, simplify InvalidEvent --- .travis.yml | 6 +- Cargo.toml | 2 +- README.md | 2 +- ruma-events-macros/src/lib.rs | 2 +- src/direct.rs | 6 +- src/dummy.rs | 6 +- src/ignored_user_list.rs | 6 +- src/json.rs | 112 ++++++++++++++++++++++++++++++++++ src/key/verification/start.rs | 41 ++++++------- src/lib.rs | 78 +++-------------------- src/presence.rs | 6 +- src/push_rules.rs | 6 +- src/room/canonical_alias.rs | 18 +++--- src/room/create.rs | 6 +- src/room/encrypted.rs | 14 ++--- src/room/message.rs | 16 +++-- src/room/name.rs | 26 ++++---- src/room/pinned_events.rs | 6 +- src/room/server_acl.rs | 6 +- src/stripped.rs | 18 +++--- src/to_device.rs | 6 +- src/util.rs | 6 +- 22 files changed, 220 insertions(+), 175 deletions(-) create mode 100644 src/json.rs diff --git a/.travis.yml b/.travis.yml index 2b507a3f..5808b3c5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: "rust" cache: "cargo" rust: - - 1.36.0 + - 1.38.0 - stable - beta - nightly @@ -13,7 +13,7 @@ jobs: before_script: - rustup component add rustfmt - | - if [ "$TRAVIS_RUST_VERSION" != "1.36.0" ]; then + if [ "$TRAVIS_RUST_VERSION" != "1.38.0" ]; then rustup component add clippy fi - | @@ -28,7 +28,7 @@ script: fi - cargo fmt --all -- --check - | - if [ "$TRAVIS_RUST_VERSION" != "1.36.0" ]; then + if [ "$TRAVIS_RUST_VERSION" != "1.38.0" ]; then cargo clippy --all --all-targets --all-features -- -D warnings fi - cargo build --all --verbose diff --git a/Cargo.toml b/Cargo.toml index 38403d5d..f1d0ee08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ ruma-events-macros = { path = "ruma-events-macros", version = "=0.20.0" } ruma-identifiers = "0.16.0" ruma-serde = "0.1.0" serde = { version = "1.0.106", features = ["derive"] } -serde_json = "1.0.51" +serde_json = { version = "1.0.51", features = ["raw_value"] } [dev-dependencies] maplit = "1.0.2" diff --git a/README.md b/README.md index d251eb72..d804a108 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ ## Minimum Rust version -ruma-events requires Rust 1.36.0 or later. +ruma-events requires Rust 1.38.0 or later. ## Documentation diff --git a/ruma-events-macros/src/lib.rs b/ruma-events-macros/src/lib.rs index 19159a1b..af0f71cf 100644 --- a/ruma-events-macros/src/lib.rs +++ b/ruma-events-macros/src/lib.rs @@ -125,7 +125,7 @@ mod parse; /// The event type and content type will have copies generated inside a private `raw` module. These /// "raw" versions are the same, except they implement `serde::Deserialize`. An implementation of /// `FromRaw` will be provided, which will allow the user to deserialize the event type as -/// `EventResult`. +/// `EventJson`. #[proc_macro] pub fn ruma_event(input: TokenStream) -> TokenStream { let ruma_event_input = syn::parse_macro_input!(input as RumaEventInput); diff --git a/src/direct.rs b/src/direct.rs index c1b7392e..a7104bb1 100644 --- a/src/direct.rs +++ b/src/direct.rs @@ -28,7 +28,7 @@ mod tests { use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::{DirectEvent, DirectEventContent}; - use crate::EventResult; + use crate::EventJson; #[test] fn serialization() { @@ -64,9 +64,9 @@ mod tests { "type": "m.direct" }); - let event: DirectEvent = from_json_value::>(json_data) + let event: DirectEvent = from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap(); let direct_rooms = event.content.get(&alice).unwrap(); diff --git a/src/dummy.rs b/src/dummy.rs index 48ad51dc..f192d2db 100644 --- a/src/dummy.rs +++ b/src/dummy.rs @@ -27,7 +27,7 @@ ruma_event! { #[cfg(test)] mod tests { use super::{DummyEvent, Empty}; - use crate::EventResult; + use crate::EventJson; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; @@ -51,9 +51,9 @@ mod tests { "type": "m.dummy" }); - assert!(from_json_value::>(json) + assert!(from_json_value::>(json) .unwrap() - .into_result() + .deserialize() .is_ok()); } } diff --git a/src/ignored_user_list.rs b/src/ignored_user_list.rs index 9cb00440..aa8438d6 100644 --- a/src/ignored_user_list.rs +++ b/src/ignored_user_list.rs @@ -74,7 +74,7 @@ mod tests { use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::{IgnoredUserListEvent, IgnoredUserListEventContent}; - use crate::EventResult; + use crate::EventJson; #[test] fn serialization() { @@ -107,9 +107,9 @@ mod tests { "type": "m.ignored_user_list" }); - let actual = from_json_value::>(json_data) + let actual = from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap(); let expected = IgnoredUserListEvent { diff --git a/src/json.rs b/src/json.rs new file mode 100644 index 00000000..bc750024 --- /dev/null +++ b/src/json.rs @@ -0,0 +1,112 @@ +use std::{ + clone::Clone, + fmt::{self, Debug, Formatter}, + marker::PhantomData, +}; + +use serde::{ + de::{Deserialize, Deserializer}, + ser::{Serialize, Serializer}, +}; +use serde_json::value::RawValue; + +use crate::{InvalidEvent, InvalidEventKind, TryFromRaw}; + +/// A wrapper around `Box`, to be used in place of event [content] [collection] types in +/// Matrix endpoint definition to allow request and response types to contain unknown events in +/// addition to the known event(s) represented by the generic argument `Ev`. +pub struct EventJson { + json: Box, + _ev: PhantomData, +} + +impl EventJson { + fn new(json: Box) -> Self { + Self { + json, + _ev: PhantomData, + } + } + + /// Access the underlying `RawValue`. + pub fn json(&self) -> &RawValue { + &self.json + } +} + +impl EventJson { + /// Try to deserialize the JSON into the expected event type. + pub fn deserialize(&self) -> Result { + let raw_ev: T::Raw = match serde_json::from_str(self.json.get()) { + Ok(raw) => raw, + Err(error) => { + return Err(InvalidEvent { + message: error.to_string(), + kind: InvalidEventKind::Deserialization, + }); + } + }; + + match T::try_from_raw(raw_ev) { + Ok(value) => Ok(value), + Err(err) => Err(InvalidEvent { + message: err.to_string(), + kind: InvalidEventKind::Validation, + }), + } + } +} + +impl From<&T> for EventJson { + fn from(val: &T) -> Self { + let json_string = serde_json::to_string(&val).unwrap(); + Self::new(RawValue::from_string(json_string).unwrap()) + } +} + +// Without the `TryFromRaw` bound, this would conflict with the next impl below +// We could remove the `TryFromRaw` bound once specialization is stabilized. +impl From for EventJson { + fn from(val: T) -> Self { + Self::from(&val) + } +} + +impl From> for EventJson { + fn from(json: Box) -> Self { + Self::new(json) + } +} + +impl Clone for EventJson { + fn clone(&self) -> Self { + Self::new(self.json.clone()) + } +} + +impl Debug for EventJson { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + use std::any::type_name; + f.debug_struct(&format!("EventJson::<{}>", type_name::())) + .field("json", &self.json) + .finish() + } +} + +impl<'de, T> Deserialize<'de> for EventJson { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + Box::::deserialize(deserializer).map(Self::new) + } +} + +impl Serialize for EventJson { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + self.json.serialize(serializer) + } +} diff --git a/src/key/verification/start.rs b/src/key/verification/start.rs index 3eecd86c..b5f14839 100644 --- a/src/key/verification/start.rs +++ b/src/key/verification/start.rs @@ -318,7 +318,7 @@ mod tests { HashAlgorithm, KeyAgreementProtocol, MSasV1Content, MSasV1ContentOptions, MessageAuthenticationCode, ShortAuthenticationString, StartEvent, StartEventContent, }; - use crate::EventResult; + use crate::EventJson; #[test] fn invalid_m_sas_v1_content_missing_required_key_agreement_protocols() { @@ -444,9 +444,9 @@ mod tests { // Deserialize the content struct separately to verify `TryFromRaw` is implemented for it. assert_eq!( - from_json_value::>(json_data) + from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap(), key_verification_start_content ); @@ -469,9 +469,9 @@ mod tests { }); assert_eq!( - from_json_value::>(json_data) + from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap(), key_verification_start ) @@ -480,17 +480,16 @@ mod tests { #[test] fn deserialization_failure() { // Ensure that invalid JSON creates a `serde_json::Error` and not `InvalidEvent` - assert!(serde_json::from_str::>("{").is_err()); + assert!(serde_json::from_str::>("{").is_err()); } #[test] fn deserialization_structure_mismatch() { // Missing several required fields. - let error = - from_json_value::>(json!({"from_device": "123"})) - .unwrap() - .into_result() - .unwrap_err(); + let error = from_json_value::>(json!({"from_device": "123"})) + .unwrap() + .deserialize() + .unwrap_err(); assert!(error.message().contains("missing field")); assert!(error.is_deserialization()); @@ -508,9 +507,9 @@ mod tests { "short_authentication_string": ["decimal"] }); - let error = from_json_value::>(json_data) + let error = from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap_err(); assert!(error.message().contains("key_agreement_protocols")); @@ -528,9 +527,9 @@ mod tests { "message_authentication_codes": ["hkdf-hmac-sha256"], "short_authentication_string": ["decimal"] }); - let error = from_json_value::>(json_data) + let error = from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap_err(); assert!(error.message().contains("hashes")); @@ -548,9 +547,9 @@ mod tests { "message_authentication_codes": [], "short_authentication_string": ["decimal"] }); - let error = from_json_value::>(json_data) + let error = from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap_err(); assert!(error.message().contains("message_authentication_codes")); @@ -568,9 +567,9 @@ mod tests { "message_authentication_codes": ["hkdf-hmac-sha256"], "short_authentication_string": [] }); - let error = from_json_value::>(json_data) + let error = from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap_err(); assert!(error.message().contains("short_authentication_string")); @@ -592,9 +591,9 @@ mod tests { }, "type": "m.key.verification.start" }); - let error = from_json_value::>(json_data) + let error = from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap_err(); assert!(error.message().contains("key_agreement_protocols")); diff --git a/src/lib.rs b/src/lib.rs index 72571a4f..a6c49aa3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,16 +73,16 @@ //! # Serialization and deserialization //! //! All concrete event types in ruma-events can be serialized via the `Serialize` trait from -//! [serde](https://serde.rs/) and can be deserialized from as `EventResult`. In order to +//! [serde](https://serde.rs/) and can be deserialized from as `EventJson`. In order to //! handle incoming data that may not conform to `ruma-events`' strict definitions of event -//! structures, deserialization will return `EventResult::Err` on error. This error covers both +//! structures, deserialization will return `EventJson::Err` on error. This error covers both //! structurally invalid JSON data as well as structurally valid JSON that doesn't fulfill //! additional constraints the matrix specification defines for some event types. The error exposes //! the deserialized `serde_json::Value` so that developers can still work with the received //! event data. This makes it possible to deserialize a collection of events without the entire //! collection failing to deserialize due to a single invalid event. The "content" type for each //! event also implements `Serialize` and either `TryFromRaw` (enabling usage as -//! `EventResult` for dedicated content types) or `Deserialize` (when the content is a +//! `EventJson` for dedicated content types) or `Deserialize` (when the content is a //! type alias), allowing content to be converted to and from JSON indepedently of the surrounding //! event structure, if needed. //! @@ -136,6 +136,7 @@ mod macros; mod algorithm; mod event_type; mod from_raw; +mod json; #[doc(hidden)] // only public for external tests pub mod util; @@ -176,19 +177,18 @@ pub use self::{ algorithm::Algorithm, event_type::EventType, from_raw::{FromRaw, TryFromRaw}, + json::EventJson, }; /// An event that is malformed or otherwise invalid. /// -/// When attempting to deserialize an [`EventResult`](enum.EventResult.html), an error in the input +/// When attempting to deserialize an [`EventJson`](enum.EventJson.html), an error in the input /// data may cause deserialization to fail, or the JSON structure may be correct, but additional /// constraints defined in the matrix specification are not upheld. This type provides an error -/// message and a `serde_json::Value` representation of the invalid event, as well as a flag for -/// which type of error was encountered. +/// message and a flag for which type of error was encountered. #[derive(Clone, Debug)] pub struct InvalidEvent { message: String, - json: Value, kind: InvalidEventKind, } @@ -204,11 +204,6 @@ impl InvalidEvent { self.message.clone() } - /// The `serde_json::Value` representation of the invalid event. - pub fn json(&self) -> &Value { - &self.json - } - /// Returns whether this is a deserialization error. pub fn is_deserialization(&self) -> bool { self.kind == InvalidEventKind::Deserialization @@ -243,65 +238,6 @@ impl Display for InvalidInput { impl Error for InvalidInput {} -/// The result of deserializing an event, which may or may not be valid. -/// -/// When data is successfully deserialized and validated, this structure will contain the -/// deserialized value `T`. When deserialization succeeds, but the event is invalid for any reason, -/// this structure will contain an [`InvalidEvent`](struct.InvalidEvent.html). See the documentation -/// for [`InvalidEvent`](struct.InvalidEvent.html) for more details. -#[derive(Clone, Debug)] -pub enum EventResult { - /// `T` deserialized and validated successfully. - Ok(T), - - /// `T` failed either deserialization or validation. - /// - /// [`InvalidEvent`](struct.InvalidEvent.html) contains the error message and the raw data. - Err(InvalidEvent), -} - -impl EventResult { - /// Convert `EventResult` into the equivalent `std::result::Result`. - pub fn into_result(self) -> Result { - match self { - EventResult::Ok(t) => Ok(t), - EventResult::Err(invalid_event) => Err(invalid_event), - } - } -} - -impl<'de, T> Deserialize<'de> for EventResult -where - T: TryFromRaw, -{ - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let json = serde_json::Value::deserialize(deserializer)?; - - let raw_data: T::Raw = match serde_json::from_value(json.clone()) { - Ok(raw) => raw, - Err(error) => { - return Ok(EventResult::Err(InvalidEvent { - json, - message: error.to_string(), - kind: InvalidEventKind::Deserialization, - })); - } - }; - - match T::try_from_raw(raw_data) { - Ok(value) => Ok(EventResult::Ok(value)), - Err(err) => Ok(EventResult::Err(InvalidEvent { - message: err.to_string(), - json, - kind: InvalidEventKind::Validation, - })), - } - } -} - /// An error when attempting to create a value from a string via the `FromStr` trait. #[derive(Clone, Copy, Eq, Debug, Hash, PartialEq)] pub struct FromStrError; diff --git a/src/presence.rs b/src/presence.rs index cb9cbf52..ed2e41f7 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -80,7 +80,7 @@ mod tests { use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::{PresenceEvent, PresenceEventContent, PresenceState}; - use crate::EventResult; + use crate::EventJson; #[test] fn serialization() { @@ -138,9 +138,9 @@ mod tests { }); assert_eq!( - from_json_value::>(json) + from_json_value::>(json) .unwrap() - .into_result() + .deserialize() .unwrap(), event ); diff --git a/src/push_rules.rs b/src/push_rules.rs index 90e8eb61..af9daf56 100644 --- a/src/push_rules.rs +++ b/src/push_rules.rs @@ -440,7 +440,7 @@ mod tests { Action, EventMatchCondition, PushCondition, PushRulesEvent, RoomMemberCountCondition, SenderNotificationPermissionCondition, Tweak, }; - use crate::EventResult; + use crate::EventJson; #[test] fn serialize_string_action() { @@ -814,9 +814,9 @@ mod tests { }, "type": "m.push_rules" }); - assert!(from_json_value::>(json_data) + assert!(from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .is_ok()); } } diff --git a/src/room/canonical_alias.rs b/src/room/canonical_alias.rs index f0d2cd48..4eda21ee 100644 --- a/src/room/canonical_alias.rs +++ b/src/room/canonical_alias.rs @@ -195,7 +195,7 @@ mod tests { use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::{CanonicalAliasEvent, CanonicalAliasEventContent}; - use crate::EventResult; + use crate::EventJson; #[test] fn serialization_with_optional_fields_as_none() { @@ -239,9 +239,9 @@ mod tests { }); assert_eq!( - from_json_value::>(json_data) + from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap() .content .alias, @@ -262,9 +262,9 @@ mod tests { "type": "m.room.canonical_alias" }); assert_eq!( - from_json_value::>(json_data) + from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap() .content .alias, @@ -285,9 +285,9 @@ mod tests { "type": "m.room.canonical_alias" }); assert_eq!( - from_json_value::>(json_data) + from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap() .content .alias, @@ -309,9 +309,9 @@ mod tests { "type": "m.room.canonical_alias" }); assert_eq!( - from_json_value::>(json_data) + from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap() .content .alias, diff --git a/src/room/create.rs b/src/room/create.rs index f8f6aa86..9d4eba71 100644 --- a/src/room/create.rs +++ b/src/room/create.rs @@ -57,7 +57,7 @@ mod tests { use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::CreateEventContent; - use crate::EventResult; + use crate::EventJson; #[test] fn serialization() { @@ -93,9 +93,9 @@ mod tests { }); assert_eq!( - from_json_value::>(json) + from_json_value::>(json) .unwrap() - .into_result() + .deserialize() .unwrap(), content ); diff --git a/src/room/encrypted.rs b/src/room/encrypted.rs index 99b4020b..8da70ee4 100644 --- a/src/room/encrypted.rs +++ b/src/room/encrypted.rs @@ -249,7 +249,7 @@ mod tests { use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::{Algorithm, EncryptedEventContent, MegolmV1AesSha2Content}; - use crate::EventResult; + use crate::EventJson; #[test] fn serializtion() { @@ -296,9 +296,9 @@ mod tests { }); assert_eq!( - from_json_value::>(json_data) + from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap(), key_verification_start_content ); @@ -316,9 +316,9 @@ mod tests { }, "algorithm": "m.olm.v1.curve25519-aes-sha2" }); - let content = from_json_value::>(json_data) + let content = from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap(); match content { @@ -335,11 +335,11 @@ mod tests { #[test] fn deserialization_failure() { - assert!(from_json_value::>( + assert!(from_json_value::>( json!({"algorithm": "m.megolm.v1.aes-sha2"}) ) .unwrap() - .into_result() + .deserialize() .is_err()); } } diff --git a/src/room/message.rs b/src/room/message.rs index a1de2bb2..77c05d68 100644 --- a/src/room/message.rs +++ b/src/room/message.rs @@ -1028,7 +1028,7 @@ mod tests { use super::{AudioMessageEventContent, MessageEventContent}; use crate::room::message::{InReplyTo, RelatesTo, TextMessageEventContent}; - use crate::EventResult; + use crate::EventJson; use ruma_identifiers::EventId; use std::convert::TryFrom; @@ -1108,9 +1108,9 @@ mod tests { }); assert_eq!( - from_json_value::>(json_data) + from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap(), message_event_content ); @@ -1122,11 +1122,9 @@ mod tests { "body": "test","msgtype": "m.location", "url": "http://example.com/audio.mp3" }); - assert!( - from_json_value::>(json_data) - .unwrap() - .into_result() - .is_err() - ); + assert!(from_json_value::>(json_data) + .unwrap() + .deserialize() + .is_err()); } } diff --git a/src/room/name.rs b/src/room/name.rs index 0fe4c54f..d708cfdc 100644 --- a/src/room/name.rs +++ b/src/room/name.rs @@ -163,7 +163,7 @@ mod tests { use ruma_identifiers::{EventId, RoomId, UserId}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; - use crate::EventResult; + use crate::EventJson; use super::{NameEvent, NameEventContent}; @@ -245,9 +245,9 @@ mod tests { "type": "m.room.name" }); assert_eq!( - from_json_value::>(json_data) + from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap() .content .name, @@ -264,10 +264,10 @@ mod tests { let long_content_json_string: String = serde_json::json!({ "name": &long_string }).to_string(); - let from_raw: EventResult = + let from_raw: EventJson = serde_json::from_str(&long_content_json_string).unwrap(); - let result = from_raw.into_result(); + let result = from_raw.deserialize(); assert!(result.is_err(), "Result should be invalid: {:?}", result); } @@ -275,10 +275,10 @@ mod tests { fn json_with_empty_name_creates_content_as_none() { let long_content_json_string: String = serde_json::json!({ "name": "" }).to_string(); - let from_raw: EventResult = + let from_raw: EventJson = serde_json::from_str(&long_content_json_string).unwrap(); assert_eq!( - from_raw.into_result().unwrap(), + from_raw.deserialize().unwrap(), NameEventContent { name: None } ); } @@ -304,9 +304,9 @@ mod tests { "type": "m.room.name" }); assert_eq!( - from_json_value::>(json_data) + from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap() .content .name, @@ -327,9 +327,9 @@ mod tests { "type": "m.room.name" }); assert_eq!( - from_json_value::>(json_data) + from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap() .content .name, @@ -352,9 +352,9 @@ mod tests { }); assert_eq!( - from_json_value::>(json_data) + from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap() .content .name, diff --git a/src/room/pinned_events.rs b/src/room/pinned_events.rs index ec149691..409cc823 100644 --- a/src/room/pinned_events.rs +++ b/src/room/pinned_events.rs @@ -27,7 +27,7 @@ mod tests { use crate::{ room::pinned_events::{PinnedEventsEvent, PinnedEventsEventContent}, - Event, EventResult, RoomEvent, StateEvent, + Event, EventJson, RoomEvent, StateEvent, }; #[test] @@ -50,9 +50,9 @@ mod tests { let serialized_event = to_string(&event).unwrap(); let parsed_event: PinnedEventsEvent = - serde_json::from_str::>(&serialized_event) + serde_json::from_str::>(&serialized_event) .unwrap() - .into_result() + .deserialize() .unwrap(); assert_eq!(parsed_event.event_id(), event.event_id()); diff --git a/src/room/server_acl.rs b/src/room/server_acl.rs index 963f0071..121dcf61 100644 --- a/src/room/server_acl.rs +++ b/src/room/server_acl.rs @@ -174,7 +174,7 @@ mod tests { use serde_json::{from_value as from_json_value, json}; use super::ServerAclEvent; - use crate::EventResult; + use crate::EventJson; #[test] fn default_values() { @@ -185,9 +185,9 @@ mod tests { "state_key": "", "type": "m.room.server_acl" }); - let server_acl_event: ServerAclEvent = from_json_value::>(json_data) + let server_acl_event: ServerAclEvent = from_json_value::>(json_data) .unwrap() - .into_result() + .deserialize() .unwrap(); assert_eq!(server_acl_event.content.allow_ip_literals, true); diff --git a/src/stripped.rs b/src/stripped.rs index 9e01dda8..59ef8d9c 100644 --- a/src/stripped.rs +++ b/src/stripped.rs @@ -317,7 +317,7 @@ mod tests { use super::{AnyStrippedStateEvent, StrippedRoomName, StrippedRoomTopic}; use crate::{ room::{join_rules::JoinRule, topic::TopicEventContent}, - EventResult, EventType, + EventJson, EventType, }; #[test] @@ -390,9 +390,9 @@ mod tests { } }); - match from_json_value::>(name_event.clone()) + match from_json_value::>(name_event.clone()) .unwrap() - .into_result() + .deserialize() .unwrap() { AnyStrippedStateEvent::RoomName(event) => { @@ -405,14 +405,14 @@ mod tests { }; // Ensure `StrippedStateContent` can be parsed, not just `StrippedState`. - assert!(from_json_value::>(name_event) + assert!(from_json_value::>(name_event) .unwrap() - .into_result() + .deserialize() .is_ok()); - match from_json_value::>(join_rules_event) + match from_json_value::>(join_rules_event) .unwrap() - .into_result() + .deserialize() .unwrap() { AnyStrippedStateEvent::RoomJoinRules(event) => { @@ -424,9 +424,9 @@ mod tests { _ => unreachable!(), }; - match from_json_value::>(avatar_event) + match from_json_value::>(avatar_event) .unwrap() - .into_result() + .deserialize() .unwrap() { AnyStrippedStateEvent::RoomAvatar(event) => { diff --git a/src/to_device.rs b/src/to_device.rs index 587c7e8a..841b81f9 100644 --- a/src/to_device.rs +++ b/src/to_device.rs @@ -272,19 +272,19 @@ mod tests { }, room::encrypted::EncryptedEventContent, room_key_request::Action, - Algorithm, Empty, EventResult, + Algorithm, Empty, EventJson, }; macro_rules! deserialize { ($source:ident, $($target:tt)*) => {{ - let event = from_json_value::>($source) + let event = from_json_value::>($source) .expect(&format!( "Can't deserialize to-device event: {} from source {}", stringify!($($target)*), stringify!($source) )); let event = event - .into_result() + .deserialize() .expect("To-device event {} deserialized into a invalid event"); match event { diff --git a/src/util.rs b/src/util.rs index 80bb7cc6..bf96c909 100644 --- a/src/util.rs +++ b/src/util.rs @@ -6,7 +6,7 @@ use serde::{ }; use serde_json::Value; -use crate::{EventResult, TryFromRaw}; +use crate::{EventJson, TryFromRaw}; pub fn try_convert_variant( variant: fn(Content) -> Enum, @@ -130,9 +130,9 @@ where assert_eq!(se, serde_json::to_value(de.clone()).unwrap()); assert_eq!( de, - serde_json::from_value::>(se) + serde_json::from_value::>(se) .unwrap() - .into_result() + .deserialize() .unwrap() ); }