Move FromRaw, TryFromRaw into a new module
This commit is contained in:
parent
ddbb06f21d
commit
0efe82bf88
37
src/from_raw.rs
Normal file
37
src/from_raw.rs
Normal file
@ -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<Self, Self::Err>;
|
||||
}
|
||||
|
||||
impl<T: FromRaw> TryFromRaw for T {
|
||||
type Raw = <T as FromRaw>::Raw;
|
||||
type Err = Infallible;
|
||||
|
||||
fn try_from_raw(raw: Self::Raw) -> Result<Self, Self::Err> {
|
||||
Ok(Self::from_raw(raw))
|
||||
}
|
||||
}
|
45
src/lib.rs
45
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<Self, Self::Err>;
|
||||
}
|
||||
|
||||
impl<T: FromRaw> TryFromRaw for T {
|
||||
type Raw = <T as FromRaw>::Raw;
|
||||
type Err = Infallible;
|
||||
|
||||
fn try_from_raw(raw: Self::Raw) -> Result<Self, Self::Err> {
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user