jhugman / uniffi-bindgen-react-native

A uniffi bindings generator for calling Rust from react-native
Other
0 stars 0 forks source link

Allow user configuration of enum tag classes and properties #60

Open jhugman opened 1 month ago

jhugman commented 1 month ago

Currently we have made a relatively arbitrary choice as to how to name of the generated structures.

This is most striking in the construction of tagged unions, i.e. enums with values:

enum MyEnum_Tags = {
  Foo, Bar, Baz
};

The shape of instances of MyEnum is:

{
  tag: MyEnum_Tags,
  inner: …
}

Each of these tag, inner and MyEnum_Tags might be configurable via uniffi.toml; perhaps a client may want to rename:

[bindings.typescript.enums]
tagProperty = "tag"
innerProperty = "data"
tagClass = "{}_tags"

Implementation sketch

  1. Start with adding tag_property, inner_property etc in to a TsEnumConfig struct. You will likely want to use a TemplateExpression for at least tag_class. You should put default values on this. You may want to add some methods on this, especially tag_class(&self, type_name: &str) -> String.
  2. Add this to TsConfig in uniffi_toml.rs, as enums.
  3. From the templates, you can access this method with config.enums.tag_class(type_name). Templates that mention _Tags, inner or tag are TaggedEnumTemplate.ts and `ObjectTemplate.ts.