Skip to content

Commit

Permalink
feat: Remove the need for try_array_to_vec.
Browse files Browse the repository at this point in the history
Now that `wasm-bindgen` supports `Vec<T>` and `Option<Vec<T>>`, we no
longer need `try_array_to_vec`. This patch removes this helper function.
  • Loading branch information
Hywan committed Jan 8, 2024
1 parent 60cd09c commit c1a12eb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
12 changes: 6 additions & 6 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use crate::{
encryption::EncryptionAlgorithm,
future::future_to_promise,
identifiers::{self, DeviceId, UserId},
impl_from_to_inner,
js::try_array_to_vec,
requests, types, verification, vodozemac,
impl_from_to_inner, requests, types, verification, vodozemac,
};

/// A device represents a E2EE capable client of an user.
Expand All @@ -28,10 +26,12 @@ impl Device {
/// Returns a Promise for a 2-element array `[VerificationRequest,
/// ToDeviceRequest]`.
#[wasm_bindgen(js_name = "requestVerification")]
pub fn request_verification(&self, methods: Option<Array>) -> Result<Promise, JsError> {
let methods =
methods.map(try_array_to_vec::<verification::VerificationMethod, _>).transpose()?;
pub fn request_verification(
&self,
methods: Option<Vec<verification::VerificationMethod>>,
) -> Result<Promise, JsError> {
let me = self.inner.clone();
let methods = methods.map(|methods| methods.iter().map(Into::into).collect());

Ok(future_to_promise(async move {
let tuple = Array::new();
Expand Down
26 changes: 13 additions & 13 deletions src/identities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
use js_sys::{Array, Promise};
use wasm_bindgen::prelude::*;

use crate::{
future::future_to_promise, identifiers, impl_from_to_inner, js::try_array_to_vec, requests,
verification,
};
use crate::{future::future_to_promise, identifiers, impl_from_to_inner, requests, verification};

pub(crate) struct UserIdentities {
inner: matrix_sdk_crypto::UserIdentities,
Expand Down Expand Up @@ -60,9 +57,11 @@ impl OwnUserIdentity {

/// Send a verification request to our other devices.
#[wasm_bindgen(js_name = "requestVerification")]
pub fn request_verification(&self, methods: Option<Array>) -> Result<Promise, JsError> {
let methods =
methods.map(try_array_to_vec::<verification::VerificationMethod, _>).transpose()?;
pub fn request_verification(
&self,
methods: Option<Vec<verification::VerificationMethod>>,
) -> Result<Promise, JsError> {
let methods = methods.map(|methods| methods.iter().map(Into::into).collect());
let me = self.inner.clone();

Ok(future_to_promise(async move {
Expand Down Expand Up @@ -164,13 +163,12 @@ impl UserIdentity {
&self,
room_id: &identifiers::RoomId,
request_event_id: &identifiers::EventId,
methods: Option<Array>,
methods: Option<Vec<verification::VerificationMethod>>,
) -> Result<Promise, JsError> {
let me = self.inner.clone();
let room_id = room_id.inner.clone();
let request_event_id = request_event_id.inner.clone();
let methods =
methods.map(try_array_to_vec::<verification::VerificationMethod, _>).transpose()?;
let methods = methods.map(|methods| methods.iter().map(Into::into).collect());

Ok(future_to_promise::<_, verification::VerificationRequest>(async move {
Ok(me
Expand All @@ -188,10 +186,12 @@ impl UserIdentity {
/// After the content has been sent out a VerificationRequest can be started
/// with the `request_verification` method.
#[wasm_bindgen(js_name = "verificationRequestContent")]
pub fn verification_request_content(&self, methods: Option<Array>) -> Result<Promise, JsError> {
pub fn verification_request_content(
&self,
methods: Option<Vec<verification::VerificationMethod>>,
) -> Result<Promise, JsError> {
let me = self.inner.clone();
let methods =
methods.map(try_array_to_vec::<verification::VerificationMethod, _>).transpose()?;
let methods = methods.map(|methods| methods.iter().map(Into::into).collect());

Ok(future_to_promise(async move {
Ok(serde_json::to_string(&me.verification_request_content(methods).await)?)
Expand Down
13 changes: 1 addition & 12 deletions src/js.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use js_sys::{Array, Object, Reflect};
use js_sys::{Object, Reflect};
use wasm_bindgen::{convert::RefFromWasmAbi, prelude::*};

/// A really hacky and dirty code to downcast a `JsValue` to `T:
Expand Down Expand Up @@ -27,14 +27,3 @@ where
)))
}
}

/// Transform a value `JS` from JavaScript to a Rust wrapper, to a
/// Rust wrapped type.
pub(crate) fn try_array_to_vec<JS, Rust>(
array: Array,
) -> Result<Vec<Rust>, <JS as TryFrom<JsValue>>::Error>
where
JS: TryFrom<JsValue> + Into<Rust>,
{
array.iter().map(|item| JS::try_from(item).map(Into::into)).collect()
}
25 changes: 20 additions & 5 deletions src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::{
future::future_to_promise,
identifiers::{DeviceId, RoomId, UserId},
impl_from_to_inner,
js::try_array_to_vec,
machine::promise_result_to_future,
requests,
};
Expand Down Expand Up @@ -63,6 +62,19 @@ impl TryFrom<JsValue> for VerificationMethod {
}
}

impl From<&VerificationMethod> for RumaVerificationMethod {
fn from(value: &VerificationMethod) -> Self {
use VerificationMethod::*;

match value {
SasV1 => Self::SasV1,
QrCodeScanV1 => Self::QrCodeScanV1,
QrCodeShowV1 => Self::QrCodeShowV1,
ReciprocateV1 => Self::ReciprocateV1,
}
}
}

impl From<VerificationMethod> for RumaVerificationMethod {
fn from(value: VerificationMethod) -> Self {
use VerificationMethod::*;
Expand Down Expand Up @@ -785,9 +797,9 @@ impl VerificationRequest {
own_user_id: &UserId,
own_device_id: &DeviceId,
other_user_id: &UserId,
methods: Option<Array>,
methods: Option<Vec<VerificationMethod>>,
) -> Result<String, JsError> {
let methods = methods.map(try_array_to_vec::<VerificationMethod, _>).transpose()?;
let methods = methods.map(|methods| methods.iter().map(Into::into).collect());

Ok(serde_json::to_string(&matrix_sdk_crypto::VerificationRequest::request(
&own_user_id.inner,
Expand Down Expand Up @@ -974,8 +986,11 @@ impl VerificationRequest {
/// It returns either a `ToDeviceRequest`, a `RoomMessageRequest`
/// or `undefined`.
#[wasm_bindgen(js_name = "acceptWithMethods")]
pub fn accept_with_methods(&self, methods: Array) -> Result<JsValue, JsError> {
let methods = try_array_to_vec::<VerificationMethod, _>(methods)?;
pub fn accept_with_methods(
&self,
methods: Vec<VerificationMethod>,
) -> Result<JsValue, JsError> {
let methods = methods.iter().map(Into::into).collect();

self.inner
.accept_with_methods(methods)
Expand Down

0 comments on commit c1a12eb

Please sign in to comment.