diff --git a/src/from_raw.rs b/src/from_raw.rs new file mode 100644 index 00000000..020ca4cf --- /dev/null +++ b/src/from_raw.rs @@ -0,0 +1,37 @@ +use std::{convert::Infallible, fmt::Display}; + +use serde::de::DeserializeOwned; + +/// See [`TryFromRaw`][try]. This trait is merely a convenience that is be implemented instead of +/// [`TryFromRaw`][try] to get a [`TryFromRaw`][try] implementation with slightly less code if the +/// conversion can't fail, that is, the raw type and `Self` are identical in definition. +/// +/// [try]: trait.TryFromRaw.html +pub trait FromRaw: Sized { + /// The raw type. + type Raw: DeserializeOwned; + + /// Converts the raw type to `Self`. + fn from_raw(_: Self::Raw) -> Self; +} + +/// Types corresponding to some item in the matrix spec. Types that implement this trait have a +/// corresponding 'raw' type, a potentially invalid representation that can be converted to `Self`. +pub trait TryFromRaw: Sized { + /// The raw type. + type Raw: DeserializeOwned; + /// The error type returned if conversion fails. + type Err: Display; + + /// Tries to convert the raw type to `Self`. + fn try_from_raw(_: Self::Raw) -> Result; +} + +impl TryFromRaw for T { + type Raw = ::Raw; + type Err = Infallible; + + fn try_from_raw(raw: Self::Raw) -> Result { + Ok(Self::from_raw(raw)) + } +} diff --git a/src/lib.rs b/src/lib.rs index a33ae4b6..e2bec39f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -115,7 +115,6 @@ #![allow(clippy::use_self)] use std::{ - convert::Infallible, error::Error, fmt::{Debug, Display, Formatter, Result as FmtResult}, }; @@ -123,7 +122,7 @@ use std::{ use js_int::UInt; use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{ - de::{DeserializeOwned, MapAccess, Visitor}, + de::{MapAccess, Visitor}, ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer, }; @@ -135,6 +134,7 @@ pub use self::{custom::CustomEvent, custom_room::CustomRoomEvent, custom_state:: mod macros; mod algorithm; mod event_type; +mod from_raw; mod util; pub mod call; @@ -166,8 +166,11 @@ pub mod tag; pub mod to_device; pub mod typing; -pub use algorithm::Algorithm; -pub use event_type::EventType; +pub use self::{ + algorithm::Algorithm, + event_type::EventType, + from_raw::{FromRaw, TryFromRaw}, +}; /// An event that is malformed or otherwise invalid. /// @@ -234,40 +237,6 @@ impl Display for InvalidInput { impl Error for InvalidInput {} -/// See [`TryFromRaw`][try]. This trait is merely a convenience that is be implemented instead of -/// [`TryFromRaw`][try] to get a [`TryFromRaw`][try] implementation with slightly less code if the -/// conversion can't fail, that is, the raw type and `Self` are identical in definition. -/// -/// [try]: trait.TryFromRaw.html -pub trait FromRaw: Sized { - /// The raw type. - type Raw: DeserializeOwned; - - /// Converts the raw type to `Self`. - fn from_raw(_: Self::Raw) -> Self; -} - -/// Types corresponding to some item in the matrix spec. Types that implement this trait have a -/// corresponding 'raw' type, a potentially invalid representation that can be converted to `Self`. -pub trait TryFromRaw: Sized { - /// The raw type. - type Raw: DeserializeOwned; - /// The error type returned if conversion fails. - type Err: Display; - - /// Tries to convert the raw type to `Self`. - fn try_from_raw(_: Self::Raw) -> Result; -} - -impl TryFromRaw for T { - type Raw = ::Raw; - type Err = Infallible; - - fn try_from_raw(raw: Self::Raw) -> Result { - Ok(Self::from_raw(raw)) - } -} - /// 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