From 1af436b7757aebb6d35f658e45c8a96df846354a Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 24 Oct 2019 22:26:20 +0200 Subject: [PATCH] Simplify InvalidEvent, update its documentation --- .../tests/ruma_events_macros.rs | 93 +++++++++++-------- src/lib.rs | 92 ++++++------------ 2 files changed, 81 insertions(+), 104 deletions(-) diff --git a/ruma-events-macros/tests/ruma_events_macros.rs b/ruma-events-macros/tests/ruma_events_macros.rs index 235bcea5..fad445e6 100644 --- a/ruma-events-macros/tests/ruma_events_macros.rs +++ b/ruma-events-macros/tests/ruma_events_macros.rs @@ -93,12 +93,12 @@ pub enum EventResult { /// `T` deserialized but was invalid. /// /// `InvalidEvent` contains the original input. - Err(InvalidEvent), + Err(InvalidEvent), } impl EventResult { - /// Convert `EventResult` into the equivalent `std::result::Result>`. - pub fn into_result(self) -> Result> { + /// 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), @@ -145,23 +145,21 @@ where let raw_data: T::Raw = match serde_json::from_value(json.clone()) { Ok(raw) => raw, Err(error) => { - return Ok(EventResult::Err(InvalidEvent( - InnerInvalidEvent::Deserialization { - json, - error: error.to_string(), - }, - ))); + 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, raw_data)) => Ok(EventResult::Err(InvalidEvent( - InnerInvalidEvent::Validation { - message: err.to_string(), - raw_data, - }, - ))), + Err((err, _)) => Ok(EventResult::Err(InvalidEvent { + message: err.to_string(), + json, + kind: InvalidEventKind::Validation, + })), } } } @@ -211,35 +209,50 @@ pub trait StateEvent: RoomEvent { /// An event that is malformed or otherwise invalid. /// -/// When attempting to create an event from a string of JSON data, an error in the input data may -/// cause deserialization to fail, or the JSON structure may not corresponded to ruma-events's -/// strict definition of the event's schema. If deserialization completely fails, this type will -/// provide a message with details about the deserialization error. If deserialization succeeds but -/// the event is otherwise invalid, a similar message will be provided, as well as a -/// `serde_json::Value` containing the raw JSON data as it was deserialized. -#[derive(Debug)] -pub struct InvalidEvent(InnerInvalidEvent); +/// When attempting to deserialize an `EventResult`, 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. +#[derive(Clone, Debug)] +pub struct InvalidEvent { + message: String, + json: Value, + kind: InvalidEventKind, +} -/// An event that is malformed or otherwise invalid. -#[derive(Debug)] -enum InnerInvalidEvent { - /// An event that failed to deserialize from JSON. - Deserialization { - /// The raw `serde_json::Value` representation of the invalid event. - json: Value, +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +enum InvalidEventKind { + Deserialization, + Validation, +} - /// The deserialization error returned by serde. - error: String, - }, +impl InvalidEvent { + /// A message describing why the event is invalid. + pub fn message(&self) -> String { + self.message.clone() + } - /// An event that deserialized but failed validation. - Validation { - /// The event data that failed validation. - raw_data: T, + /// The `serde_json::Value` representation of the invalid event. + pub fn json(&self) -> &Value { + &self.json + } - /// A message describing why the event was invalid. - message: String, - }, + /// Returns whether this is a deserialization error. + pub fn is_deserialization(&self) -> bool { + self.kind == InvalidEventKind::Deserialization + } + + /// Returns whether this is a validation error. + pub fn is_validation(&self) -> bool { + self.kind == InvalidEventKind::Validation + } +} + +impl Display for InvalidEvent { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + write!(f, "{}", self.message()) + } } // See note about wrapping macro expansion in a module from `src/lib.rs` diff --git a/src/lib.rs b/src/lib.rs index 528b8650..7dcc8da5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -166,63 +166,43 @@ pub mod typing; /// An event that is malformed or otherwise invalid. /// -/// When attempting to create an event from a string of JSON data, an error in the input data may -/// cause deserialization to fail, or the JSON structure may not corresponded to ruma-events's -/// strict definition of the event's schema. If deserialization completely fails, this type will -/// provide a message with details about the deserialization error. If deserialization succeeds but -/// the event is otherwise invalid, a similar message will be provided, as well as a -/// `serde_json::Value` containing the raw JSON data as it was deserialized. +/// When attempting to deserialize an `EventResult`, 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. #[derive(Clone, Debug)] -pub enum InvalidEvent { - /// An error that occured during deserialization. - Deserialization(DeserializationError), +pub struct InvalidEvent { + message: String, + json: Value, + kind: InvalidEventKind, +} - /// An error that occured during raw event validation. - Validation(ValidationError), - - /// Additional variants may be added in the future and will not be considered breaking changes - /// to ruma-events. - #[doc(hidden)] - __Nonexhaustive, +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +enum InvalidEventKind { + Deserialization, + Validation, } impl InvalidEvent { /// A message describing why the event is invalid. pub fn message(&self) -> String { - match self { - InvalidEvent::Deserialization(err) => err.message.clone(), - InvalidEvent::Validation(err) => err.message.clone(), - InvalidEvent::__Nonexhaustive => { - panic!("__Nonexhaustive enum variant is not intended for use.") - } - } + self.message.clone() } /// The `serde_json::Value` representation of the invalid event. pub fn json(&self) -> &Value { - match self { - InvalidEvent::Deserialization(err) => &err.json, - InvalidEvent::Validation(err) => &err.json, - InvalidEvent::__Nonexhaustive => { - panic!("__Nonexhaustive enum variant is not intended for use.") - } - } + &self.json } /// Returns whether this is a deserialization error. pub fn is_deserialization(&self) -> bool { - match self { - InvalidEvent::Deserialization(_) => true, - _ => false, - } + self.kind == InvalidEventKind::Deserialization } /// Returns whether this is a validation error. pub fn is_validation(&self) -> bool { - match self { - InvalidEvent::Validation(_) => true, - _ => false, - } + self.kind == InvalidEventKind::Validation } } @@ -234,20 +214,6 @@ impl Display for InvalidEvent { impl Error for InvalidEvent {} -/// An error that occured during deserialization. -#[derive(Clone, Debug)] -pub struct DeserializationError { - message: String, - json: Value, -} - -/// An error that occured during raw event validation. -#[derive(Clone, Debug)] -pub struct ValidationError { - message: String, - json: Value, -} - /// An error returned when attempting to create an event with data that would make it invalid. /// /// This type is similar to `InvalidEvent`, but used during the construction of a new event, as @@ -336,23 +302,21 @@ where let raw_data: T::Raw = match serde_json::from_value(json.clone()) { Ok(raw) => raw, Err(error) => { - return Ok(EventResult::Err(InvalidEvent::Deserialization( - DeserializationError { - json, - message: error.to_string(), - }, - ))); + 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::Validation( - ValidationError { - message: err.to_string(), - json, - }, - ))), + Err((err, _)) => Ok(EventResult::Err(InvalidEvent { + message: err.to_string(), + json, + kind: InvalidEventKind::Validation, + })), } } }