codama-idl/codama

Use a single EnumVariantTypeNode that can use any TypeNode as data

lorisleiva opened this issue · 0 comments

This is a proposal that needs further discussion before implementing.


Currently, we have three kinds of EnumVariantTypeNode:

  • EmptyEnumVariantTypeNode: For variants with no data, e.g. Message.Quit.
  • TupleEnumVariantTypeNode: For variants with data of type TupleTypeNode, e.g. Message.Move(x, y).
  • StructEnumVariantTypeNode: For variants with data of type StructTypeNode, e.g. Message.Login { username, password }.

This is mimicking the Rust behaviour but the Codama IDL is language agnostic and should be use to describe data structure regardless of the language or framework used by the program or clients.

As such we could instead combine these three nodes into a single EnumVariantTypeNode that can optionally accept any TypeNode.

{
  kind: 'enumVariantTypeNode',
  name: string,
  type?: TypeNode,
}

Now, say we had to use that node to render a Rust enum. We could use the following logic to achieve this taks:

  • No type attribute → Render an empty variant.
  • type attribute of type StructTypeNode → Render a struct variant.
  • type attribute of type TupleTypeNode → Render a tuple variant.
  • type attribute of any other type → Render a tuple variant such that it's only item is that type.

Additionally, we could consider accepting a DefinedTypeLinkNode on top of TypeNode which would allow enum variants to link to defined types instead of having to wrap them in TupleTypeNodes to achieve this.