mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-14 11:05:32 -04:00
chore(crypto-js): Simplify code with a lovely macro.
This commit is contained in:
@@ -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<matrix_sdk_crypto::Device> 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<matrix_sdk_crypto::UserDevices> 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 {
|
||||
|
||||
@@ -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<ruma::OwnedUserId> 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<ruma::OwnedDeviceId> 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<ruma::OwnedDeviceKeyId> 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<ruma::DeviceKeyAlgorithm> 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<ruma::OwnedRoomId> for RoomId {
|
||||
fn from(inner: ruma::OwnedRoomId) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
}
|
||||
impl_from_to_inner!(ruma::OwnedRoomId => RoomId);
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl RoomId {
|
||||
|
||||
@@ -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;
|
||||
|
||||
33
bindings/matrix-sdk-crypto-js/src/macros.rs
Normal file
33
bindings/matrix-sdk-crypto-js/src/macros.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
/// We have the following pattern quite often in our code:
|
||||
///
|
||||
/// ```rust
|
||||
/// struct Foo {
|
||||
/// inner: Bar,
|
||||
/// }
|
||||
///
|
||||
/// impl From<Bar> 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 }
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -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<matrix_sdk_crypto::olm::CrossSigningStatus> 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 {
|
||||
|
||||
@@ -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<matrix_sdk_crypto::store::CrossSigningKeyExport> 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 {
|
||||
|
||||
@@ -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<matrix_sdk_crypto::types::Signatures> 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<matrix_sdk_crypto::types::Signature> 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<MaybeSignatureInner> for MaybeSignature {
|
||||
fn from(inner: MaybeSignatureInner) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
}
|
||||
impl_from_to_inner!(MaybeSignatureInner => MaybeSignature);
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl MaybeSignature {
|
||||
|
||||
@@ -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<matrix_sdk_crypto::Sas> 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<matrix_sdk_crypto::QrVerification> 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<matrix_sdk_crypto::CancelInfo> 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<matrix_sdk_crypto::Emoji> 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<matrix_sdk_qrcode::qrcode::QrCode> 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<matrix_sdk_qrcode::QrVerificationData> 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<matrix_sdk_crypto::VerificationRequest> 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<matrix_sdk_crypto::OutgoingVerificationRequest> 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<OutgoingVerificationRequest> for JsValue {
|
||||
type Error = serde_json::Error;
|
||||
|
||||
fn try_from(outgoing_request: OutgoingVerificationRequest) -> Result<Self, Self::Error> {
|
||||
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))?)
|
||||
}
|
||||
|
||||
@@ -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<vodozemac::Ed25519PublicKey> 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<vodozemac::Ed25519Signature> 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<vodozemac::Curve25519PublicKey> 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<matrix_sdk_crypto::types::DeviceKey> 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 {
|
||||
|
||||
Reference in New Issue
Block a user