From abcd2874960c8cfb8a11aee5b7798a6226ed501e Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 14 Sep 2022 16:05:05 +0200 Subject: [PATCH] chore(crypto-js): Simplify code with a lovely macro. --- bindings/matrix-sdk-crypto-js/src/device.rs | 14 +---- .../matrix-sdk-crypto-js/src/identifiers.rs | 32 +++------- bindings/matrix-sdk-crypto-js/src/lib.rs | 1 + bindings/matrix-sdk-crypto-js/src/macros.rs | 33 ++++++++++ bindings/matrix-sdk-crypto-js/src/olm.rs | 8 +-- bindings/matrix-sdk-crypto-js/src/store.rs | 8 +-- bindings/matrix-sdk-crypto-js/src/types.rs | 19 ++---- .../matrix-sdk-crypto-js/src/verification.rs | 60 +++++-------------- .../matrix-sdk-crypto-js/src/vodozemac.rs | 26 ++------ 9 files changed, 74 insertions(+), 127 deletions(-) create mode 100644 bindings/matrix-sdk-crypto-js/src/macros.rs diff --git a/bindings/matrix-sdk-crypto-js/src/device.rs b/bindings/matrix-sdk-crypto-js/src/device.rs index 84d79a5c9..f18d431c4 100644 --- a/bindings/matrix-sdk-crypto-js/src/device.rs +++ b/bindings/matrix-sdk-crypto-js/src/device.rs @@ -6,7 +6,7 @@ use wasm_bindgen::prelude::*; use crate::{ future::future_to_promise, identifiers::{self, DeviceId, UserId}, - types, verification, vodozemac, + impl_from_to_inner, types, verification, vodozemac, }; /// A device represents a E2EE capable client of an user. @@ -16,11 +16,7 @@ pub struct Device { pub(crate) inner: matrix_sdk_crypto::Device, } -impl From for Device { - fn from(inner: matrix_sdk_crypto::Device) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_crypto::Device => Device); #[wasm_bindgen] impl Device { @@ -228,11 +224,7 @@ pub struct UserDevices { pub(crate) inner: matrix_sdk_crypto::UserDevices, } -impl From for UserDevices { - fn from(inner: matrix_sdk_crypto::UserDevices) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_crypto::UserDevices => UserDevices); #[wasm_bindgen] impl UserDevices { diff --git a/bindings/matrix-sdk-crypto-js/src/identifiers.rs b/bindings/matrix-sdk-crypto-js/src/identifiers.rs index c75a75d56..c2022909a 100644 --- a/bindings/matrix-sdk-crypto-js/src/identifiers.rs +++ b/bindings/matrix-sdk-crypto-js/src/identifiers.rs @@ -3,6 +3,8 @@ use wasm_bindgen::prelude::*; +use crate::impl_from_to_inner; + /// A Matrix [user ID]. /// /// [user ID]: https://spec.matrix.org/v1.2/appendices/#user-identifiers @@ -12,11 +14,7 @@ pub struct UserId { pub(crate) inner: ruma::OwnedUserId, } -impl From for UserId { - fn from(inner: ruma::OwnedUserId) -> Self { - Self { inner } - } -} +impl_from_to_inner!(ruma::OwnedUserId => UserId); #[wasm_bindgen] impl UserId { @@ -66,11 +64,7 @@ pub struct DeviceId { pub(crate) inner: ruma::OwnedDeviceId, } -impl From for DeviceId { - fn from(inner: ruma::OwnedDeviceId) -> Self { - Self { inner } - } -} +impl_from_to_inner!(ruma::OwnedDeviceId => DeviceId); #[wasm_bindgen] impl DeviceId { @@ -97,11 +91,7 @@ pub struct DeviceKeyId { pub(crate) inner: ruma::OwnedDeviceKeyId, } -impl From for DeviceKeyId { - fn from(inner: ruma::OwnedDeviceKeyId) -> Self { - Self { inner } - } -} +impl_from_to_inner!(ruma::OwnedDeviceKeyId => DeviceKeyId); #[wasm_bindgen] impl DeviceKeyId { @@ -138,11 +128,7 @@ pub struct DeviceKeyAlgorithm { pub(crate) inner: ruma::DeviceKeyAlgorithm, } -impl From for DeviceKeyAlgorithm { - fn from(inner: ruma::DeviceKeyAlgorithm) -> Self { - Self { inner } - } -} +impl_from_to_inner!(ruma::DeviceKeyAlgorithm => DeviceKeyAlgorithm); #[wasm_bindgen] impl DeviceKeyAlgorithm { @@ -221,11 +207,7 @@ pub struct RoomId { pub(crate) inner: ruma::OwnedRoomId, } -impl From for RoomId { - fn from(inner: ruma::OwnedRoomId) -> Self { - Self { inner } - } -} +impl_from_to_inner!(ruma::OwnedRoomId => RoomId); #[wasm_bindgen] impl RoomId { diff --git a/bindings/matrix-sdk-crypto-js/src/lib.rs b/bindings/matrix-sdk-crypto-js/src/lib.rs index e444a661d..2421eb801 100644 --- a/bindings/matrix-sdk-crypto-js/src/lib.rs +++ b/bindings/matrix-sdk-crypto-js/src/lib.rs @@ -25,6 +25,7 @@ mod future; pub mod identifiers; pub mod machine; pub mod olm; +mod macros; pub mod requests; pub mod responses; pub mod store; diff --git a/bindings/matrix-sdk-crypto-js/src/macros.rs b/bindings/matrix-sdk-crypto-js/src/macros.rs new file mode 100644 index 000000000..8d1189fef --- /dev/null +++ b/bindings/matrix-sdk-crypto-js/src/macros.rs @@ -0,0 +1,33 @@ +/// We have the following pattern quite often in our code: +/// +/// ```rust +/// struct Foo { +/// inner: Bar, +/// } +/// +/// impl From for Foo { +/// fn from(inner: Bar) -> Self { +/// Self { inner } +/// } +/// } +/// ``` +/// +/// Because I feel lazy, let's do a macro to write this: +/// +/// ```rust +/// struct Foo { +/// inner: Bar, +/// } +/// +/// impl_from_to_inner!(Bar => Foo); +/// ``` +#[macro_export] +macro_rules! impl_from_to_inner { + ($from:ty => $to:ty) => { + impl From<$from> for $to { + fn from(inner: $from) -> Self { + Self { inner } + } + } + }; +} diff --git a/bindings/matrix-sdk-crypto-js/src/olm.rs b/bindings/matrix-sdk-crypto-js/src/olm.rs index 09332dbcb..8a68c6bdc 100644 --- a/bindings/matrix-sdk-crypto-js/src/olm.rs +++ b/bindings/matrix-sdk-crypto-js/src/olm.rs @@ -2,6 +2,8 @@ use wasm_bindgen::prelude::*; +use crate::impl_from_to_inner; + /// Struct representing the state of our private cross signing keys, /// it shows which private cross signing keys we have locally stored. #[wasm_bindgen] @@ -10,11 +12,7 @@ pub struct CrossSigningStatus { inner: matrix_sdk_crypto::olm::CrossSigningStatus, } -impl From for CrossSigningStatus { - fn from(inner: matrix_sdk_crypto::olm::CrossSigningStatus) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_crypto::olm::CrossSigningStatus => CrossSigningStatus); #[wasm_bindgen] impl CrossSigningStatus { diff --git a/bindings/matrix-sdk-crypto-js/src/store.rs b/bindings/matrix-sdk-crypto-js/src/store.rs index eabae3f55..f8033840e 100644 --- a/bindings/matrix-sdk-crypto-js/src/store.rs +++ b/bindings/matrix-sdk-crypto-js/src/store.rs @@ -2,6 +2,8 @@ use wasm_bindgen::prelude::*; +use crate::impl_from_to_inner; + /// A struct containing private cross signing keys that can be backed /// up or uploaded to the secret store. #[wasm_bindgen] @@ -10,11 +12,7 @@ pub struct CrossSigningKeyExport { pub(crate) inner: matrix_sdk_crypto::store::CrossSigningKeyExport, } -impl From for CrossSigningKeyExport { - fn from(inner: matrix_sdk_crypto::store::CrossSigningKeyExport) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_crypto::store::CrossSigningKeyExport => CrossSigningKeyExport); #[wasm_bindgen] impl CrossSigningKeyExport { diff --git a/bindings/matrix-sdk-crypto-js/src/types.rs b/bindings/matrix-sdk-crypto-js/src/types.rs index 22bf62a9b..c42a40ad4 100644 --- a/bindings/matrix-sdk-crypto-js/src/types.rs +++ b/bindings/matrix-sdk-crypto-js/src/types.rs @@ -5,6 +5,7 @@ use wasm_bindgen::prelude::*; use crate::{ identifiers::{DeviceKeyId, UserId}, + impl_from_to_inner, vodozemac::Ed25519Signature, }; @@ -15,11 +16,7 @@ pub struct Signatures { inner: matrix_sdk_crypto::types::Signatures, } -impl From for Signatures { - fn from(inner: matrix_sdk_crypto::types::Signatures) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_crypto::types::Signatures => Signatures); #[wasm_bindgen] impl Signatures { @@ -97,11 +94,7 @@ pub struct Signature { inner: matrix_sdk_crypto::types::Signature, } -impl From for Signature { - fn from(inner: matrix_sdk_crypto::types::Signature) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_crypto::types::Signature => Signature); #[wasm_bindgen] impl Signature { @@ -129,11 +122,7 @@ pub struct MaybeSignature { inner: MaybeSignatureInner, } -impl From for MaybeSignature { - fn from(inner: MaybeSignatureInner) -> Self { - Self { inner } - } -} +impl_from_to_inner!(MaybeSignatureInner => MaybeSignature); #[wasm_bindgen] impl MaybeSignature { diff --git a/bindings/matrix-sdk-crypto-js/src/verification.rs b/bindings/matrix-sdk-crypto-js/src/verification.rs index dd7134890..b82fb5fb9 100644 --- a/bindings/matrix-sdk-crypto-js/src/verification.rs +++ b/bindings/matrix-sdk-crypto-js/src/verification.rs @@ -14,7 +14,7 @@ use wasm_bindgen::prelude::*; use crate::{ future::future_to_promise, identifiers::{DeviceId, RoomId, UserId}, - requests, + impl_from_to_inner, requests, }; /// List of available verification methods. @@ -136,11 +136,7 @@ pub struct Sas { inner: matrix_sdk_crypto::Sas, } -impl From for Sas { - fn from(inner: matrix_sdk_crypto::Sas) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_crypto::Sas => Sas); #[wasm_bindgen] impl Sas { @@ -376,11 +372,7 @@ pub struct Qr { } #[cfg(feature = "qrcode")] -impl From for Qr { - fn from(inner: matrix_sdk_crypto::QrVerification) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_crypto::QrVerification => Qr); #[cfg(feature = "qrcode")] #[wasm_bindgen] @@ -555,11 +547,7 @@ pub struct CancelInfo { inner: matrix_sdk_crypto::CancelInfo, } -impl From for CancelInfo { - fn from(inner: matrix_sdk_crypto::CancelInfo) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_crypto::CancelInfo => CancelInfo); #[wasm_bindgen] impl CancelInfo { @@ -694,11 +682,7 @@ pub struct Emoji { inner: matrix_sdk_crypto::Emoji, } -impl From for Emoji { - fn from(inner: matrix_sdk_crypto::Emoji) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_crypto::Emoji => Emoji); #[wasm_bindgen] impl Emoji { @@ -731,11 +715,7 @@ impl fmt::Debug for QrCode { } #[cfg(feature = "qrcode")] -impl From for QrCode { - fn from(inner: matrix_sdk_qrcode::qrcode::QrCode) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_qrcode::qrcode::QrCode => QrCode); #[cfg(feature = "qrcode")] #[wasm_bindgen] @@ -762,11 +742,7 @@ pub struct QrCodeScan { } #[cfg(feature = "qrcode")] -impl From for QrCodeScan { - fn from(inner: matrix_sdk_qrcode::QrVerificationData) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_qrcode::QrVerificationData => QrCodeScan); #[cfg(feature = "qrcode")] #[wasm_bindgen] @@ -797,11 +773,7 @@ pub struct VerificationRequest { inner: matrix_sdk_crypto::VerificationRequest, } -impl From for VerificationRequest { - fn from(inner: matrix_sdk_crypto::VerificationRequest) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_crypto::VerificationRequest => VerificationRequest); #[wasm_bindgen] impl VerificationRequest { @@ -1078,25 +1050,21 @@ impl VerificationRequest { // JavaScript has no complex enums like Rust. To return structs of // different types, we have no choice that hiding everything behind a // `JsValue`. -pub(crate) struct OutgoingVerificationRequest( - pub(crate) matrix_sdk_crypto::OutgoingVerificationRequest, -); - -impl From for OutgoingVerificationRequest { - fn from(inner: matrix_sdk_crypto::OutgoingVerificationRequest) -> Self { - Self(inner) - } +pub(crate) struct OutgoingVerificationRequest { + pub(crate) inner: matrix_sdk_crypto::OutgoingVerificationRequest, } +impl_from_to_inner!(matrix_sdk_crypto::OutgoingVerificationRequest => OutgoingVerificationRequest); + impl TryFrom for JsValue { type Error = serde_json::Error; fn try_from(outgoing_request: OutgoingVerificationRequest) -> Result { use matrix_sdk_crypto::OutgoingVerificationRequest::*; - let request_id = outgoing_request.0.request_id().to_string(); + let request_id = outgoing_request.inner.request_id().to_string(); - Ok(match outgoing_request.0 { + Ok(match outgoing_request.inner { ToDevice(request) => { JsValue::from(requests::ToDeviceRequest::try_from((request_id, &request))?) } diff --git a/bindings/matrix-sdk-crypto-js/src/vodozemac.rs b/bindings/matrix-sdk-crypto-js/src/vodozemac.rs index e8b8cf2a1..cb9896e01 100644 --- a/bindings/matrix-sdk-crypto-js/src/vodozemac.rs +++ b/bindings/matrix-sdk-crypto-js/src/vodozemac.rs @@ -2,6 +2,8 @@ use wasm_bindgen::prelude::*; +use crate::impl_from_to_inner; + /// An Ed25519 public key, used to verify digital signatures. #[wasm_bindgen] #[derive(Debug, Clone)] @@ -25,11 +27,7 @@ impl Ed25519PublicKey { } } -impl From for Ed25519PublicKey { - fn from(inner: vodozemac::Ed25519PublicKey) -> Self { - Self { inner } - } -} +impl_from_to_inner!(vodozemac::Ed25519PublicKey => Ed25519PublicKey); /// An Ed25519 digital signature, can be used to verify the /// authenticity of a message. @@ -39,11 +37,7 @@ pub struct Ed25519Signature { pub(crate) inner: vodozemac::Ed25519Signature, } -impl From for Ed25519Signature { - fn from(inner: vodozemac::Ed25519Signature) -> Self { - Self { inner } - } -} +impl_from_to_inner!(vodozemac::Ed25519Signature => Ed25519Signature); #[wasm_bindgen] impl Ed25519Signature { @@ -85,11 +79,7 @@ impl Curve25519PublicKey { } } -impl From for Curve25519PublicKey { - fn from(inner: vodozemac::Curve25519PublicKey) -> Self { - Self { inner } - } -} +impl_from_to_inner!(vodozemac::Curve25519PublicKey => Curve25519PublicKey); /// Struct holding the two public identity keys of an account. #[wasm_bindgen(getter_with_clone)] @@ -122,11 +112,7 @@ pub struct DeviceKey { inner: matrix_sdk_crypto::types::DeviceKey, } -impl From for DeviceKey { - fn from(inner: matrix_sdk_crypto::types::DeviceKey) -> Self { - Self { inner } - } -} +impl_from_to_inner!(matrix_sdk_crypto::types::DeviceKey => DeviceKey); #[wasm_bindgen] impl DeviceKey {