wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

witgen to automatically generate wit files from rust

Open bnjjj opened this issue 4 years ago • 6 comments

Hello, I made this tool https://github.com/bnjjj/witgen to be able to automatically generate record, type, function, variant, ... from your rust code.

Maybe it could help some people with the same needs.

Also feel free to let me know if I made a mistake or if you think it's a bad idea :)

Thanks for your work I enjoy using wit-bindgen even if it's not stable yet .

bnjjj avatar Dec 07 '21 17:12 bnjjj

It looks like a fun idea! The question I have is about this might fit into a developer workflow. If you write Rust, and use witgen to generate a wit file from it, would you then need to run wit-bindgen and generate Rust bindings? But then you're not using your original code, unless I'm misunderstanding how it works.

sunfishcode avatar Dec 09 '21 00:12 sunfishcode

Yes the only purpose of witgen(for now) is to generate the wit files. Indeed in our developer workflow we didn't wanted to maintain or keep in sync our code and also the .wit file. By using witgen we never touch the wit file. But next step I have in mind is to automatically use or generate the right trait impl for wit-bindgen and then make directly the link with the generated rust (using the Input trait). But for now our main goal was mainly to not maintain the wit file

bnjjj avatar Dec 09 '21 15:12 bnjjj

So for now, it seems like the main thing this gives you is that you can write Rust syntax instead of wit syntax; is that right? If so: wit is a new syntax, and we're really interested in feedback on it. It's fine if the answer is you'd just prefer to write Rust, but if there are any specific things you can identify about wit that make it hard to work with, I encourage you to share them.

sunfishcode avatar Dec 09 '21 15:12 sunfishcode

About feedbacks, not a lot for now except it's really good to have expected and option as part of the spec. I'm just wondering how can I do to use a HashMap in wit ? It seems impossible. Maybe I'm wrong

bnjjj avatar Dec 21 '21 09:12 bnjjj

Currently interface types doesn't have a map type, the closest equivalent is a list-of-pairs. If you're interested in seeing that added I'd recommend opening an issue in the interface-types repository.

alexcrichton avatar Dec 21 '21 15:12 alexcrichton

About feedbacks, not a lot for now except it's really good to have expected and option as part of the spec.

:smiley:

I'm just wondering how can I do to use a HashMap in wit ? It seems impossible. Maybe I'm wrong

If you know the set of keys you'll recognize in advance, you can use a record with the fields as keys, with option for keys that may be omitted. It's worth making an effort to use this form, as it'll give the most efficient and ergonomic bindings. If you don't know the keys in advance, currently the best option I know of is list<tuple<key, value>>.

sunfishcode avatar Dec 21 '21 15:12 sunfishcode

I'm going to close this since I think we're not at a point for integrating this into this repository but otherwise it's a neat idea! I think it will make sense one day to reevaluate bringing this into the main set of bindings but for now I'm going to try to clean up the issue tracker a bit.

alexcrichton avatar Sep 06 '22 18:09 alexcrichton