Skip to content

Commit a49bffa

Browse files
BillCarsonFrpoljar
authored andcommitted
review: refactors ProcessedToDeviceEvent to tuple variants
Simplifies the `ProcessedToDeviceEvent` enum by converting its variants to tuple variants. This change improves code readability and conciseness by removing the need for named fields within the variants.
1 parent d4a0c28 commit a49bffa

File tree

7 files changed

+32
-42
lines changed

7 files changed

+32
-42
lines changed

crates/matrix-sdk-crypto/src/machine/mod.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use std::{
2121
use itertools::Itertools;
2222
use matrix_sdk_common::{
2323
deserialized_responses::{
24-
AlgorithmInfo, DecryptedRoomEvent, DeviceLinkProblem, EncryptionInfo,
25-
UnableToDecryptInfo, UnableToDecryptReason,
26-
UnsignedDecryptionResult, UnsignedEventLocation, VerificationLevel, VerificationState,
24+
AlgorithmInfo, DecryptedRoomEvent, DeviceLinkProblem, EncryptionInfo, UnableToDecryptInfo,
25+
UnableToDecryptReason, UnsignedDecryptionResult, UnsignedEventLocation, VerificationLevel,
26+
VerificationState,
2727
},
2828
locks::RwLock as StdRwLock,
2929
BoxFuture,
@@ -95,14 +95,13 @@ use crate::{
9595
AnyIncomingResponse, KeysQueryRequest, OutgoingRequest, ToDeviceRequest,
9696
UploadSigningKeysRequest,
9797
},
98-
EventEncryptionAlgorithm, Signatures,
98+
EventEncryptionAlgorithm, ProcessedToDeviceEvent, Signatures,
9999
},
100100
utilities::timestamp_to_iso8601,
101101
verification::{Verification, VerificationMachine, VerificationRequest},
102102
CollectStrategy, CrossSigningKeyExport, CryptoStoreError, DecryptionSettings, DeviceData,
103103
LocalTrust, RoomEventDecryptionResult, SignatureError, TrustRequirement,
104104
};
105-
use crate::types::ProcessedToDeviceEvent;
106105

107106
/// State machine implementation of the Olm/Megolm encryption protocol used for
108107
/// Matrix end to end encryption.
@@ -1320,7 +1319,7 @@ impl OlmMachine {
13201319
}
13211320
}
13221321

1323-
return Some(ProcessedToDeviceEvent::UnableToDecrypt { event: raw_event });
1322+
return Some(ProcessedToDeviceEvent::UnableToDecrypt(raw_event));
13241323
}
13251324
};
13261325

@@ -1373,7 +1372,7 @@ impl OlmMachine {
13731372
}
13741373
}
13751374

1376-
Some(ProcessedToDeviceEvent::Decrypted { decrypted_event: raw_event })
1375+
Some(ProcessedToDeviceEvent::Decrypted(raw_event))
13771376
}
13781377

13791378
e => {

crates/matrix-sdk-crypto/src/machine/tests/megolm_sender_data.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use std::{fmt::Debug, iter, pin::Pin};
1717
use assert_matches::assert_matches;
1818
use futures_core::Stream;
1919
use futures_util::{FutureExt, StreamExt};
20-
use crate::types::ProcessedToDeviceEvent;
2120
use matrix_sdk_test::async_test;
2221
use ruma::{room_id, user_id, RoomId, TransactionId, UserId};
2322
use serde::Serialize;
@@ -34,7 +33,10 @@ use crate::{
3433
},
3534
olm::{InboundGroupSession, SenderData},
3635
store::RoomKeyInfo,
37-
types::events::{room::encrypted::ToDeviceEncryptedEventContent, EventType, ToDeviceEvent},
36+
types::{
37+
events::{room::encrypted::ToDeviceEncryptedEventContent, EventType, ToDeviceEvent},
38+
ProcessedToDeviceEvent,
39+
},
3840
DeviceData, EncryptionSettings, EncryptionSyncChanges, OlmMachine, Session,
3941
};
4042

crates/matrix-sdk-crypto/src/machine/tests/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ use assert_matches2::{assert_let, assert_matches};
1818
use futures_util::{pin_mut, FutureExt, StreamExt};
1919
use itertools::Itertools;
2020
use matrix_sdk_common::deserialized_responses::{
21-
AlgorithmInfo, UnableToDecryptInfo, UnableToDecryptReason,
22-
UnsignedDecryptionResult, UnsignedEventLocation, VerificationLevel, VerificationState,
23-
WithheldCode,
21+
AlgorithmInfo, UnableToDecryptInfo, UnableToDecryptReason, UnsignedDecryptionResult,
22+
UnsignedEventLocation, VerificationLevel, VerificationState, WithheldCode,
2423
};
2524
use matrix_sdk_test::{async_test, message_like_event_content, ruma_response_from_json, test_json};
2625
use ruma::{
@@ -71,14 +70,13 @@ use crate::{
7170
ToDeviceEvent,
7271
},
7372
requests::{AnyOutgoingRequest, ToDeviceRequest},
74-
DeviceKeys, SignedKey, SigningKeys,
73+
DeviceKeys, ProcessedToDeviceEvent, SignedKey, SigningKeys,
7574
},
7675
utilities::json_convert,
7776
verification::tests::bob_id,
7877
Account, DecryptionSettings, DeviceData, EncryptionSettings, LocalTrust, MegolmError, OlmError,
7978
RoomEventDecryptionResult, TrustRequirement,
8079
};
81-
use crate::types::ProcessedToDeviceEvent;
8280

8381
mod decryption_verification_state;
8482
mod interactive_verification;

crates/matrix-sdk-crypto/src/machine/tests/olm_encryption.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use std::{
1818
};
1919

2020
use assert_matches2::assert_let;
21-
use crate::types::ProcessedToDeviceEvent;
2221
use matrix_sdk_test::async_test;
2322
use ruma::{
2423
device_id,
@@ -39,7 +38,7 @@ use crate::{
3938
},
4039
olm::utility::SignJson,
4140
store::Changes,
42-
types::{events::ToDeviceEvent, DeviceKeys},
41+
types::{events::ToDeviceEvent, DeviceKeys, ProcessedToDeviceEvent},
4342
DeviceData, OlmMachine,
4443
};
4544

@@ -295,9 +294,7 @@ async fn test_decrypt_to_device_message_with_unsigned_sender_keys() {
295294

296295
let event = to_device_events.first().expect("Bob did not get a to-device event").clone();
297296

298-
// The to-device event should remain decrypted.
299-
let ProcessedToDeviceEvent::UnableToDecrypt { event } = event else {
300-
panic!("Should refuse to decrypt")
301-
};
297+
// The to-device event should remain encrypted.
298+
assert_let!(ProcessedToDeviceEvent::UnableToDecrypt(event) = event);
302299
assert_eq!(event.get_field("type").unwrap(), Some("m.room.encrypted"));
303300
}

crates/matrix-sdk-crypto/src/machine/tests/send_encrypted_to_device.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
use assert_matches2::{assert_let, assert_matches};
16-
use crate::types::ProcessedToDeviceEvent;
1716
use matrix_sdk_test::async_test;
1817
use ruma::{events::AnyToDeviceEvent, serde::Raw, to_device::DeviceIdOrAllDevices};
1918
use serde_json::{json, value::to_raw_value};
@@ -26,6 +25,7 @@ use crate::{
2625
types::{
2726
events::{ToDeviceCustomEvent, ToDeviceEvent},
2827
requests::ToDeviceRequest,
28+
ProcessedToDeviceEvent,
2929
},
3030
utilities::json_convert,
3131
EncryptionSyncChanges, OlmError,
@@ -87,7 +87,7 @@ async fn test_send_encrypted_to_device() {
8787
assert_eq!(1, decrypted.len());
8888
let processed_event = &decrypted[0];
8989

90-
assert_let!(ProcessedToDeviceEvent::Decrypted { decrypted_event, .. } = processed_event);
90+
assert_let!(ProcessedToDeviceEvent::Decrypted(decrypted_event) = processed_event);
9191

9292
let decrypted_event = decrypted_event.deserialize().unwrap();
9393

@@ -208,7 +208,7 @@ async fn test_processed_to_device_variants() {
208208
assert_eq!(4, processed.len());
209209

210210
let processed_event = &processed[0];
211-
assert_matches!(processed_event, ProcessedToDeviceEvent::Decrypted { .. });
211+
assert_matches!(processed_event, ProcessedToDeviceEvent::Decrypted(_));
212212

213213
let processed_event = &processed[1];
214214
assert_matches!(processed_event, ProcessedToDeviceEvent::PlainText(_));

crates/matrix-sdk-crypto/src/session_manager/group_sessions/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,11 +1063,10 @@ mod tests {
10631063
room_key_withheld::RoomKeyWithheldContent::{self, MegolmV1AesSha2},
10641064
},
10651065
requests::ToDeviceRequest,
1066-
DeviceKeys, EventEncryptionAlgorithm,
1066+
DeviceKeys, EventEncryptionAlgorithm, ProcessedToDeviceEvent,
10671067
},
10681068
EncryptionSettings, LocalTrust, OlmMachine,
10691069
};
1070-
use crate::types::ProcessedToDeviceEvent;
10711070

10721071
fn alice_id() -> &'static UserId {
10731072
user_id!("@alice:example.org")
@@ -1824,8 +1823,7 @@ mod tests {
18241823
assert_eq!(1, decrypted.len());
18251824
use crate::types::events::EventType;
18261825
assert_let!(
1827-
ProcessedToDeviceEvent::Decrypted { decrypted_event, .. } =
1828-
decrypted.first().unwrap().clone()
1826+
ProcessedToDeviceEvent::Decrypted(decrypted_event) = decrypted.first().unwrap().clone()
18291827
);
18301828
assert_eq!(
18311829
decrypted_event.get_field::<String>("type").unwrap().unwrap(),

crates/matrix-sdk-crypto/src/types/mod.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ use std::{
3636
use as_variant::as_variant;
3737
use matrix_sdk_common::deserialized_responses::PrivOwnedStr;
3838
use ruma::{
39-
serde::StringEnum, DeviceKeyAlgorithm, DeviceKeyId, OwnedDeviceKeyId, OwnedUserId, UserId,
39+
events::AnyToDeviceEvent,
40+
serde::{Raw, StringEnum},
41+
DeviceKeyAlgorithm, DeviceKeyId, OwnedDeviceKeyId, OwnedUserId, UserId,
4042
};
4143
use serde::{Deserialize, Deserializer, Serialize, Serializer};
4244
use vodozemac::{Curve25519PublicKey, Ed25519PublicKey, Ed25519Signature, KeyError};
4345
use zeroize::{Zeroize, ZeroizeOnDrop};
44-
use ruma::serde::Raw;
45-
use ruma::events::AnyToDeviceEvent;
4646

4747
mod backup;
4848
mod cross_signing;
@@ -633,16 +633,11 @@ mod test {
633633
#[derive(Clone, Debug, Serialize, Deserialize)]
634634
pub enum ProcessedToDeviceEvent {
635635
/// A successfully-decrypted encrypted event.
636-
Decrypted {
637-
/// The decrypted to device event
638-
decrypted_event: Raw<AnyToDeviceEvent>,
639-
},
636+
/// Contains the raw decrypted event .
637+
Decrypted(Raw<AnyToDeviceEvent>),
640638

641639
/// An encrypted event which could not be decrypted.
642-
UnableToDecrypt {
643-
/// The `m.room.encrypted` to device event.
644-
event: Raw<AnyToDeviceEvent>,
645-
},
640+
UnableToDecrypt(Raw<AnyToDeviceEvent>),
646641

647642
/// An unencrypted event.
648643
PlainText(Raw<AnyToDeviceEvent>),
@@ -652,13 +647,14 @@ pub enum ProcessedToDeviceEvent {
652647
}
653648

654649
impl ProcessedToDeviceEvent {
655-
/// Converts a ProcessedToDeviceEvent to the Raw<AnyToDeviceEvent> it encapsulates
650+
/// Converts a ProcessedToDeviceEvent to the Raw<AnyToDeviceEvent> it
651+
/// encapsulates
656652
pub fn to_raw(&self) -> Raw<AnyToDeviceEvent> {
657653
match self {
658-
ProcessedToDeviceEvent::Decrypted { decrypted_event, .. } => decrypted_event.clone(),
659-
ProcessedToDeviceEvent::UnableToDecrypt { event } => event.clone(),
654+
ProcessedToDeviceEvent::Decrypted(decrypted_event) => decrypted_event.clone(),
655+
ProcessedToDeviceEvent::UnableToDecrypt(event) => event.clone(),
660656
ProcessedToDeviceEvent::PlainText(event) => event.clone(),
661657
ProcessedToDeviceEvent::NotProcessed(event) => event.clone(),
662658
}
663659
}
664-
}
660+
}

0 commit comments

Comments
 (0)