diff --git a/CHANGELOG.md b/CHANGELOG.md index bb6fcdda..71f5dfbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # [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 Bug fixes: diff --git a/src/collections/all.rs b/src/collections/all.rs index 0be21c5b..482ad296 100644 --- a/src/collections/all.rs +++ b/src/collections/all.rs @@ -341,65 +341,57 @@ impl TryFromRaw for Event { type Raw = raw::Event; type Err = String; - fn try_from_raw(raw: raw::Event) -> Result { + fn try_from_raw(raw: raw::Event) -> Result { use crate::util::try_convert_variant as conv; use raw::Event::*; match raw { - CallAnswer(c) => conv(CallAnswer, Event::CallAnswer, c), - CallCandidates(c) => conv(CallCandidates, Event::CallCandidates, c), - CallHangup(c) => conv(CallHangup, Event::CallHangup, c), - CallInvite(c) => conv(CallInvite, Event::CallInvite, c), - Direct(c) => conv(Direct, Event::Direct, c), - Dummy(c) => conv(Dummy, Event::Dummy, c), - ForwardedRoomKey(c) => conv(ForwardedRoomKey, Event::ForwardedRoomKey, c), - FullyRead(c) => conv(FullyRead, Event::FullyRead, c), - IgnoredUserList(c) => conv(IgnoredUserList, Event::IgnoredUserList, c), - KeyVerificationAccept(c) => { - conv(KeyVerificationAccept, Event::KeyVerificationAccept, c) - } - KeyVerificationCancel(c) => { - conv(KeyVerificationCancel, Event::KeyVerificationCancel, c) - } - KeyVerificationKey(c) => conv(KeyVerificationKey, Event::KeyVerificationKey, c), - KeyVerificationMac(c) => conv(KeyVerificationMac, Event::KeyVerificationMac, c), - KeyVerificationRequest(c) => { - conv(KeyVerificationRequest, Event::KeyVerificationRequest, c) - } - KeyVerificationStart(c) => conv(KeyVerificationStart, Event::KeyVerificationStart, c), - Presence(c) => conv(Presence, Event::Presence, c), - PushRules(c) => conv(PushRules, Event::PushRules, c), - Receipt(c) => conv(Receipt, Event::Receipt, c), - RoomAliases(c) => conv(RoomAliases, Event::RoomAliases, c), - RoomAvatar(c) => conv(RoomAvatar, Event::RoomAvatar, c), - RoomCanonicalAlias(c) => conv(RoomCanonicalAlias, Event::RoomCanonicalAlias, c), - RoomCreate(c) => conv(RoomCreate, Event::RoomCreate, c), - RoomEncrypted(c) => conv(RoomEncrypted, Event::RoomEncrypted, c), - RoomEncryption(c) => conv(RoomEncryption, Event::RoomEncryption, c), - RoomGuestAccess(c) => conv(RoomGuestAccess, Event::RoomGuestAccess, c), - RoomHistoryVisibility(c) => { - conv(RoomHistoryVisibility, Event::RoomHistoryVisibility, c) - } - RoomJoinRules(c) => conv(RoomJoinRules, Event::RoomJoinRules, c), - RoomMember(c) => conv(RoomMember, Event::RoomMember, c), - RoomMessage(c) => conv(RoomMessage, Event::RoomMessage, c), - RoomMessageFeedback(c) => conv(RoomMessageFeedback, Event::RoomMessageFeedback, c), - RoomName(c) => conv(RoomName, Event::RoomName, c), - RoomPinnedEvents(c) => conv(RoomPinnedEvents, Event::RoomPinnedEvents, c), - RoomPowerLevels(c) => conv(RoomPowerLevels, Event::RoomPowerLevels, c), - RoomRedaction(c) => conv(RoomRedaction, Event::RoomRedaction, c), - RoomServerAcl(c) => conv(RoomServerAcl, Event::RoomServerAcl, c), - RoomThirdPartyInvite(c) => conv(RoomThirdPartyInvite, Event::RoomThirdPartyInvite, c), - RoomTombstone(c) => conv(RoomTombstone, Event::RoomTombstone, c), - RoomTopic(c) => conv(RoomTopic, Event::RoomTopic, 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), + CallAnswer(c) => conv(Event::CallAnswer, c), + CallCandidates(c) => conv(Event::CallCandidates, c), + CallHangup(c) => conv(Event::CallHangup, c), + CallInvite(c) => conv(Event::CallInvite, c), + Direct(c) => conv(Event::Direct, c), + Dummy(c) => conv(Event::Dummy, c), + ForwardedRoomKey(c) => conv(Event::ForwardedRoomKey, c), + FullyRead(c) => conv(Event::FullyRead, c), + IgnoredUserList(c) => conv(Event::IgnoredUserList, c), + KeyVerificationAccept(c) => conv(Event::KeyVerificationAccept, c), + KeyVerificationCancel(c) => conv(Event::KeyVerificationCancel, c), + KeyVerificationKey(c) => conv(Event::KeyVerificationKey, c), + KeyVerificationMac(c) => conv(Event::KeyVerificationMac, c), + KeyVerificationRequest(c) => conv(Event::KeyVerificationRequest, c), + KeyVerificationStart(c) => conv(Event::KeyVerificationStart, c), + Presence(c) => conv(Event::Presence, c), + PushRules(c) => conv(Event::PushRules, c), + Receipt(c) => conv(Event::Receipt, c), + RoomAliases(c) => conv(Event::RoomAliases, c), + RoomAvatar(c) => conv(Event::RoomAvatar, c), + RoomCanonicalAlias(c) => conv(Event::RoomCanonicalAlias, c), + RoomCreate(c) => conv(Event::RoomCreate, c), + RoomEncrypted(c) => conv(Event::RoomEncrypted, c), + RoomEncryption(c) => conv(Event::RoomEncryption, c), + RoomGuestAccess(c) => conv(Event::RoomGuestAccess, c), + RoomHistoryVisibility(c) => conv(Event::RoomHistoryVisibility, c), + RoomJoinRules(c) => conv(Event::RoomJoinRules, c), + RoomMember(c) => conv(Event::RoomMember, c), + RoomMessage(c) => conv(Event::RoomMessage, c), + RoomMessageFeedback(c) => conv(Event::RoomMessageFeedback, c), + RoomName(c) => conv(Event::RoomName, c), + RoomPinnedEvents(c) => conv(Event::RoomPinnedEvents, c), + RoomPowerLevels(c) => conv(Event::RoomPowerLevels, c), + RoomRedaction(c) => conv(Event::RoomRedaction, c), + RoomServerAcl(c) => conv(Event::RoomServerAcl, c), + RoomThirdPartyInvite(c) => conv(Event::RoomThirdPartyInvite, c), + RoomTombstone(c) => conv(Event::RoomTombstone, c), + RoomTopic(c) => conv(Event::RoomTopic, c), + RoomKey(c) => conv(Event::RoomKey, c), + RoomKeyRequest(c) => conv(Event::RoomKeyRequest, c), + Sticker(c) => conv(Event::Sticker, c), + Tag(c) => conv(Event::Tag, c), + Typing(c) => conv(Event::Typing, c), + Custom(c) => conv(Event::Custom, c), + CustomRoom(c) => conv(Event::CustomRoom, c), + CustomState(c) => conv(Event::CustomState, c), } } } @@ -408,42 +400,38 @@ impl TryFromRaw for RoomEvent { type Raw = raw::RoomEvent; type Err = String; - fn try_from_raw(raw: raw::RoomEvent) -> Result { + fn try_from_raw(raw: raw::RoomEvent) -> Result { use crate::util::try_convert_variant as conv; use raw::RoomEvent::*; match raw { - CallAnswer(c) => conv(CallAnswer, RoomEvent::CallAnswer, c), - CallCandidates(c) => conv(CallCandidates, RoomEvent::CallCandidates, c), - CallHangup(c) => conv(CallHangup, RoomEvent::CallHangup, c), - CallInvite(c) => conv(CallInvite, RoomEvent::CallInvite, c), - RoomAliases(c) => conv(RoomAliases, RoomEvent::RoomAliases, c), - RoomAvatar(c) => conv(RoomAvatar, RoomEvent::RoomAvatar, c), - RoomCanonicalAlias(c) => conv(RoomCanonicalAlias, RoomEvent::RoomCanonicalAlias, c), - RoomCreate(c) => conv(RoomCreate, RoomEvent::RoomCreate, c), - RoomEncrypted(c) => conv(RoomEncrypted, RoomEvent::RoomEncrypted, c), - RoomEncryption(c) => conv(RoomEncryption, RoomEvent::RoomEncryption, c), - RoomGuestAccess(c) => conv(RoomGuestAccess, RoomEvent::RoomGuestAccess, c), - RoomHistoryVisibility(c) => { - conv(RoomHistoryVisibility, RoomEvent::RoomHistoryVisibility, c) - } - RoomJoinRules(c) => conv(RoomJoinRules, RoomEvent::RoomJoinRules, c), - RoomMember(c) => conv(RoomMember, RoomEvent::RoomMember, c), - RoomMessage(c) => conv(RoomMessage, RoomEvent::RoomMessage, c), - RoomMessageFeedback(c) => conv(RoomMessageFeedback, RoomEvent::RoomMessageFeedback, c), - RoomName(c) => conv(RoomName, RoomEvent::RoomName, c), - RoomPinnedEvents(c) => conv(RoomPinnedEvents, RoomEvent::RoomPinnedEvents, c), - RoomPowerLevels(c) => conv(RoomPowerLevels, RoomEvent::RoomPowerLevels, c), - RoomRedaction(c) => conv(RoomRedaction, RoomEvent::RoomRedaction, c), - RoomServerAcl(c) => conv(RoomServerAcl, RoomEvent::RoomServerAcl, c), - RoomThirdPartyInvite(c) => { - conv(RoomThirdPartyInvite, RoomEvent::RoomThirdPartyInvite, c) - } - RoomTombstone(c) => conv(RoomTombstone, RoomEvent::RoomTombstone, 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), + CallAnswer(c) => conv(RoomEvent::CallAnswer, c), + CallCandidates(c) => conv(RoomEvent::CallCandidates, c), + CallHangup(c) => conv(RoomEvent::CallHangup, c), + CallInvite(c) => conv(RoomEvent::CallInvite, c), + RoomAliases(c) => conv(RoomEvent::RoomAliases, c), + RoomAvatar(c) => conv(RoomEvent::RoomAvatar, c), + RoomCanonicalAlias(c) => conv(RoomEvent::RoomCanonicalAlias, c), + RoomCreate(c) => conv(RoomEvent::RoomCreate, c), + RoomEncrypted(c) => conv(RoomEvent::RoomEncrypted, c), + RoomEncryption(c) => conv(RoomEvent::RoomEncryption, c), + RoomGuestAccess(c) => conv(RoomEvent::RoomGuestAccess, c), + RoomHistoryVisibility(c) => conv(RoomEvent::RoomHistoryVisibility, c), + RoomJoinRules(c) => conv(RoomEvent::RoomJoinRules, c), + RoomMember(c) => conv(RoomEvent::RoomMember, c), + RoomMessage(c) => conv(RoomEvent::RoomMessage, c), + RoomMessageFeedback(c) => conv(RoomEvent::RoomMessageFeedback, c), + RoomName(c) => conv(RoomEvent::RoomName, c), + RoomPinnedEvents(c) => conv(RoomEvent::RoomPinnedEvents, c), + RoomPowerLevels(c) => conv(RoomEvent::RoomPowerLevels, c), + RoomRedaction(c) => conv(RoomEvent::RoomRedaction, c), + RoomServerAcl(c) => conv(RoomEvent::RoomServerAcl, c), + RoomThirdPartyInvite(c) => conv(RoomEvent::RoomThirdPartyInvite, c), + RoomTombstone(c) => conv(RoomEvent::RoomTombstone, c), + RoomTopic(c) => conv(RoomEvent::RoomTopic, c), + Sticker(c) => conv(RoomEvent::Sticker, c), + CustomRoom(c) => conv(RoomEvent::CustomRoom, c), + CustomState(c) => conv(RoomEvent::CustomState, c), } } } @@ -452,32 +440,28 @@ impl TryFromRaw for StateEvent { type Raw = raw::StateEvent; type Err = String; - fn try_from_raw(raw: raw::StateEvent) -> Result { + fn try_from_raw(raw: raw::StateEvent) -> Result { use crate::util::try_convert_variant as conv; use raw::StateEvent::*; match raw { - RoomAliases(c) => conv(RoomAliases, StateEvent::RoomAliases, c), - RoomAvatar(c) => conv(RoomAvatar, StateEvent::RoomAvatar, c), - RoomCanonicalAlias(c) => conv(RoomCanonicalAlias, StateEvent::RoomCanonicalAlias, c), - RoomCreate(c) => conv(RoomCreate, StateEvent::RoomCreate, c), - RoomEncryption(c) => conv(RoomEncryption, StateEvent::RoomEncryption, c), - RoomGuestAccess(c) => conv(RoomGuestAccess, StateEvent::RoomGuestAccess, c), - RoomHistoryVisibility(c) => { - conv(RoomHistoryVisibility, StateEvent::RoomHistoryVisibility, c) - } - RoomJoinRules(c) => conv(RoomJoinRules, StateEvent::RoomJoinRules, c), - RoomMember(c) => conv(RoomMember, StateEvent::RoomMember, c), - RoomName(c) => conv(RoomName, StateEvent::RoomName, c), - RoomPinnedEvents(c) => conv(RoomPinnedEvents, StateEvent::RoomPinnedEvents, c), - RoomPowerLevels(c) => conv(RoomPowerLevels, StateEvent::RoomPowerLevels, c), - RoomServerAcl(c) => conv(RoomServerAcl, StateEvent::RoomServerAcl, c), - RoomThirdPartyInvite(c) => { - conv(RoomThirdPartyInvite, StateEvent::RoomThirdPartyInvite, c) - } - RoomTombstone(c) => conv(RoomTombstone, StateEvent::RoomTombstone, c), - RoomTopic(c) => conv(RoomTopic, StateEvent::RoomTopic, c), - CustomState(c) => conv(CustomState, StateEvent::CustomState, c), + RoomAliases(c) => conv(StateEvent::RoomAliases, c), + RoomAvatar(c) => conv(StateEvent::RoomAvatar, c), + RoomCanonicalAlias(c) => conv(StateEvent::RoomCanonicalAlias, c), + RoomCreate(c) => conv(StateEvent::RoomCreate, c), + RoomEncryption(c) => conv(StateEvent::RoomEncryption, c), + RoomGuestAccess(c) => conv(StateEvent::RoomGuestAccess, c), + RoomHistoryVisibility(c) => conv(StateEvent::RoomHistoryVisibility, c), + RoomJoinRules(c) => conv(StateEvent::RoomJoinRules, c), + RoomMember(c) => conv(StateEvent::RoomMember, c), + RoomName(c) => conv(StateEvent::RoomName, c), + RoomPinnedEvents(c) => conv(StateEvent::RoomPinnedEvents, c), + RoomPowerLevels(c) => conv(StateEvent::RoomPowerLevels, c), + RoomServerAcl(c) => conv(StateEvent::RoomServerAcl, c), + RoomThirdPartyInvite(c) => conv(StateEvent::RoomThirdPartyInvite, c), + RoomTombstone(c) => conv(StateEvent::RoomTombstone, c), + RoomTopic(c) => conv(StateEvent::RoomTopic, c), + CustomState(c) => conv(StateEvent::CustomState, c), } } } diff --git a/src/collections/only.rs b/src/collections/only.rs index d382d2ea..141e63b7 100644 --- a/src/collections/only.rs +++ b/src/collections/only.rs @@ -136,36 +136,30 @@ impl TryFromRaw for Event { type Raw = raw::Event; type Err = String; - fn try_from_raw(raw: raw::Event) -> Result { + fn try_from_raw(raw: raw::Event) -> Result { use crate::util::try_convert_variant as conv; use raw::Event::*; match raw { - Direct(c) => conv(Direct, Event::Direct, c), - Dummy(c) => conv(Dummy, Event::Dummy, c), - ForwardedRoomKey(c) => conv(ForwardedRoomKey, Event::ForwardedRoomKey, c), - FullyRead(c) => conv(FullyRead, Event::FullyRead, c), - KeyVerificationAccept(c) => { - conv(KeyVerificationAccept, Event::KeyVerificationAccept, c) - } - KeyVerificationCancel(c) => { - conv(KeyVerificationCancel, Event::KeyVerificationCancel, c) - } - KeyVerificationKey(c) => conv(KeyVerificationKey, Event::KeyVerificationKey, c), - KeyVerificationMac(c) => conv(KeyVerificationMac, Event::KeyVerificationMac, c), - KeyVerificationRequest(c) => { - conv(KeyVerificationRequest, Event::KeyVerificationRequest, c) - } - KeyVerificationStart(c) => conv(KeyVerificationStart, Event::KeyVerificationStart, c), - IgnoredUserList(c) => conv(IgnoredUserList, Event::IgnoredUserList, c), - Presence(c) => conv(Presence, Event::Presence, c), - PushRules(c) => conv(PushRules, Event::PushRules, 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), + Direct(c) => conv(Event::Direct, c), + Dummy(c) => conv(Event::Dummy, c), + ForwardedRoomKey(c) => conv(Event::ForwardedRoomKey, c), + FullyRead(c) => conv(Event::FullyRead, c), + KeyVerificationAccept(c) => conv(Event::KeyVerificationAccept, c), + KeyVerificationCancel(c) => conv(Event::KeyVerificationCancel, c), + KeyVerificationKey(c) => conv(Event::KeyVerificationKey, c), + KeyVerificationMac(c) => conv(Event::KeyVerificationMac, c), + KeyVerificationRequest(c) => conv(Event::KeyVerificationRequest, c), + KeyVerificationStart(c) => conv(Event::KeyVerificationStart, c), + IgnoredUserList(c) => conv(Event::IgnoredUserList, c), + Presence(c) => conv(Event::Presence, c), + PushRules(c) => conv(Event::PushRules, c), + RoomKey(c) => conv(Event::RoomKey, c), + RoomKeyRequest(c) => conv(Event::RoomKeyRequest, c), + Receipt(c) => conv(Event::Receipt, c), + Tag(c) => conv(Event::Tag, c), + Typing(c) => conv(Event::Typing, c), + Custom(c) => conv(Event::Custom, c), } } } @@ -174,21 +168,21 @@ impl TryFromRaw for RoomEvent { type Raw = raw::RoomEvent; type Err = String; - fn try_from_raw(raw: raw::RoomEvent) -> Result { + fn try_from_raw(raw: raw::RoomEvent) -> Result { use crate::util::try_convert_variant as conv; use raw::RoomEvent::*; match raw { - CallAnswer(c) => conv(CallAnswer, RoomEvent::CallAnswer, c), - CallCandidates(c) => conv(CallCandidates, RoomEvent::CallCandidates, c), - CallHangup(c) => conv(CallHangup, RoomEvent::CallHangup, c), - CallInvite(c) => conv(CallInvite, RoomEvent::CallInvite, c), - RoomEncrypted(c) => conv(RoomEncrypted, RoomEvent::RoomEncrypted, c), - RoomMessage(c) => conv(RoomMessage, RoomEvent::RoomMessage, c), - RoomMessageFeedback(c) => conv(RoomMessageFeedback, RoomEvent::RoomMessageFeedback, c), - RoomRedaction(c) => conv(RoomRedaction, RoomEvent::RoomRedaction, c), - Sticker(c) => conv(Sticker, RoomEvent::Sticker, c), - CustomRoom(c) => conv(CustomRoom, RoomEvent::CustomRoom, c), + CallAnswer(c) => conv(RoomEvent::CallAnswer, c), + CallCandidates(c) => conv(RoomEvent::CallCandidates, c), + CallHangup(c) => conv(RoomEvent::CallHangup, c), + CallInvite(c) => conv(RoomEvent::CallInvite, c), + RoomEncrypted(c) => conv(RoomEvent::RoomEncrypted, c), + RoomMessage(c) => conv(RoomEvent::RoomMessage, c), + RoomMessageFeedback(c) => conv(RoomEvent::RoomMessageFeedback, c), + RoomRedaction(c) => conv(RoomEvent::RoomRedaction, c), + Sticker(c) => conv(RoomEvent::Sticker, c), + CustomRoom(c) => conv(RoomEvent::CustomRoom, c), } } } diff --git a/src/key/verification/start.rs b/src/key/verification/start.rs index 53befab0..02631fc6 100644 --- a/src/key/verification/start.rs +++ b/src/key/verification/start.rs @@ -35,11 +35,8 @@ impl TryFromRaw for StartEvent { type Raw = raw::StartEvent; type Err = &'static str; - fn try_from_raw(raw: raw::StartEvent) -> Result { - match StartEventContent::try_from_raw(raw.content) { - Ok(content) => Ok(Self { content }), - Err((msg, content)) => Err((msg, raw::StartEvent { content })), - } + fn try_from_raw(raw: raw::StartEvent) -> Result { + StartEventContent::try_from_raw(raw.content).map(|content| Self { content }) } } @@ -67,44 +64,38 @@ impl TryFromRaw for StartEventContent { type Raw = raw::StartEventContent; type Err = &'static str; - fn try_from_raw(raw: raw::StartEventContent) -> Result { + fn try_from_raw(raw: raw::StartEventContent) -> Result { match raw { raw::StartEventContent::MSasV1(content) => { if !content .key_agreement_protocols .contains(&KeyAgreementProtocol::Curve25519) { - return Err(( - "`key_agreement_protocols` must contain at least `KeyAgreementProtocol::Curve25519`", - raw::StartEventContent::MSasV1(content), - )); + return Err( + "`key_agreement_protocols` must contain at least `KeyAgreementProtocol::Curve25519`" + ); } if !content.hashes.contains(&HashAlgorithm::Sha256) { - return Err(( - "`hashes` must contain at least `HashAlgorithm::Sha256`", - raw::StartEventContent::MSasV1(content), - )); + return Err("`hashes` must contain at least `HashAlgorithm::Sha256`"); } if !content .message_authentication_codes .contains(&MessageAuthenticationCode::HkdfHmacSha256) { - return Err(( - "`message_authentication_codes` must contain at least `MessageAuthenticationCode::HkdfHmacSha256`", - raw::StartEventContent::MSasV1(content), - )); + return Err( + "`message_authentication_codes` must contain at least `MessageAuthenticationCode::HkdfHmacSha256`" + ); } if !content .short_authentication_string .contains(&ShortAuthenticationString::Decimal) { - return Err(( + return Err( "`short_authentication_string` must contain at least `ShortAuthenticationString::Decimal`", - raw::StartEventContent::MSasV1(content), - )); + ); } Ok(StartEventContent::MSasV1(content)) diff --git a/src/lib.rs b/src/lib.rs index d2aa9948..bf4d01c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -250,14 +250,14 @@ pub trait TryFromRaw: Sized { type Err: Display; /// Tries to convert the raw type to `Self`. - fn try_from_raw(_: Self::Raw) -> Result; + 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 { + fn try_from_raw(raw: Self::Raw) -> Result { Ok(Self::from_raw(raw)) } } @@ -312,7 +312,7 @@ where match T::try_from_raw(raw_data) { Ok(value) => Ok(EventResult::Ok(value)), - Err((err, _)) => Ok(EventResult::Err(InvalidEvent { + Err(err) => Ok(EventResult::Err(InvalidEvent { message: err.to_string(), json, kind: InvalidEventKind::Validation, diff --git a/src/stripped.rs b/src/stripped.rs index d2879f7e..2807d9cd 100644 --- a/src/stripped.rs +++ b/src/stripped.rs @@ -122,29 +122,23 @@ impl TryFromRaw for StrippedState { type Raw = raw::StrippedState; type Err = String; - fn try_from_raw(raw: raw::StrippedState) -> Result { + fn try_from_raw(raw: raw::StrippedState) -> Result { use crate::util::try_convert_variant as conv; use raw::StrippedState::*; match raw { - RoomAliases(c) => conv(RoomAliases, StrippedState::RoomAliases, c), - RoomAvatar(c) => conv(RoomAvatar, StrippedState::RoomAvatar, c), - RoomCanonicalAlias(c) => conv(RoomCanonicalAlias, StrippedState::RoomCanonicalAlias, c), - RoomCreate(c) => conv(RoomCreate, StrippedState::RoomCreate, c), - RoomGuestAccess(c) => conv(RoomGuestAccess, StrippedState::RoomGuestAccess, c), - RoomHistoryVisibility(c) => conv( - RoomHistoryVisibility, - StrippedState::RoomHistoryVisibility, - c, - ), - RoomJoinRules(c) => conv(RoomJoinRules, StrippedState::RoomJoinRules, c), - RoomMember(c) => conv(RoomMember, StrippedState::RoomMember, 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), + RoomAliases(c) => conv(StrippedState::RoomAliases, c), + RoomAvatar(c) => conv(StrippedState::RoomAvatar, c), + RoomCanonicalAlias(c) => conv(StrippedState::RoomCanonicalAlias, c), + RoomCreate(c) => conv(StrippedState::RoomCreate, c), + RoomGuestAccess(c) => conv(StrippedState::RoomGuestAccess, c), + RoomHistoryVisibility(c) => conv(StrippedState::RoomHistoryVisibility, c), + RoomJoinRules(c) => conv(StrippedState::RoomJoinRules, c), + RoomMember(c) => conv(StrippedState::RoomMember, c), + RoomName(c) => conv(StrippedState::RoomName, c), + RoomPowerLevels(c) => conv(StrippedState::RoomPowerLevels, c), + RoomThirdPartyInvite(c) => conv(StrippedState::RoomThirdPartyInvite, c), + RoomTopic(c) => conv(StrippedState::RoomTopic, c), } } } @@ -156,16 +150,9 @@ where type Raw = StrippedStateContent; type Err = C::Err; - fn try_from_raw(mut raw: StrippedStateContent) -> Result { + fn try_from_raw(raw: StrippedStateContent) -> Result { Ok(Self { - content: match 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)); - } - }, + content: C::try_from_raw(raw.content)?, event_type: raw.event_type, state_key: raw.state_key, sender: raw.sender, diff --git a/src/util.rs b/src/util.rs index 0627d266..2a5c9631 100644 --- a/src/util.rs +++ b/src/util.rs @@ -4,13 +4,12 @@ use serde_json::Value; use crate::TryFromRaw; pub fn try_convert_variant( - raw_variant: fn(Content::Raw) -> Enum::Raw, variant: fn(Content) -> Enum, raw: Content::Raw, -) -> Result { +) -> Result { Content::try_from_raw(raw) .map(variant) - .map_err(|(err, raw)| (err.to_string(), raw_variant(raw))) + .map_err(|err| err.to_string()) } pub fn try_variant_from_value(value: Value, variant: fn(T) -> U) -> Result