Rename EventResultCompatible to TryFromRaw

This commit is contained in:
Jonas Platte 2019-10-15 20:02:12 +02:00
parent 516f027aca
commit a6c34596d7
14 changed files with 50 additions and 52 deletions

View File

@ -327,7 +327,7 @@ impl ToTokens for RumaEvent {
}
quote! {
impl crate::EventResultCompatible for #content_name {
impl crate::TryFromRaw for #content_name {
type Raw = raw::#content_name;
type Err = crate::Void;
@ -353,7 +353,7 @@ impl ToTokens for RumaEvent {
#content
impl crate::EventResultCompatible for #name {
impl crate::TryFromRaw for #name {
type Raw = raw::#name;
type Err = crate::Void;

View File

@ -85,7 +85,7 @@ impl<'de> Deserialize<'de> for EventType {
/// The result of deserializing an event, which may or may not be valid.
#[derive(Debug)]
pub enum EventResult<T: EventResultCompatible> {
pub enum EventResult<T: TryFromRaw> {
/// `T` deserialized and validated successfully.
Ok(T),
@ -95,7 +95,7 @@ pub enum EventResult<T: EventResultCompatible> {
Err(InvalidEvent<T::Raw>),
}
impl<T: EventResultCompatible> EventResult<T> {
impl<T: TryFromRaw> EventResult<T> {
/// Convert `EventResult<T>` into the equivalent `std::result::Result<T, InvalidEvent<T::Raw>>`.
pub fn into_result(self) -> Result<T, InvalidEvent<T::Raw>> {
match self {
@ -105,7 +105,7 @@ impl<T: EventResultCompatible> EventResult<T> {
}
}
pub trait EventResultCompatible {
pub trait TryFromRaw {
/// The raw form of this event that deserialization falls back to if deserializing `Self` fails.
type Raw;
type Err: Into<String>;
@ -115,7 +115,7 @@ pub trait EventResultCompatible {
fn from_raw<T>(raw: T::Raw) -> T
where
T: EventResultCompatible<Err = Void>,
T: TryFromRaw<Err = Void>,
{
match T::try_from_raw(raw) {
Ok(c) => c,
@ -132,7 +132,7 @@ impl From<Void> for String {
}
/// A basic event.
pub trait Event: Debug + Serialize + EventResultCompatible {
pub trait Event: Debug + Serialize + TryFromRaw {
/// The type of this event's `content` field.
type Content: Debug + Serialize;

View File

@ -46,7 +46,7 @@ use crate::{
sticker::StickerEvent,
tag::TagEvent,
typing::TypingEvent,
CustomEvent, CustomRoomEvent, CustomStateEvent, EventResultCompatible, Void,
CustomEvent, CustomRoomEvent, CustomStateEvent, TryFromRaw, Void,
};
/// A basic event, room event, or state event.
@ -334,7 +334,7 @@ pub enum StateEvent {
CustomState(CustomStateEvent),
}
impl EventResultCompatible for Event {
impl TryFromRaw for Event {
type Raw = raw::Event;
type Err = Void;
@ -343,7 +343,7 @@ impl EventResultCompatible for Event {
}
}
impl EventResultCompatible for RoomEvent {
impl TryFromRaw for RoomEvent {
type Raw = raw::RoomEvent;
type Err = Void;
@ -352,7 +352,7 @@ impl EventResultCompatible for RoomEvent {
}
}
impl EventResultCompatible for StateEvent {
impl TryFromRaw for StateEvent {
type Raw = raw::StateEvent;
type Err = Void;

View File

@ -30,7 +30,7 @@ use crate::{
sticker::StickerEvent,
tag::TagEvent,
typing::TypingEvent,
CustomEvent, CustomRoomEvent, EventResultCompatible, Void,
CustomEvent, CustomRoomEvent, TryFromRaw, Void,
};
/// A basic event.
@ -130,7 +130,7 @@ pub enum RoomEvent {
CustomRoom(CustomRoomEvent),
}
impl EventResultCompatible for Event {
impl TryFromRaw for Event {
type Raw = raw::Event;
type Err = Void;
@ -139,7 +139,7 @@ impl EventResultCompatible for Event {
}
}
impl EventResultCompatible for RoomEvent {
impl TryFromRaw for RoomEvent {
type Raw = raw::RoomEvent;
type Err = Void;

View File

@ -3,7 +3,7 @@
use ruma_identifiers::UserId;
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
use crate::{vec_as_map_of_empty, Event as _, EventResultCompatible, EventType, Void};
use crate::{vec_as_map_of_empty, Event as _, EventType, TryFromRaw, Void};
/// A list of users to ignore.
#[derive(Clone, Debug, PartialEq)]
@ -12,7 +12,7 @@ pub struct IgnoredUserListEvent {
pub content: IgnoredUserListEventContent,
}
impl EventResultCompatible for IgnoredUserListEvent {
impl TryFromRaw for IgnoredUserListEvent {
type Raw = raw::IgnoredUserListEvent;
type Err = Void;
@ -44,7 +44,7 @@ pub struct IgnoredUserListEventContent {
pub ignored_users: Vec<UserId>,
}
impl EventResultCompatible for IgnoredUserListEventContent {
impl TryFromRaw for IgnoredUserListEventContent {
type Raw = raw::IgnoredUserListEventContent;
type Err = Void;

View File

@ -8,7 +8,7 @@ use super::{
HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString,
VerificationMethod,
};
use crate::{Event, EventResultCompatible, EventType, InvalidInput};
use crate::{Event, EventType, InvalidInput, TryFromRaw};
/// Begins an SAS key verification process.
///
@ -31,7 +31,7 @@ pub enum StartEventContent {
__Nonexhaustive,
}
impl EventResultCompatible for StartEvent {
impl TryFromRaw for StartEvent {
type Raw = raw::StartEvent;
type Err = &'static str;
@ -63,7 +63,7 @@ impl_event!(
EventType::KeyVerificationStart
);
impl EventResultCompatible for StartEventContent {
impl TryFromRaw for StartEventContent {
type Raw = raw::StartEventContent;
type Err = &'static str;

View File

@ -246,7 +246,7 @@ impl Display for InvalidInput {
impl Error for InvalidInput {}
/// Marks types that can be deserialized as EventResult<Self>
pub trait EventResultCompatible: 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<String>;
@ -267,7 +267,7 @@ impl From<Void> for String {
fn from_raw<T>(raw: T::Raw) -> T
where
T: EventResultCompatible<Err = Void>,
T: TryFromRaw<Err = Void>,
{
match T::try_from_raw(raw) {
Ok(c) => c,
@ -282,7 +282,7 @@ where
/// this structure will contain an `InvalidEvent`. See the documentation for `InvalidEvent` for
/// more details.
#[derive(Clone, Debug)]
pub enum EventResult<T: EventResultCompatible> {
pub enum EventResult<T: TryFromRaw> {
/// `T` deserialized and validated successfully.
Ok(T),
@ -292,7 +292,7 @@ pub enum EventResult<T: EventResultCompatible> {
Err(InvalidEvent<T::Raw>),
}
impl<T: EventResultCompatible> EventResult<T> {
impl<T: TryFromRaw> EventResult<T> {
/// Convert `EventResult<T>` into the equivalent `std::result::Result<T, InvalidEvent>`.
pub fn into_result(self) -> Result<T, InvalidEvent<T::Raw>> {
match self {
@ -304,7 +304,7 @@ impl<T: EventResultCompatible> EventResult<T> {
impl<'de, T> Deserialize<'de> for EventResult<T>
where
T: EventResultCompatible,
T: TryFromRaw,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
@ -338,7 +338,7 @@ where
// For now, we don't support serialization of EventResult.
// This is going to be added in a future version.
impl<T: EventResultCompatible> Serialize for EventResult<T> {
impl<T: TryFromRaw> Serialize for EventResult<T> {
fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
@ -546,7 +546,7 @@ pub enum EventType {
}
/// A basic event.
pub trait Event: Debug + Serialize + Sized + EventResultCompatible {
pub trait Event: Debug + Serialize + Sized + TryFromRaw {
/// The type of this event's `content` field.
type Content: Debug + Serialize;

View File

@ -5,7 +5,7 @@ use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId};
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
use serde_json::Value;
use crate::{empty_string_as_none, Event, EventResultCompatible, EventType, Void};
use crate::{empty_string_as_none, Event, EventType, TryFromRaw, Void};
/// Informs the room as to which alias is the canonical one.
#[derive(Clone, Debug, PartialEq)]
@ -45,7 +45,7 @@ pub struct CanonicalAliasEventContent {
pub alias: Option<RoomAliasId>,
}
impl EventResultCompatible for CanonicalAliasEvent {
impl TryFromRaw for CanonicalAliasEvent {
type Raw = raw::CanonicalAliasEvent;
type Err = Void;
@ -63,7 +63,7 @@ impl EventResultCompatible for CanonicalAliasEvent {
}
}
impl EventResultCompatible for CanonicalAliasEventContent {
impl TryFromRaw for CanonicalAliasEventContent {
type Raw = raw::CanonicalAliasEventContent;
type Err = Void;

View File

@ -5,7 +5,7 @@ use ruma_identifiers::{DeviceId, EventId, RoomId, UserId};
use serde::{de::Error, ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{from_value, Value};
use crate::{Algorithm, Event, EventResultCompatible, EventType, Void};
use crate::{Algorithm, Event, EventType, TryFromRaw, Void};
/// This event type is used when sending encrypted events.
///
@ -48,7 +48,7 @@ pub enum EncryptedEventContent {
__Nonexhaustive,
}
impl EventResultCompatible for EncryptedEvent {
impl TryFromRaw for EncryptedEvent {
type Raw = raw::EncryptedEvent;
type Err = Void;
@ -64,7 +64,7 @@ impl EventResultCompatible for EncryptedEvent {
}
}
impl EventResultCompatible for EncryptedEventContent {
impl TryFromRaw for EncryptedEventContent {
type Raw = raw::EncryptedEventContent;
type Err = Void;

View File

@ -10,7 +10,7 @@ use serde::{
use serde_json::{from_value, Value};
use super::{EncryptedFile, ImageInfo, ThumbnailInfo};
use crate::{Event, EventResultCompatible, EventType, Void};
use crate::{Event, EventType, TryFromRaw, Void};
pub mod feedback;
@ -74,7 +74,7 @@ pub enum MessageEventContent {
__Nonexhaustive,
}
impl EventResultCompatible for MessageEvent {
impl TryFromRaw for MessageEvent {
type Raw = raw::MessageEvent;
type Err = Void;
@ -90,7 +90,7 @@ impl EventResultCompatible for MessageEvent {
}
}
impl EventResultCompatible for MessageEventContent {
impl TryFromRaw for MessageEventContent {
type Raw = raw::MessageEventContent;
type Err = Void;

View File

@ -5,9 +5,7 @@ use ruma_identifiers::{EventId, RoomId, UserId};
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
use serde_json::Value;
use crate::{
empty_string_as_none, Event as _, EventResultCompatible, EventType, InvalidInput, Void,
};
use crate::{empty_string_as_none, Event as _, EventType, InvalidInput, TryFromRaw, Void};
/// A human-friendly room name designed to be displayed to the end-user.
#[derive(Clone, Debug, PartialEq)]
@ -45,7 +43,7 @@ pub struct NameEventContent {
pub(crate) name: Option<String>,
}
impl EventResultCompatible for NameEvent {
impl TryFromRaw for NameEvent {
type Raw = raw::NameEvent;
type Err = Void;
@ -63,7 +61,7 @@ impl EventResultCompatible for NameEvent {
}
}
impl EventResultCompatible for NameEventContent {
impl TryFromRaw for NameEventContent {
type Raw = raw::NameEventContent;
type Err = Void;

View File

@ -7,7 +7,7 @@ use ruma_identifiers::{EventId, RoomId, UserId};
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
use serde_json::Value;
use crate::{Event as _, EventResultCompatible, EventType, Void};
use crate::{Event as _, EventType, TryFromRaw, Void};
/// Defines the power levels (privileges) of users in the room.
#[derive(Clone, Debug, PartialEq)]
@ -85,7 +85,7 @@ pub struct PowerLevelsEventContent {
pub notifications: NotificationPowerLevels,
}
impl EventResultCompatible for PowerLevelsEvent {
impl TryFromRaw for PowerLevelsEvent {
type Raw = raw::PowerLevelsEvent;
type Err = Void;
@ -103,7 +103,7 @@ impl EventResultCompatible for PowerLevelsEvent {
}
}
impl EventResultCompatible for PowerLevelsEventContent {
impl TryFromRaw for PowerLevelsEventContent {
type Raw = raw::PowerLevelsEventContent;
type Err = Void;

View File

@ -5,7 +5,7 @@ use ruma_identifiers::{EventId, RoomId, UserId};
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
use serde_json::Value;
use crate::{default_true, Event as _, EventResultCompatible, EventType, Void};
use crate::{default_true, Event as _, EventType, TryFromRaw, Void};
/// An event to indicate which servers are permitted to participate in the room.
#[derive(Clone, Debug, PartialEq)]
@ -65,7 +65,7 @@ pub struct ServerAclEventContent {
pub deny: Vec<String>,
}
impl EventResultCompatible for ServerAclEvent {
impl TryFromRaw for ServerAclEvent {
type Raw = raw::ServerAclEvent;
type Err = Void;
@ -83,7 +83,7 @@ impl EventResultCompatible for ServerAclEvent {
}
}
impl EventResultCompatible for ServerAclEventContent {
impl TryFromRaw for ServerAclEventContent {
type Raw = raw::ServerAclEventContent;
type Err = Void;

View File

@ -18,7 +18,7 @@ use crate::{
power_levels::PowerLevelsEventContent, third_party_invite::ThirdPartyInviteEventContent,
topic::TopicEventContent,
},
EventResultCompatible, EventType,
EventType, TryFromRaw,
};
/// A stripped-down version of a state event that is included along with some other events.
@ -116,14 +116,14 @@ pub type StrippedRoomThirdPartyInvite = StrippedStateContent<ThirdPartyInviteEve
/// A stripped-down version of the *m.room.topic* event.
pub type StrippedRoomTopic = StrippedStateContent<TopicEventContent>;
impl EventResultCompatible for StrippedState {
impl TryFromRaw for StrippedState {
type Raw = raw::StrippedState;
type Err = String;
fn try_from_raw(raw: raw::StrippedState) -> Result<Self, (Self::Err, Self::Raw)> {
use raw::StrippedState::*;
fn convert<T: EventResultCompatible>(
fn convert<T: TryFromRaw>(
raw_variant: fn(T::Raw) -> raw::StrippedState,
variant: fn(T) -> StrippedState,
raw: T::Raw,
@ -152,9 +152,9 @@ impl EventResultCompatible for StrippedState {
}
}
impl<C> EventResultCompatible for StrippedStateContent<C>
impl<C> TryFromRaw for StrippedStateContent<C>
where
C: EventResultCompatible,
C: TryFromRaw,
{
type Raw = StrippedStateContent<C::Raw>;
type Err = C::Err;