Rename to_device::ToDevice to AnyToDeviceEvent

This commit is contained in:
Jonas Platte 2020-03-12 21:02:04 +01:00
parent 7920445832
commit fdef9a12e6
No known key found for this signature in database
GPG Key ID: 7D261D771D915378

View File

@ -25,7 +25,7 @@ use crate::{
/// sync response. /// sync response.
#[derive(Clone, Debug, PartialEq, Serialize)] #[derive(Clone, Debug, PartialEq, Serialize)]
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
pub enum ToDevice { pub enum AnyToDeviceEvent {
// TODO this should include a *m.dummy" event. // TODO this should include a *m.dummy" event.
/// To-device version of the *m.room_key* event. /// To-device version of the *m.room_key* event.
RoomKey(ToDeviceRoomKey), RoomKey(ToDeviceRoomKey),
@ -88,25 +88,25 @@ pub type ToDeviceVerificationCancel = ToDeviceEvent<CancelEventContent>;
/// To-device version of the *m.key.verification.request* event. /// To-device version of the *m.key.verification.request* event.
pub type ToDeviceVerificationRequest = ToDeviceEvent<RequestEventContent>; pub type ToDeviceVerificationRequest = ToDeviceEvent<RequestEventContent>;
impl TryFromRaw for ToDevice { impl TryFromRaw for AnyToDeviceEvent {
type Raw = raw::ToDevice; type Raw = raw::AnyToDeviceEvent;
type Err = String; type Err = String;
fn try_from_raw(raw: raw::ToDevice) -> Result<Self, Self::Err> { fn try_from_raw(raw: raw::AnyToDeviceEvent) -> Result<Self, Self::Err> {
use crate::util::try_convert_variant as conv; use crate::util::try_convert_variant as conv;
use raw::ToDevice::*; use raw::AnyToDeviceEvent::*;
match raw { match raw {
RoomKey(c) => conv(ToDevice::RoomKey, c), RoomKey(c) => conv(AnyToDeviceEvent::RoomKey, c),
RoomEncrypted(c) => conv(ToDevice::RoomEncrypted, c), RoomEncrypted(c) => conv(AnyToDeviceEvent::RoomEncrypted, c),
ForwardedRoomKey(c) => conv(ToDevice::ForwardedRoomKey, c), ForwardedRoomKey(c) => conv(AnyToDeviceEvent::ForwardedRoomKey, c),
RoomKeyRequest(c) => conv(ToDevice::RoomKeyRequest, c), RoomKeyRequest(c) => conv(AnyToDeviceEvent::RoomKeyRequest, c),
KeyVerificationStart(c) => conv(ToDevice::KeyVerificationStart, c), KeyVerificationStart(c) => conv(AnyToDeviceEvent::KeyVerificationStart, c),
KeyVerificationAccept(c) => conv(ToDevice::KeyVerificationAccept, c), KeyVerificationAccept(c) => conv(AnyToDeviceEvent::KeyVerificationAccept, c),
KeyVerificationKey(c) => conv(ToDevice::KeyVerificationKey, c), KeyVerificationKey(c) => conv(AnyToDeviceEvent::KeyVerificationKey, c),
KeyVerificationMac(c) => conv(ToDevice::KeyVerificationMac, c), KeyVerificationMac(c) => conv(AnyToDeviceEvent::KeyVerificationMac, c),
KeyVerificationCancel(c) => conv(ToDevice::KeyVerificationCancel, c), KeyVerificationCancel(c) => conv(AnyToDeviceEvent::KeyVerificationCancel, c),
KeyVerificationRequest(c) => conv(ToDevice::KeyVerificationRequest, c), KeyVerificationRequest(c) => conv(AnyToDeviceEvent::KeyVerificationRequest, c),
} }
} }
} }
@ -187,7 +187,7 @@ mod raw {
/// A stripped-down version of a state event that is included along with some other events. /// A stripped-down version of a state event that is included along with some other events.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
pub enum ToDevice { pub enum AnyToDeviceEvent {
/// To-device version of the *m.room_key* event. /// To-device version of the *m.room_key* event.
RoomKey(ToDeviceRoomKey), RoomKey(ToDeviceRoomKey),
/// To-device version of the *m.room.encrypted* event. /// To-device version of the *m.room.encrypted* event.
@ -210,7 +210,7 @@ mod raw {
KeyVerificationRequest(ToDeviceVerificationRequest), KeyVerificationRequest(ToDeviceVerificationRequest),
} }
impl<'de> Deserialize<'de> for ToDevice { impl<'de> Deserialize<'de> for AnyToDeviceEvent {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
@ -224,16 +224,18 @@ mod raw {
let event_type = get_field(&value, "type")?; let event_type = get_field(&value, "type")?;
match event_type { match event_type {
RoomKey => from_value(value, ToDevice::RoomKey), RoomKey => from_value(value, AnyToDeviceEvent::RoomKey),
RoomEncrypted => from_value(value, ToDevice::RoomEncrypted), RoomEncrypted => from_value(value, AnyToDeviceEvent::RoomEncrypted),
ForwardedRoomKey => from_value(value, ToDevice::ForwardedRoomKey), ForwardedRoomKey => from_value(value, AnyToDeviceEvent::ForwardedRoomKey),
RoomKeyRequest => from_value(value, ToDevice::RoomKeyRequest), RoomKeyRequest => from_value(value, AnyToDeviceEvent::RoomKeyRequest),
KeyVerificationStart => from_value(value, ToDevice::KeyVerificationStart), KeyVerificationStart => from_value(value, AnyToDeviceEvent::KeyVerificationStart),
KeyVerificationAccept => from_value(value, ToDevice::KeyVerificationAccept), KeyVerificationAccept => from_value(value, AnyToDeviceEvent::KeyVerificationAccept),
KeyVerificationKey => from_value(value, ToDevice::KeyVerificationKey), KeyVerificationKey => from_value(value, AnyToDeviceEvent::KeyVerificationKey),
KeyVerificationMac => from_value(value, ToDevice::KeyVerificationMac), KeyVerificationMac => from_value(value, AnyToDeviceEvent::KeyVerificationMac),
KeyVerificationCancel => from_value(value, ToDevice::KeyVerificationCancel), KeyVerificationCancel => from_value(value, AnyToDeviceEvent::KeyVerificationCancel),
KeyVerificationRequest => from_value(value, ToDevice::KeyVerificationRequest), KeyVerificationRequest => {
from_value(value, AnyToDeviceEvent::KeyVerificationRequest)
}
_ => Err(D::Error::custom("unknown to-device event")), _ => Err(D::Error::custom("unknown to-device event")),
} }
} }
@ -248,7 +250,7 @@ mod tests {
use ruma_identifiers::{RoomId, UserId}; use ruma_identifiers::{RoomId, UserId};
use super::ToDevice; use super::AnyToDeviceEvent;
use crate::{ use crate::{
key::verification::{ key::verification::{
cancel::CancelCode, start::StartEventContent, HashAlgorithm, KeyAgreementProtocol, cancel::CancelCode, start::StartEventContent, HashAlgorithm, KeyAgreementProtocol,
@ -261,7 +263,7 @@ mod tests {
macro_rules! deserialize { macro_rules! deserialize {
($source:ident, $($target:tt)*) => {{ ($source:ident, $($target:tt)*) => {{
let event = serde_json::from_str::<EventResult<ToDevice>>($source) let event = serde_json::from_str::<EventResult<AnyToDeviceEvent>>($source)
.expect(&format!( .expect(&format!(
"Can't deserialize to-device event: {} from source {}", "Can't deserialize to-device event: {} from source {}",
stringify!($($target)*), $source stringify!($($target)*), $source
@ -300,7 +302,7 @@ mod tests {
"type": "m.room_key" "type": "m.room_key"
}"#; }"#;
let event = deserialize! {room_key, ToDevice::RoomKey}; let event = deserialize! {room_key, AnyToDeviceEvent::RoomKey};
assert_eq!( assert_eq!(
event.content.room_id, event.content.room_id,
@ -332,7 +334,7 @@ mod tests {
"sender": "@alice:example.org" "sender": "@alice:example.org"
}"#; }"#;
let event = deserialize! {source, ToDevice::RoomEncrypted}; let event = deserialize! {source, AnyToDeviceEvent::RoomEncrypted};
let content = match &event.content { let content = match &event.content {
EncryptedEventContent::OlmV1Curve25519AesSha2(c) => c, EncryptedEventContent::OlmV1Curve25519AesSha2(c) => c,
@ -366,7 +368,7 @@ mod tests {
"type": "m.forwarded_room_key" "type": "m.forwarded_room_key"
}"#; }"#;
let event = deserialize! {source, ToDevice::ForwardedRoomKey}; let event = deserialize! {source, AnyToDeviceEvent::ForwardedRoomKey};
assert_eq!( assert_eq!(
event.content.room_id, event.content.room_id,
@ -403,7 +405,7 @@ mod tests {
"type": "m.room_key_request" "type": "m.room_key_request"
}"#; }"#;
let event = deserialize! {source, ToDevice::RoomKeyRequest}; let event = deserialize! {source, AnyToDeviceEvent::RoomKeyRequest};
let body = event.content.body.as_ref().unwrap(); let body = event.content.body.as_ref().unwrap();
assert_eq!(event.content.action, Action::Request); assert_eq!(event.content.action, Action::Request);
@ -432,7 +434,7 @@ mod tests {
"type": "m.room_key_request" "type": "m.room_key_request"
}"#; }"#;
let event = deserialize! {source, ToDevice::RoomKeyRequest}; let event = deserialize! {source, AnyToDeviceEvent::RoomKeyRequest};
assert_eq!(event.content.action, Action::CancelRequest); assert_eq!(event.content.action, Action::CancelRequest);
assert_eq!(event.content.request_id, "1495474790150.19"); assert_eq!(event.content.request_id, "1495474790150.19");
assert_eq!(event.content.requesting_device_id, "RJYKSTBOIE"); assert_eq!(event.content.requesting_device_id, "RJYKSTBOIE");
@ -463,7 +465,7 @@ mod tests {
"sender": "@alice:example.org" "sender": "@alice:example.org"
}"#; }"#;
let event = deserialize! {source, ToDevice::KeyVerificationStart}; let event = deserialize! {source, AnyToDeviceEvent::KeyVerificationStart};
let content = match &event.content { let content = match &event.content {
StartEventContent::MSasV1(c) => c, StartEventContent::MSasV1(c) => c,
@ -509,7 +511,7 @@ mod tests {
"sender": "@alice:example.org" "sender": "@alice:example.org"
}"#; }"#;
let event = deserialize! {source, ToDevice::KeyVerificationAccept}; let event = deserialize! {source, AnyToDeviceEvent::KeyVerificationAccept};
assert_eq!(event.content.hash, HashAlgorithm::Sha256); assert_eq!(event.content.hash, HashAlgorithm::Sha256);
assert_eq!( assert_eq!(
event.content.commitment, event.content.commitment,
@ -545,7 +547,7 @@ mod tests {
"sender": "@alice:example.org" "sender": "@alice:example.org"
}"#; }"#;
let event = deserialize! {source, ToDevice::KeyVerificationKey}; let event = deserialize! {source, AnyToDeviceEvent::KeyVerificationKey};
assert_eq!(event.content.transaction_id, "S0meUniqueAndOpaqueString"); assert_eq!(event.content.transaction_id, "S0meUniqueAndOpaqueString");
assert_eq!( assert_eq!(
@ -568,7 +570,7 @@ mod tests {
"sender": "@alice:example.org" "sender": "@alice:example.org"
}"#; }"#;
let event = deserialize! {source, ToDevice::KeyVerificationMac}; let event = deserialize! {source, AnyToDeviceEvent::KeyVerificationMac};
assert_eq!(event.content.transaction_id, "S0meUniqueAndOpaqueString"); assert_eq!(event.content.transaction_id, "S0meUniqueAndOpaqueString");
assert_eq!( assert_eq!(
event.content.keys, event.content.keys,
@ -592,7 +594,7 @@ mod tests {
"sender": "@alice:example.org" "sender": "@alice:example.org"
}"#; }"#;
let event = deserialize! {source, ToDevice::KeyVerificationCancel}; let event = deserialize! {source, AnyToDeviceEvent::KeyVerificationCancel};
assert_eq!(event.content.transaction_id, "S0meUniqueAndOpaqueString"); assert_eq!(event.content.transaction_id, "S0meUniqueAndOpaqueString");
assert_eq!(event.content.reason, "Some reason"); assert_eq!(event.content.reason, "Some reason");
assert_eq!(event.content.code, CancelCode::User); assert_eq!(event.content.code, CancelCode::User);
@ -613,7 +615,7 @@ mod tests {
"sender": "@alice:example.org" "sender": "@alice:example.org"
}"#; }"#;
let event = deserialize! {source, ToDevice::KeyVerificationRequest}; let event = deserialize! {source, AnyToDeviceEvent::KeyVerificationRequest};
assert_eq!(event.content.transaction_id, "S0meUniqueAndOpaqueString"); assert_eq!(event.content.transaction_id, "S0meUniqueAndOpaqueString");
assert_eq!(event.content.from_device, "AliceDevice2"); assert_eq!(event.content.from_device, "AliceDevice2");
assert_eq!(event.content.methods, &[VerificationMethod::MSasV1]); assert_eq!(event.content.methods, &[VerificationMethod::MSasV1]);