-
Notifications
You must be signed in to change notification settings - Fork 176
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
wit records defined with reserved rust keywords cannot be used with serde additional derives #927
Comments
Thanks for the report! Using raw identifiers would be one solution but I'd personally prefer to go the route of adding |
Would prefer |
I'm going to throw my hat in the ring for using raw identifiers. It's not the most pretty solution, but I do think it strikes the right balance of reflecting the wit identifiers while being idiomatic. The true idiomatic approach in Rust is to avoid using keywords as identifiers or to use alternate spellings (e.g., Allowing for |
That's a good point that a trailing underscore isn't idiomatic, so given that I think you're right that |
Summary of Issue
When defining a wit record with a reserved rust keyword, and making use of serde as an additional
derives
, the generated rustbindings.rs
uses an_
suffix on field names which prevents serde from being able to correctly deserialize valid JSON blobs.Steps to Reproduce
There's probably a simpler way to reproduce this, but I'm most familiar with components, so that's what I've documented here 😅
Create two new components
The following commands will create a new
lib
component and a newcommand
component.Each of these will commands will generate a new directory structure in your current working directory.
Create a file
bug-report.wit
Create this file within your current working directory. We will reference it in the following
Cargo.toml
files.Note that the cargo component command will actually create a default wit file beneath the
lib
directory. You can delete this. In this simple POC, we'll use a single wit for both thelib
component and thecommand
component.type
, which happens to be a reserved keyword in both wit and rust.Update
lib
Update
lib/Cargo.toml
Update
lib/src/lib.rs
type
. As we'll see, this fails to deserialize because the generated rust code expects a field namedtype_
.Update
main
Update
main/Cargo.toml
Update
main/src/main.rs
Build
Run
Result
You'll see a runtime failure complaining of a "missing field
type_
".This failure is a result of trying to deserialize a json blob, which contains a field named
type
, into a rust struct which expects a field namedtype_
.Root Cause
You'll notice that in
lib/src/bindings.rs
, the structRecordWithKey
contains a field namedtype_
.The
type
word is a reserved word in both wit and rust. In wit, we needed to prefix the field with the%
character. In rust, adding the_
suffix obviously compiles, but it causes an issue with our serde deserialization, since serde now expects the json blob to also contain a key the exactly matches the field name, including the_
suffix.Proposed Fix
For rust keywords, would it be possible to have the generated
bindings.rs
file use raw identifiers instead of the_
suffix? I believe this would resolve our issue, since serde should correctly manage rust raw identifiers.I'm not sure how else to work around this problem - if there's a simple fix I can use on my end would love to learn about that for the short-term 😄
The text was updated successfully, but these errors were encountered: