Stream: jco

Topic: wit variants vs Rust enums, via serde_json


view this post on Zulip DougAnderson444 | PeerPiper.io (Jan 11 2024 at 18:45):

When I try to make a WIT variant from Rust using serde_json from an enum using serde's "adjacently tagged", the tag value is capitalized.

But in jco the tag values are all lowercase.... meaning the switch cases won't match.

// from Rust we get Capitalized:
{ tag: "Enumfromrust", val: "some string...."}
// jco expect wit lowercase:
{ tag: "enumfromrust", val: "some string...."}

Anyone know of a straightforward way to convert between the two?

Example of Uppercase serialised enum:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a4673792be008cb23d4533da42f8cabc

Serde adj tags: https://serde.rs/enum-representations.html#adjacently-tagged

view this post on Zulip Guy Bedford (Jan 11 2024 at 18:50):

I wonder if it would be worth updating the output in Rust to use kebab-case enum tags?

view this post on Zulip Guy Bedford (Jan 11 2024 at 18:51):

in JS at least, that is the toolchain convention and per WIT semantics, toolchain-specific casing is used where appropriate

view this post on Zulip DougAnderson444 | PeerPiper.io (Jan 11 2024 at 18:55):

true, but my issue is mostly about that first Letter being Caps
I haven't found an elegant solution to make those leading letter lowercase in Rust yet, using serde anyway

view this post on Zulip DougAnderson444 | PeerPiper.io (Jan 11 2024 at 18:56):

The only serde.rs conversion I've found (builtin, so far) is camelCase https://serde.rs/attr-rename.html

view this post on Zulip Guy Bedford (Jan 11 2024 at 18:56):

the rename_all should support kebab-case

view this post on Zulip DougAnderson444 | PeerPiper.io (Jan 11 2024 at 18:57):

Yep you're right https://serde.rs/container-attrs.html#rename_all

view this post on Zulip DougAnderson444 | PeerPiper.io (Jan 11 2024 at 18:58):

AHH! There's lowercase:

The possible values are "lowercase", "UPPERCASE", "PascalCase", "camelCase", "snake_case", "SCREAMING_SNAKE_CASE", "kebab-case", "SCREAMING-KEBAB-CASE".

view this post on Zulip DougAnderson444 | PeerPiper.io (Jan 11 2024 at 18:59):

but good point about lowercase kebab, feels verrrry ...witty.

view this post on Zulip DougAnderson444 | PeerPiper.io (Jan 11 2024 at 19:51):

Works like a charm, thanks @Guy Bedford !

view this post on Zulip Notification Bot (Jan 11 2024 at 19:51):

DougAnderson444 | PeerPiper.io has marked this topic as resolved.

view this post on Zulip Notification Bot (Jan 14 2024 at 00:53):

Geoff Goodman has marked this topic as unresolved.


Last updated: Nov 22 2024 at 16:03 UTC