diff --git a/ruma-events-macros/tests/ruma_events_macros.rs b/ruma-events-macros/tests/ruma_events_macros.rs index 76bc70ba..235bcea5 100644 --- a/ruma-events-macros/tests/ruma_events_macros.rs +++ b/ruma-events-macros/tests/ruma_events_macros.rs @@ -1,4 +1,7 @@ -use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; +use std::{ + convert::Infallible, + fmt::{Debug, Display, Formatter, Result as FmtResult}, +}; use serde::{ de::{DeserializeOwned, Error as SerdeError, Visitor}, @@ -115,14 +118,14 @@ pub trait FromRaw: Sized { pub trait TryFromRaw: Sized { /// The raw form of this event that deserialization falls back to if deserializing `Self` fails. type Raw: DeserializeOwned; - type Err: Into; + type Err: Display; fn try_from_raw(_: Self::Raw) -> Result; } impl TryFromRaw for T { type Raw = ::Raw; - type Err = Void; + type Err = Infallible; fn try_from_raw(raw: Self::Raw) -> Result { Ok(Self::from_raw(raw)) @@ -153,9 +156,9 @@ where match T::try_from_raw(raw_data) { Ok(value) => Ok(EventResult::Ok(value)), - Err((msg, raw_data)) => Ok(EventResult::Err(InvalidEvent( + Err((err, raw_data)) => Ok(EventResult::Err(InvalidEvent( InnerInvalidEvent::Validation { - message: msg.into(), + message: err.to_string(), raw_data, }, ))), @@ -163,14 +166,6 @@ where } } -pub enum Void {} - -impl From for String { - fn from(v: Void) -> Self { - match v {} - } -} - /// A basic event. pub trait Event: Debug + Serialize + TryFromRaw { /// The type of this event's `content` field. @@ -242,7 +237,7 @@ enum InnerInvalidEvent { /// The event data that failed validation. raw_data: T, - /// An message describing why the event was invalid. + /// A message describing why the event was invalid. message: String, }, } diff --git a/src/lib.rs b/src/lib.rs index 797ea10d..764238e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -115,6 +115,7 @@ //#![deny(warnings)] use std::{ + convert::Infallible, error::Error, fmt::{Debug, Display, Error as FmtError, Formatter, Result as FmtResult}, }; @@ -226,7 +227,7 @@ enum InnerInvalidEvent { /// The event data that failed validation. raw_data: T, - /// An message describing why the event was invalid. + /// A message describing why the event was invalid. message: String, }, } @@ -259,31 +260,20 @@ pub trait FromRaw: Sized { pub trait TryFromRaw: Sized { /// The raw form of this event that deserialization falls back to if deserializing `Self` fails. type Raw: DeserializeOwned; - type Err: Into; + type Err: Display; fn try_from_raw(_: Self::Raw) -> Result; } impl TryFromRaw for T { type Raw = ::Raw; - type Err = Void; + type Err = Infallible; fn try_from_raw(raw: Self::Raw) -> Result { Ok(Self::from_raw(raw)) } } -// TODO: Replace with ! once that is stable -/// An empty type -#[derive(Debug)] -pub enum Void {} - -impl From for String { - fn from(v: Void) -> Self { - match v {} - } -} - /// 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 @@ -335,9 +325,9 @@ where match T::try_from_raw(raw_data) { Ok(value) => Ok(EventResult::Ok(value)), - Err((msg, raw_data)) => Ok(EventResult::Err(InvalidEvent( + Err((err, raw_data)) => Ok(EventResult::Err(InvalidEvent( InnerInvalidEvent::Validation { - message: msg.into(), + message: err.to_string(), raw_data, }, ))), diff --git a/src/util.rs b/src/util.rs index d225d298..69a6fb91 100644 --- a/src/util.rs +++ b/src/util.rs @@ -7,7 +7,7 @@ pub fn try_convert_variant( ) -> Result { Content::try_from_raw(raw) .map(variant) - .map_err(|(msg, raw)| (msg.into(), raw_variant(raw))) + .map_err(|(err, raw)| (err.to_string(), raw_variant(raw))) } pub fn serde_json_error_to_generic_de_error(error: serde_json::Error) -> E {