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

Support component enum, variant, flags types #397

Merged
merged 3 commits into from
Nov 14, 2024
Merged

Conversation

jbourassa
Copy link
Collaborator

@jbourassa jbourassa commented Nov 3, 2024

Add support for enum, variant and flags component model types.

  • enum: maps to a Ruby String. The string is not validated at convert time given Wasmtime will validate it at a later point. By adding an extra validation, we'd incur the validation cost twice. The tradeoff here is that the error happens outside of the conversion where we can no longer add additional context to the error message (e.g. list of valid values, which arg the error occurred in, etc.)
  • variant: maps to a Wasmtime::Component::Variant similar to result.
  • flags: maps to a Ruby Array of Strings, reflecting Val::Flags which wraps a Vec<String>.

Decision: the enum string is not validated against the list of possible
values. The rationale is that Wasmtime will validate it at a later
point, thus by adding an extra validation we're incurring the cost
twice.

The tradeoff is that we need to rely on Wasmtime's error message which
does not include the list of valid values.

An implementation of validation would look like:
```rust
// SAFETY: we're only calling into Ruby after we're done using the str
let rstring_str = unsafe { rstring.as_str()? };
if !enum_.names().any(|name| name == rstring_str) {
    return err!(
        "invalid enum value \"{}\", valid values: {:?}",
        rstring_str,
        RArray::from_iter(enum_.names())
    );
}
```

And the test:
```ruby
["enum", "no", /invalid enum value "no", valid values: \["s", "m", "l"\]/],
```
Flags are passed to/from Ruby as `Array<String>`, matching the
Wasmtime's `Val::Flags`.

Similar to enum types, the strings are not matched against valid flags
given the rust layer does when lowering the flags.
@jbourassa jbourassa marked this pull request as ready for review November 3, 2024 19:25
@jbourassa jbourassa changed the base branch from components-result to main November 3, 2024 19:26
@jbourassa jbourassa merged commit 5167d9e into main Nov 14, 2024
23 checks passed
@jbourassa jbourassa deleted the components-more-types branch November 14, 2024 03:37
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.

2 participants