Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly encode Duration and optional fields #72

Merged
merged 11 commits into from
Jan 22, 2024

Conversation

uhoreg
Copy link
Member

@uhoreg uhoreg commented Dec 14, 2023

fixes #56, #57, and (hopefully) element-hq/element-web#26566

I ported matrix-org/matrix-rust-sdk#817 over, and added another parameter to mark fields as optional if they're Option<...>, to avoid sticking null in responses. It's kind of ugly, so suggestions welcome.

I'm not sure what's the best way to test this. All the existing tests are in JS, and as far as I can tell, from JS the only way to get these structs is to get them from OlmMachine, which means coercing OlmMachine to spit out the right requests. Otherwise, I could write a test in Rust that creates the ruma request struct and test that it transforms into the wasm request struct correctly, but that would mean adding another testing system. That's not too bad since Rust has its own testing built-in, but I don't know if we care about keeping the tests in JS.

@@ -316,7 +318,7 @@ macro_rules! request {
(
$destination_request:ident from $source_request:ident
$( extracts $( $field_name:ident : $field_type:tt ),+ $(,)? )?
$( $( and )? groups $( $grouped_field_name:ident ),+ $(,)? )?
$( $( and )? groups $( $grouped_field_name:ident $( { $transformation:expr } )? $( $optional:literal )? ),+ $(,)? )?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm adding "optional" to flag things as optional. But the match will match any literal, which is suboptimal. I'd prefer to match just optional, but I don't see any way of doing that. If I use $( optional ), then that would match just optional, but then I don't have a way to expand that back out it the transcription below, because it isn't assigned to a metavariable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we check at runtime that the token is optional? It would be nice to have some kind of check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or I guess you could have two alternatives, one with optional and one without, and give the metavariables different names? I've not done many of these macros so I'm just guessing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hywan as original author of the request! macro, do you have any suggestions for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about simply optional?

$( $( and )? groups $( $grouped_field_name:ident $( { $transformation:expr } )? $( optional )? ),+ $(,)? )?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I tried doing $( optional )?, but I couldn't figure out how to handle the expansion so that it does something different depending on whether it's specified or not, because it isn't bound to a metavariable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep things as they are now, I may update this later.

@uhoreg
Copy link
Member Author

uhoreg commented Dec 15, 2023

Otherwise, I could write a test in Rust that creates the ruma request struct and test that it transforms into the wasm request struct correctly, but that would mean adding another testing system. That's not too bad since Rust has its own testing built-in, but I don't know if we care about keeping the tests in JS.

Just for fun, I decided to see what would happen if I ran cargo test, and I got

    Finished test [unoptimized + debuginfo] target(s) in 56.12s
     Running unittests src/lib.rs (target/wasm32-unknown-unknown/debug/deps/matrix_sdk_crypto_wasm-a46182b35ca3089b.wasm)
error: test failed, to rerun pass `--lib`

Caused by:
  could not execute process `/home/hubert/Projects/matrix/matrix-rust-sdk-crypto-wasm/target/wasm32-unknown-unknown/debug/deps/matrix_sdk_crypto_wasm-a46182b35ca3089b.wasm` (never executed)

Caused by:
  Exec format error (os error 8)

which suggests that doing Rust tests will not be as trivial as I thought it would be.

@uhoreg
Copy link
Member Author

uhoreg commented Dec 15, 2023

I figured out a way to test the conversions, and it's only moderately ugly!

src/requests.rs Outdated

use super::{KeysClaimRequest, KeysQueryRequest, KeysUploadRequest};

#[wasm_bindgen(js_name = "_test_make_keys_claim_request")]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deliberately used ugly names here since they're only intended for testing

@uhoreg uhoreg marked this pull request as ready for review December 15, 2023 23:05
@uhoreg uhoreg requested a review from a team as a code owner December 15, 2023 23:05
@uhoreg uhoreg requested a review from andybalaam December 15, 2023 23:05
Copy link
Member

@andybalaam andybalaam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found these macros very difficult to understand, but as far as I did understand, this looks sensible, and the tests appear to demonstrate it works :-)

@@ -316,7 +318,7 @@ macro_rules! request {
(
$destination_request:ident from $source_request:ident
$( extracts $( $field_name:ident : $field_type:tt ),+ $(,)? )?
$( $( and )? groups $( $grouped_field_name:ident ),+ $(,)? )?
$( $( and )? groups $( $grouped_field_name:ident $( { $transformation:expr } )? $( $optional:literal )? ),+ $(,)? )?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we check at runtime that the token is optional? It would be nice to have some kind of check.

@@ -316,7 +318,7 @@ macro_rules! request {
(
$destination_request:ident from $source_request:ident
$( extracts $( $field_name:ident : $field_type:tt ),+ $(,)? )?
$( $( and )? groups $( $grouped_field_name:ident ),+ $(,)? )?
$( $( and )? groups $( $grouped_field_name:ident $( { $transformation:expr } )? $( $optional:literal )? ),+ $(,)? )?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or I guess you could have two alternatives, one with optional and one without, and give the metavariables different names? I've not done many of these macros so I'm just guessing.

src/requests.rs Outdated Show resolved Hide resolved
@@ -316,7 +318,7 @@ macro_rules! request {
(
$destination_request:ident from $source_request:ident
$( extracts $( $field_name:ident : $field_type:tt ),+ $(,)? )?
$( $( and )? groups $( $grouped_field_name:ident ),+ $(,)? )?
$( $( and )? groups $( $grouped_field_name:ident $( { $transformation:expr } )? $( $optional:literal )? ),+ $(,)? )?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about simply optional?

$( $( and )? groups $( $grouped_field_name:ident $( { $transformation:expr } )? $( optional )? ),+ $(,)? )?

@uhoreg uhoreg requested a review from Hywan January 10, 2024 18:57
Cargo.toml Outdated Show resolved Hide resolved
@@ -316,7 +318,7 @@ macro_rules! request {
(
$destination_request:ident from $source_request:ident
$( extracts $( $field_name:ident : $field_type:tt ),+ $(,)? )?
$( $( and )? groups $( $grouped_field_name:ident ),+ $(,)? )?
$( $( and )? groups $( $grouped_field_name:ident $( { $transformation:expr } )? $( $optional:literal )? ),+ $(,)? )?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep things as they are now, I may update this later.

@uhoreg uhoreg requested a review from Hywan January 20, 2024 19:30
@Hywan Hywan merged commit 33d531a into matrix-org:main Jan 22, 2024
3 checks passed
richvdh added a commit that referenced this pull request Jan 23, 2024
PR #72 landed in 4.0.1, not 4.0.0.
richvdh added a commit that referenced this pull request Jan 23, 2024
PR #72 landed in 4.0.1, not 4.0.0.

Also: add UNRELEASED section
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

KeysClaimRequest generates malformed bodies
4 participants