Skip to content

Commit 50ccc26

Browse files
committed
Factor out common parts of MegolmDecryptionError construction
1 parent d30d701 commit 50ccc26

File tree

1 file changed

+20
-38
lines changed

1 file changed

+20
-38
lines changed

src/error.rs

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Errors related to room event decryption.
22
33
use js_sys::JsString;
4-
use matrix_sdk_common::deserialized_responses::VerificationLevel;
4+
use matrix_sdk_common::deserialized_responses::{VerificationLevel, WithheldCode};
55
use matrix_sdk_crypto::{vodozemac, MegolmError};
66
use wasm_bindgen::prelude::wasm_bindgen;
77

@@ -55,52 +55,34 @@ impl MegolmDecryptionError {
5555

5656
impl From<MegolmError> for MegolmDecryptionError {
5757
fn from(value: MegolmError) -> Self {
58+
let decryption_error = |code: DecryptionErrorCode,
59+
maybe_withheld: Option<&WithheldCode>|
60+
-> MegolmDecryptionError {
61+
let description = value.to_string().into();
62+
let maybe_withheld = maybe_withheld.map(|code| code.to_string().to_owned().into());
63+
MegolmDecryptionError { code, description, maybe_withheld }
64+
};
65+
5866
match &value {
59-
MegolmError::MissingRoomKey(withheld_code) => MegolmDecryptionError {
60-
code: DecryptionErrorCode::MissingRoomKey,
61-
description: value.to_string().into(),
62-
maybe_withheld: withheld_code
63-
.as_ref()
64-
.map(|code| code.to_string().to_owned().into()),
65-
},
67+
MegolmError::MissingRoomKey(withheld_code) => {
68+
decryption_error(DecryptionErrorCode::MissingRoomKey, withheld_code.as_ref())
69+
}
6670
MegolmError::Decryption(vodozemac::megolm::DecryptionError::UnknownMessageIndex(
6771
..,
68-
)) => MegolmDecryptionError {
69-
code: DecryptionErrorCode::UnknownMessageIndex,
70-
description: value.to_string().into(),
71-
maybe_withheld: None,
72-
},
73-
MegolmError::MismatchedIdentityKeys { .. } => MegolmDecryptionError {
74-
code: DecryptionErrorCode::UnknownMessageIndex,
75-
description: value.to_string().into(),
76-
maybe_withheld: None,
77-
},
72+
)) => decryption_error(DecryptionErrorCode::UnknownMessageIndex, None),
73+
MegolmError::MismatchedIdentityKeys { .. } => {
74+
decryption_error(DecryptionErrorCode::UnknownMessageIndex, None)
75+
}
7876
MegolmError::SenderIdentityNotTrusted(VerificationLevel::VerificationViolation) => {
79-
MegolmDecryptionError {
80-
code: DecryptionErrorCode::SenderIdentityVerificationViolation,
81-
description: value.to_string().into(),
82-
maybe_withheld: None,
83-
}
77+
decryption_error(DecryptionErrorCode::SenderIdentityVerificationViolation, None)
8478
}
8579
MegolmError::SenderIdentityNotTrusted(VerificationLevel::UnsignedDevice) => {
86-
MegolmDecryptionError {
87-
code: DecryptionErrorCode::UnsignedSenderDevice,
88-
description: value.to_string().into(),
89-
maybe_withheld: None,
90-
}
80+
decryption_error(DecryptionErrorCode::UnsignedSenderDevice, None)
9181
}
9282
MegolmError::SenderIdentityNotTrusted(VerificationLevel::None(..)) => {
93-
MegolmDecryptionError {
94-
code: DecryptionErrorCode::UnknownSenderDevice,
95-
description: value.to_string().into(),
96-
maybe_withheld: None,
97-
}
83+
decryption_error(DecryptionErrorCode::UnknownSenderDevice, None)
9884
}
99-
_ => MegolmDecryptionError {
100-
code: DecryptionErrorCode::UnableToDecrypt,
101-
description: value.to_string().into(),
102-
maybe_withheld: None,
103-
},
85+
_ => decryption_error(DecryptionErrorCode::UnableToDecrypt, None),
10486
}
10587
}
10688
}

0 commit comments

Comments
 (0)