Simplify TryFromRaw::try_from_raw

This commit is contained in:
Jonas Platte 2019-11-24 17:40:13 +01:00
parent b9f5fd796e
commit cf5fccaccc
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
7 changed files with 160 additions and 201 deletions

View File

@ -1,5 +1,9 @@
# [unreleased] # [unreleased]
Breaking changes:
* `TryFromRaw::try_from_raw`'s signature has been simplified. The previous signature was a relict that was no longer sensible.
# 0.15.1 # 0.15.1
Bug fixes: Bug fixes:

View File

@ -341,65 +341,57 @@ impl TryFromRaw for Event {
type Raw = raw::Event; type Raw = raw::Event;
type Err = String; type Err = String;
fn try_from_raw(raw: raw::Event) -> Result<Self, (Self::Err, Self::Raw)> { fn try_from_raw(raw: raw::Event) -> Result<Self, Self::Err> {
use crate::util::try_convert_variant as conv; use crate::util::try_convert_variant as conv;
use raw::Event::*; use raw::Event::*;
match raw { match raw {
CallAnswer(c) => conv(CallAnswer, Event::CallAnswer, c), CallAnswer(c) => conv(Event::CallAnswer, c),
CallCandidates(c) => conv(CallCandidates, Event::CallCandidates, c), CallCandidates(c) => conv(Event::CallCandidates, c),
CallHangup(c) => conv(CallHangup, Event::CallHangup, c), CallHangup(c) => conv(Event::CallHangup, c),
CallInvite(c) => conv(CallInvite, Event::CallInvite, c), CallInvite(c) => conv(Event::CallInvite, c),
Direct(c) => conv(Direct, Event::Direct, c), Direct(c) => conv(Event::Direct, c),
Dummy(c) => conv(Dummy, Event::Dummy, c), Dummy(c) => conv(Event::Dummy, c),
ForwardedRoomKey(c) => conv(ForwardedRoomKey, Event::ForwardedRoomKey, c), ForwardedRoomKey(c) => conv(Event::ForwardedRoomKey, c),
FullyRead(c) => conv(FullyRead, Event::FullyRead, c), FullyRead(c) => conv(Event::FullyRead, c),
IgnoredUserList(c) => conv(IgnoredUserList, Event::IgnoredUserList, c), IgnoredUserList(c) => conv(Event::IgnoredUserList, c),
KeyVerificationAccept(c) => { KeyVerificationAccept(c) => conv(Event::KeyVerificationAccept, c),
conv(KeyVerificationAccept, Event::KeyVerificationAccept, c) KeyVerificationCancel(c) => conv(Event::KeyVerificationCancel, c),
} KeyVerificationKey(c) => conv(Event::KeyVerificationKey, c),
KeyVerificationCancel(c) => { KeyVerificationMac(c) => conv(Event::KeyVerificationMac, c),
conv(KeyVerificationCancel, Event::KeyVerificationCancel, c) KeyVerificationRequest(c) => conv(Event::KeyVerificationRequest, c),
} KeyVerificationStart(c) => conv(Event::KeyVerificationStart, c),
KeyVerificationKey(c) => conv(KeyVerificationKey, Event::KeyVerificationKey, c), Presence(c) => conv(Event::Presence, c),
KeyVerificationMac(c) => conv(KeyVerificationMac, Event::KeyVerificationMac, c), PushRules(c) => conv(Event::PushRules, c),
KeyVerificationRequest(c) => { Receipt(c) => conv(Event::Receipt, c),
conv(KeyVerificationRequest, Event::KeyVerificationRequest, c) RoomAliases(c) => conv(Event::RoomAliases, c),
} RoomAvatar(c) => conv(Event::RoomAvatar, c),
KeyVerificationStart(c) => conv(KeyVerificationStart, Event::KeyVerificationStart, c), RoomCanonicalAlias(c) => conv(Event::RoomCanonicalAlias, c),
Presence(c) => conv(Presence, Event::Presence, c), RoomCreate(c) => conv(Event::RoomCreate, c),
PushRules(c) => conv(PushRules, Event::PushRules, c), RoomEncrypted(c) => conv(Event::RoomEncrypted, c),
Receipt(c) => conv(Receipt, Event::Receipt, c), RoomEncryption(c) => conv(Event::RoomEncryption, c),
RoomAliases(c) => conv(RoomAliases, Event::RoomAliases, c), RoomGuestAccess(c) => conv(Event::RoomGuestAccess, c),
RoomAvatar(c) => conv(RoomAvatar, Event::RoomAvatar, c), RoomHistoryVisibility(c) => conv(Event::RoomHistoryVisibility, c),
RoomCanonicalAlias(c) => conv(RoomCanonicalAlias, Event::RoomCanonicalAlias, c), RoomJoinRules(c) => conv(Event::RoomJoinRules, c),
RoomCreate(c) => conv(RoomCreate, Event::RoomCreate, c), RoomMember(c) => conv(Event::RoomMember, c),
RoomEncrypted(c) => conv(RoomEncrypted, Event::RoomEncrypted, c), RoomMessage(c) => conv(Event::RoomMessage, c),
RoomEncryption(c) => conv(RoomEncryption, Event::RoomEncryption, c), RoomMessageFeedback(c) => conv(Event::RoomMessageFeedback, c),
RoomGuestAccess(c) => conv(RoomGuestAccess, Event::RoomGuestAccess, c), RoomName(c) => conv(Event::RoomName, c),
RoomHistoryVisibility(c) => { RoomPinnedEvents(c) => conv(Event::RoomPinnedEvents, c),
conv(RoomHistoryVisibility, Event::RoomHistoryVisibility, c) RoomPowerLevels(c) => conv(Event::RoomPowerLevels, c),
} RoomRedaction(c) => conv(Event::RoomRedaction, c),
RoomJoinRules(c) => conv(RoomJoinRules, Event::RoomJoinRules, c), RoomServerAcl(c) => conv(Event::RoomServerAcl, c),
RoomMember(c) => conv(RoomMember, Event::RoomMember, c), RoomThirdPartyInvite(c) => conv(Event::RoomThirdPartyInvite, c),
RoomMessage(c) => conv(RoomMessage, Event::RoomMessage, c), RoomTombstone(c) => conv(Event::RoomTombstone, c),
RoomMessageFeedback(c) => conv(RoomMessageFeedback, Event::RoomMessageFeedback, c), RoomTopic(c) => conv(Event::RoomTopic, c),
RoomName(c) => conv(RoomName, Event::RoomName, c), RoomKey(c) => conv(Event::RoomKey, c),
RoomPinnedEvents(c) => conv(RoomPinnedEvents, Event::RoomPinnedEvents, c), RoomKeyRequest(c) => conv(Event::RoomKeyRequest, c),
RoomPowerLevels(c) => conv(RoomPowerLevels, Event::RoomPowerLevels, c), Sticker(c) => conv(Event::Sticker, c),
RoomRedaction(c) => conv(RoomRedaction, Event::RoomRedaction, c), Tag(c) => conv(Event::Tag, c),
RoomServerAcl(c) => conv(RoomServerAcl, Event::RoomServerAcl, c), Typing(c) => conv(Event::Typing, c),
RoomThirdPartyInvite(c) => conv(RoomThirdPartyInvite, Event::RoomThirdPartyInvite, c), Custom(c) => conv(Event::Custom, c),
RoomTombstone(c) => conv(RoomTombstone, Event::RoomTombstone, c), CustomRoom(c) => conv(Event::CustomRoom, c),
RoomTopic(c) => conv(RoomTopic, Event::RoomTopic, c), CustomState(c) => conv(Event::CustomState, c),
RoomKey(c) => conv(RoomKey, Event::RoomKey, c),
RoomKeyRequest(c) => conv(RoomKeyRequest, Event::RoomKeyRequest, c),
Sticker(c) => conv(Sticker, Event::Sticker, c),
Tag(c) => conv(Tag, Event::Tag, c),
Typing(c) => conv(Typing, Event::Typing, c),
Custom(c) => conv(Custom, Event::Custom, c),
CustomRoom(c) => conv(CustomRoom, Event::CustomRoom, c),
CustomState(c) => conv(CustomState, Event::CustomState, c),
} }
} }
} }
@ -408,42 +400,38 @@ impl TryFromRaw for RoomEvent {
type Raw = raw::RoomEvent; type Raw = raw::RoomEvent;
type Err = String; type Err = String;
fn try_from_raw(raw: raw::RoomEvent) -> Result<Self, (Self::Err, Self::Raw)> { fn try_from_raw(raw: raw::RoomEvent) -> Result<Self, Self::Err> {
use crate::util::try_convert_variant as conv; use crate::util::try_convert_variant as conv;
use raw::RoomEvent::*; use raw::RoomEvent::*;
match raw { match raw {
CallAnswer(c) => conv(CallAnswer, RoomEvent::CallAnswer, c), CallAnswer(c) => conv(RoomEvent::CallAnswer, c),
CallCandidates(c) => conv(CallCandidates, RoomEvent::CallCandidates, c), CallCandidates(c) => conv(RoomEvent::CallCandidates, c),
CallHangup(c) => conv(CallHangup, RoomEvent::CallHangup, c), CallHangup(c) => conv(RoomEvent::CallHangup, c),
CallInvite(c) => conv(CallInvite, RoomEvent::CallInvite, c), CallInvite(c) => conv(RoomEvent::CallInvite, c),
RoomAliases(c) => conv(RoomAliases, RoomEvent::RoomAliases, c), RoomAliases(c) => conv(RoomEvent::RoomAliases, c),
RoomAvatar(c) => conv(RoomAvatar, RoomEvent::RoomAvatar, c), RoomAvatar(c) => conv(RoomEvent::RoomAvatar, c),
RoomCanonicalAlias(c) => conv(RoomCanonicalAlias, RoomEvent::RoomCanonicalAlias, c), RoomCanonicalAlias(c) => conv(RoomEvent::RoomCanonicalAlias, c),
RoomCreate(c) => conv(RoomCreate, RoomEvent::RoomCreate, c), RoomCreate(c) => conv(RoomEvent::RoomCreate, c),
RoomEncrypted(c) => conv(RoomEncrypted, RoomEvent::RoomEncrypted, c), RoomEncrypted(c) => conv(RoomEvent::RoomEncrypted, c),
RoomEncryption(c) => conv(RoomEncryption, RoomEvent::RoomEncryption, c), RoomEncryption(c) => conv(RoomEvent::RoomEncryption, c),
RoomGuestAccess(c) => conv(RoomGuestAccess, RoomEvent::RoomGuestAccess, c), RoomGuestAccess(c) => conv(RoomEvent::RoomGuestAccess, c),
RoomHistoryVisibility(c) => { RoomHistoryVisibility(c) => conv(RoomEvent::RoomHistoryVisibility, c),
conv(RoomHistoryVisibility, RoomEvent::RoomHistoryVisibility, c) RoomJoinRules(c) => conv(RoomEvent::RoomJoinRules, c),
} RoomMember(c) => conv(RoomEvent::RoomMember, c),
RoomJoinRules(c) => conv(RoomJoinRules, RoomEvent::RoomJoinRules, c), RoomMessage(c) => conv(RoomEvent::RoomMessage, c),
RoomMember(c) => conv(RoomMember, RoomEvent::RoomMember, c), RoomMessageFeedback(c) => conv(RoomEvent::RoomMessageFeedback, c),
RoomMessage(c) => conv(RoomMessage, RoomEvent::RoomMessage, c), RoomName(c) => conv(RoomEvent::RoomName, c),
RoomMessageFeedback(c) => conv(RoomMessageFeedback, RoomEvent::RoomMessageFeedback, c), RoomPinnedEvents(c) => conv(RoomEvent::RoomPinnedEvents, c),
RoomName(c) => conv(RoomName, RoomEvent::RoomName, c), RoomPowerLevels(c) => conv(RoomEvent::RoomPowerLevels, c),
RoomPinnedEvents(c) => conv(RoomPinnedEvents, RoomEvent::RoomPinnedEvents, c), RoomRedaction(c) => conv(RoomEvent::RoomRedaction, c),
RoomPowerLevels(c) => conv(RoomPowerLevels, RoomEvent::RoomPowerLevels, c), RoomServerAcl(c) => conv(RoomEvent::RoomServerAcl, c),
RoomRedaction(c) => conv(RoomRedaction, RoomEvent::RoomRedaction, c), RoomThirdPartyInvite(c) => conv(RoomEvent::RoomThirdPartyInvite, c),
RoomServerAcl(c) => conv(RoomServerAcl, RoomEvent::RoomServerAcl, c), RoomTombstone(c) => conv(RoomEvent::RoomTombstone, c),
RoomThirdPartyInvite(c) => { RoomTopic(c) => conv(RoomEvent::RoomTopic, c),
conv(RoomThirdPartyInvite, RoomEvent::RoomThirdPartyInvite, c) Sticker(c) => conv(RoomEvent::Sticker, c),
} CustomRoom(c) => conv(RoomEvent::CustomRoom, c),
RoomTombstone(c) => conv(RoomTombstone, RoomEvent::RoomTombstone, c), CustomState(c) => conv(RoomEvent::CustomState, c),
RoomTopic(c) => conv(RoomTopic, RoomEvent::RoomTopic, c),
Sticker(c) => conv(Sticker, RoomEvent::Sticker, c),
CustomRoom(c) => conv(CustomRoom, RoomEvent::CustomRoom, c),
CustomState(c) => conv(CustomState, RoomEvent::CustomState, c),
} }
} }
} }
@ -452,32 +440,28 @@ impl TryFromRaw for StateEvent {
type Raw = raw::StateEvent; type Raw = raw::StateEvent;
type Err = String; type Err = String;
fn try_from_raw(raw: raw::StateEvent) -> Result<Self, (Self::Err, Self::Raw)> { fn try_from_raw(raw: raw::StateEvent) -> Result<Self, Self::Err> {
use crate::util::try_convert_variant as conv; use crate::util::try_convert_variant as conv;
use raw::StateEvent::*; use raw::StateEvent::*;
match raw { match raw {
RoomAliases(c) => conv(RoomAliases, StateEvent::RoomAliases, c), RoomAliases(c) => conv(StateEvent::RoomAliases, c),
RoomAvatar(c) => conv(RoomAvatar, StateEvent::RoomAvatar, c), RoomAvatar(c) => conv(StateEvent::RoomAvatar, c),
RoomCanonicalAlias(c) => conv(RoomCanonicalAlias, StateEvent::RoomCanonicalAlias, c), RoomCanonicalAlias(c) => conv(StateEvent::RoomCanonicalAlias, c),
RoomCreate(c) => conv(RoomCreate, StateEvent::RoomCreate, c), RoomCreate(c) => conv(StateEvent::RoomCreate, c),
RoomEncryption(c) => conv(RoomEncryption, StateEvent::RoomEncryption, c), RoomEncryption(c) => conv(StateEvent::RoomEncryption, c),
RoomGuestAccess(c) => conv(RoomGuestAccess, StateEvent::RoomGuestAccess, c), RoomGuestAccess(c) => conv(StateEvent::RoomGuestAccess, c),
RoomHistoryVisibility(c) => { RoomHistoryVisibility(c) => conv(StateEvent::RoomHistoryVisibility, c),
conv(RoomHistoryVisibility, StateEvent::RoomHistoryVisibility, c) RoomJoinRules(c) => conv(StateEvent::RoomJoinRules, c),
} RoomMember(c) => conv(StateEvent::RoomMember, c),
RoomJoinRules(c) => conv(RoomJoinRules, StateEvent::RoomJoinRules, c), RoomName(c) => conv(StateEvent::RoomName, c),
RoomMember(c) => conv(RoomMember, StateEvent::RoomMember, c), RoomPinnedEvents(c) => conv(StateEvent::RoomPinnedEvents, c),
RoomName(c) => conv(RoomName, StateEvent::RoomName, c), RoomPowerLevels(c) => conv(StateEvent::RoomPowerLevels, c),
RoomPinnedEvents(c) => conv(RoomPinnedEvents, StateEvent::RoomPinnedEvents, c), RoomServerAcl(c) => conv(StateEvent::RoomServerAcl, c),
RoomPowerLevels(c) => conv(RoomPowerLevels, StateEvent::RoomPowerLevels, c), RoomThirdPartyInvite(c) => conv(StateEvent::RoomThirdPartyInvite, c),
RoomServerAcl(c) => conv(RoomServerAcl, StateEvent::RoomServerAcl, c), RoomTombstone(c) => conv(StateEvent::RoomTombstone, c),
RoomThirdPartyInvite(c) => { RoomTopic(c) => conv(StateEvent::RoomTopic, c),
conv(RoomThirdPartyInvite, StateEvent::RoomThirdPartyInvite, c) CustomState(c) => conv(StateEvent::CustomState, c),
}
RoomTombstone(c) => conv(RoomTombstone, StateEvent::RoomTombstone, c),
RoomTopic(c) => conv(RoomTopic, StateEvent::RoomTopic, c),
CustomState(c) => conv(CustomState, StateEvent::CustomState, c),
} }
} }
} }

View File

@ -136,36 +136,30 @@ impl TryFromRaw for Event {
type Raw = raw::Event; type Raw = raw::Event;
type Err = String; type Err = String;
fn try_from_raw(raw: raw::Event) -> Result<Self, (Self::Err, Self::Raw)> { fn try_from_raw(raw: raw::Event) -> Result<Self, Self::Err> {
use crate::util::try_convert_variant as conv; use crate::util::try_convert_variant as conv;
use raw::Event::*; use raw::Event::*;
match raw { match raw {
Direct(c) => conv(Direct, Event::Direct, c), Direct(c) => conv(Event::Direct, c),
Dummy(c) => conv(Dummy, Event::Dummy, c), Dummy(c) => conv(Event::Dummy, c),
ForwardedRoomKey(c) => conv(ForwardedRoomKey, Event::ForwardedRoomKey, c), ForwardedRoomKey(c) => conv(Event::ForwardedRoomKey, c),
FullyRead(c) => conv(FullyRead, Event::FullyRead, c), FullyRead(c) => conv(Event::FullyRead, c),
KeyVerificationAccept(c) => { KeyVerificationAccept(c) => conv(Event::KeyVerificationAccept, c),
conv(KeyVerificationAccept, Event::KeyVerificationAccept, c) KeyVerificationCancel(c) => conv(Event::KeyVerificationCancel, c),
} KeyVerificationKey(c) => conv(Event::KeyVerificationKey, c),
KeyVerificationCancel(c) => { KeyVerificationMac(c) => conv(Event::KeyVerificationMac, c),
conv(KeyVerificationCancel, Event::KeyVerificationCancel, c) KeyVerificationRequest(c) => conv(Event::KeyVerificationRequest, c),
} KeyVerificationStart(c) => conv(Event::KeyVerificationStart, c),
KeyVerificationKey(c) => conv(KeyVerificationKey, Event::KeyVerificationKey, c), IgnoredUserList(c) => conv(Event::IgnoredUserList, c),
KeyVerificationMac(c) => conv(KeyVerificationMac, Event::KeyVerificationMac, c), Presence(c) => conv(Event::Presence, c),
KeyVerificationRequest(c) => { PushRules(c) => conv(Event::PushRules, c),
conv(KeyVerificationRequest, Event::KeyVerificationRequest, c) RoomKey(c) => conv(Event::RoomKey, c),
} RoomKeyRequest(c) => conv(Event::RoomKeyRequest, c),
KeyVerificationStart(c) => conv(KeyVerificationStart, Event::KeyVerificationStart, c), Receipt(c) => conv(Event::Receipt, c),
IgnoredUserList(c) => conv(IgnoredUserList, Event::IgnoredUserList, c), Tag(c) => conv(Event::Tag, c),
Presence(c) => conv(Presence, Event::Presence, c), Typing(c) => conv(Event::Typing, c),
PushRules(c) => conv(PushRules, Event::PushRules, c), Custom(c) => conv(Event::Custom, c),
RoomKey(c) => conv(RoomKey, Event::RoomKey, c),
RoomKeyRequest(c) => conv(RoomKeyRequest, Event::RoomKeyRequest, c),
Receipt(c) => conv(Receipt, Event::Receipt, c),
Tag(c) => conv(Tag, Event::Tag, c),
Typing(c) => conv(Typing, Event::Typing, c),
Custom(c) => conv(Custom, Event::Custom, c),
} }
} }
} }
@ -174,21 +168,21 @@ impl TryFromRaw for RoomEvent {
type Raw = raw::RoomEvent; type Raw = raw::RoomEvent;
type Err = String; type Err = String;
fn try_from_raw(raw: raw::RoomEvent) -> Result<Self, (Self::Err, Self::Raw)> { fn try_from_raw(raw: raw::RoomEvent) -> Result<Self, Self::Err> {
use crate::util::try_convert_variant as conv; use crate::util::try_convert_variant as conv;
use raw::RoomEvent::*; use raw::RoomEvent::*;
match raw { match raw {
CallAnswer(c) => conv(CallAnswer, RoomEvent::CallAnswer, c), CallAnswer(c) => conv(RoomEvent::CallAnswer, c),
CallCandidates(c) => conv(CallCandidates, RoomEvent::CallCandidates, c), CallCandidates(c) => conv(RoomEvent::CallCandidates, c),
CallHangup(c) => conv(CallHangup, RoomEvent::CallHangup, c), CallHangup(c) => conv(RoomEvent::CallHangup, c),
CallInvite(c) => conv(CallInvite, RoomEvent::CallInvite, c), CallInvite(c) => conv(RoomEvent::CallInvite, c),
RoomEncrypted(c) => conv(RoomEncrypted, RoomEvent::RoomEncrypted, c), RoomEncrypted(c) => conv(RoomEvent::RoomEncrypted, c),
RoomMessage(c) => conv(RoomMessage, RoomEvent::RoomMessage, c), RoomMessage(c) => conv(RoomEvent::RoomMessage, c),
RoomMessageFeedback(c) => conv(RoomMessageFeedback, RoomEvent::RoomMessageFeedback, c), RoomMessageFeedback(c) => conv(RoomEvent::RoomMessageFeedback, c),
RoomRedaction(c) => conv(RoomRedaction, RoomEvent::RoomRedaction, c), RoomRedaction(c) => conv(RoomEvent::RoomRedaction, c),
Sticker(c) => conv(Sticker, RoomEvent::Sticker, c), Sticker(c) => conv(RoomEvent::Sticker, c),
CustomRoom(c) => conv(CustomRoom, RoomEvent::CustomRoom, c), CustomRoom(c) => conv(RoomEvent::CustomRoom, c),
} }
} }
} }

View File

@ -35,11 +35,8 @@ impl TryFromRaw for StartEvent {
type Raw = raw::StartEvent; type Raw = raw::StartEvent;
type Err = &'static str; type Err = &'static str;
fn try_from_raw(raw: raw::StartEvent) -> Result<Self, (Self::Err, Self::Raw)> { fn try_from_raw(raw: raw::StartEvent) -> Result<Self, Self::Err> {
match StartEventContent::try_from_raw(raw.content) { StartEventContent::try_from_raw(raw.content).map(|content| Self { content })
Ok(content) => Ok(Self { content }),
Err((msg, content)) => Err((msg, raw::StartEvent { content })),
}
} }
} }
@ -67,44 +64,38 @@ impl TryFromRaw for StartEventContent {
type Raw = raw::StartEventContent; type Raw = raw::StartEventContent;
type Err = &'static str; type Err = &'static str;
fn try_from_raw(raw: raw::StartEventContent) -> Result<Self, (Self::Err, Self::Raw)> { fn try_from_raw(raw: raw::StartEventContent) -> Result<Self, Self::Err> {
match raw { match raw {
raw::StartEventContent::MSasV1(content) => { raw::StartEventContent::MSasV1(content) => {
if !content if !content
.key_agreement_protocols .key_agreement_protocols
.contains(&KeyAgreementProtocol::Curve25519) .contains(&KeyAgreementProtocol::Curve25519)
{ {
return Err(( return Err(
"`key_agreement_protocols` must contain at least `KeyAgreementProtocol::Curve25519`", "`key_agreement_protocols` must contain at least `KeyAgreementProtocol::Curve25519`"
raw::StartEventContent::MSasV1(content), );
));
} }
if !content.hashes.contains(&HashAlgorithm::Sha256) { if !content.hashes.contains(&HashAlgorithm::Sha256) {
return Err(( return Err("`hashes` must contain at least `HashAlgorithm::Sha256`");
"`hashes` must contain at least `HashAlgorithm::Sha256`",
raw::StartEventContent::MSasV1(content),
));
} }
if !content if !content
.message_authentication_codes .message_authentication_codes
.contains(&MessageAuthenticationCode::HkdfHmacSha256) .contains(&MessageAuthenticationCode::HkdfHmacSha256)
{ {
return Err(( return Err(
"`message_authentication_codes` must contain at least `MessageAuthenticationCode::HkdfHmacSha256`", "`message_authentication_codes` must contain at least `MessageAuthenticationCode::HkdfHmacSha256`"
raw::StartEventContent::MSasV1(content), );
));
} }
if !content if !content
.short_authentication_string .short_authentication_string
.contains(&ShortAuthenticationString::Decimal) .contains(&ShortAuthenticationString::Decimal)
{ {
return Err(( return Err(
"`short_authentication_string` must contain at least `ShortAuthenticationString::Decimal`", "`short_authentication_string` must contain at least `ShortAuthenticationString::Decimal`",
raw::StartEventContent::MSasV1(content), );
));
} }
Ok(StartEventContent::MSasV1(content)) Ok(StartEventContent::MSasV1(content))

View File

@ -250,14 +250,14 @@ pub trait TryFromRaw: Sized {
type Err: Display; type Err: Display;
/// Tries to convert the raw type to `Self`. /// Tries to convert the raw type to `Self`.
fn try_from_raw(_: Self::Raw) -> Result<Self, (Self::Err, Self::Raw)>; fn try_from_raw(_: Self::Raw) -> Result<Self, Self::Err>;
} }
impl<T: FromRaw> TryFromRaw for T { impl<T: FromRaw> TryFromRaw for T {
type Raw = <T as FromRaw>::Raw; type Raw = <T as FromRaw>::Raw;
type Err = Infallible; type Err = Infallible;
fn try_from_raw(raw: Self::Raw) -> Result<Self, (Self::Err, Self::Raw)> { fn try_from_raw(raw: Self::Raw) -> Result<Self, Self::Err> {
Ok(Self::from_raw(raw)) Ok(Self::from_raw(raw))
} }
} }
@ -312,7 +312,7 @@ where
match T::try_from_raw(raw_data) { match T::try_from_raw(raw_data) {
Ok(value) => Ok(EventResult::Ok(value)), Ok(value) => Ok(EventResult::Ok(value)),
Err((err, _)) => Ok(EventResult::Err(InvalidEvent { Err(err) => Ok(EventResult::Err(InvalidEvent {
message: err.to_string(), message: err.to_string(),
json, json,
kind: InvalidEventKind::Validation, kind: InvalidEventKind::Validation,

View File

@ -122,29 +122,23 @@ impl TryFromRaw for StrippedState {
type Raw = raw::StrippedState; type Raw = raw::StrippedState;
type Err = String; type Err = String;
fn try_from_raw(raw: raw::StrippedState) -> Result<Self, (Self::Err, Self::Raw)> { fn try_from_raw(raw: raw::StrippedState) -> Result<Self, Self::Err> {
use crate::util::try_convert_variant as conv; use crate::util::try_convert_variant as conv;
use raw::StrippedState::*; use raw::StrippedState::*;
match raw { match raw {
RoomAliases(c) => conv(RoomAliases, StrippedState::RoomAliases, c), RoomAliases(c) => conv(StrippedState::RoomAliases, c),
RoomAvatar(c) => conv(RoomAvatar, StrippedState::RoomAvatar, c), RoomAvatar(c) => conv(StrippedState::RoomAvatar, c),
RoomCanonicalAlias(c) => conv(RoomCanonicalAlias, StrippedState::RoomCanonicalAlias, c), RoomCanonicalAlias(c) => conv(StrippedState::RoomCanonicalAlias, c),
RoomCreate(c) => conv(RoomCreate, StrippedState::RoomCreate, c), RoomCreate(c) => conv(StrippedState::RoomCreate, c),
RoomGuestAccess(c) => conv(RoomGuestAccess, StrippedState::RoomGuestAccess, c), RoomGuestAccess(c) => conv(StrippedState::RoomGuestAccess, c),
RoomHistoryVisibility(c) => conv( RoomHistoryVisibility(c) => conv(StrippedState::RoomHistoryVisibility, c),
RoomHistoryVisibility, RoomJoinRules(c) => conv(StrippedState::RoomJoinRules, c),
StrippedState::RoomHistoryVisibility, RoomMember(c) => conv(StrippedState::RoomMember, c),
c, RoomName(c) => conv(StrippedState::RoomName, c),
), RoomPowerLevels(c) => conv(StrippedState::RoomPowerLevels, c),
RoomJoinRules(c) => conv(RoomJoinRules, StrippedState::RoomJoinRules, c), RoomThirdPartyInvite(c) => conv(StrippedState::RoomThirdPartyInvite, c),
RoomMember(c) => conv(RoomMember, StrippedState::RoomMember, c), RoomTopic(c) => conv(StrippedState::RoomTopic, c),
RoomName(c) => conv(RoomName, StrippedState::RoomName, c),
RoomPowerLevels(c) => conv(RoomPowerLevels, StrippedState::RoomPowerLevels, c),
RoomThirdPartyInvite(c) => {
conv(RoomThirdPartyInvite, StrippedState::RoomThirdPartyInvite, c)
}
RoomTopic(c) => conv(RoomTopic, StrippedState::RoomTopic, c),
} }
} }
} }
@ -156,16 +150,9 @@ where
type Raw = StrippedStateContent<C::Raw>; type Raw = StrippedStateContent<C::Raw>;
type Err = C::Err; type Err = C::Err;
fn try_from_raw(mut raw: StrippedStateContent<C::Raw>) -> Result<Self, (Self::Err, Self::Raw)> { fn try_from_raw(raw: StrippedStateContent<C::Raw>) -> Result<Self, Self::Err> {
Ok(Self { Ok(Self {
content: match C::try_from_raw(raw.content) { content: C::try_from_raw(raw.content)?,
Ok(c) => c,
Err((msg, raw_content)) => {
// we moved raw.content, so we need to put it back before returning raw
raw.content = raw_content;
return Err((msg, raw));
}
},
event_type: raw.event_type, event_type: raw.event_type,
state_key: raw.state_key, state_key: raw.state_key,
sender: raw.sender, sender: raw.sender,

View File

@ -4,13 +4,12 @@ use serde_json::Value;
use crate::TryFromRaw; use crate::TryFromRaw;
pub fn try_convert_variant<Enum: TryFromRaw, Content: TryFromRaw>( pub fn try_convert_variant<Enum: TryFromRaw, Content: TryFromRaw>(
raw_variant: fn(Content::Raw) -> Enum::Raw,
variant: fn(Content) -> Enum, variant: fn(Content) -> Enum,
raw: Content::Raw, raw: Content::Raw,
) -> Result<Enum, (String, Enum::Raw)> { ) -> Result<Enum, String> {
Content::try_from_raw(raw) Content::try_from_raw(raw)
.map(variant) .map(variant)
.map_err(|(err, raw)| (err.to_string(), raw_variant(raw))) .map_err(|err| err.to_string())
} }
pub fn try_variant_from_value<T, U, E>(value: Value, variant: fn(T) -> U) -> Result<U, E> pub fn try_variant_from_value<T, U, E>(value: Value, variant: fn(T) -> U) -> Result<U, E>