codama-idl / codama

Generate clients, CLIs, documentation and more from your Solana programs
MIT License
73 stars 16 forks source link

Support bit granularity in type nodes #93

Open lorisleiva opened 3 months ago

lorisleiva commented 3 months ago

This is a proposal that needs further discussion before implementing.


Currently, the smallest granularity that Codama supports when it comes to type definition is one byte.

However, there may be cases were, within a byte, some bits are being used for different things. For instance, a byte could be used to store 8 booleans using bit flags.

There are several ways we could achieve this.

From one end of the spectrum, we could analyse the concrete use-cases where such thing would be needed and offer specific type nodes from them. For instance, a BitFlagTypeNode could represent an array or struct of booleans such that each boolean is store in a single bit.

On the other end of the spectrum, we could reduce the overall number granularity supported by Codama by adding number formats such as u1, u2, etc. This makes the whole type system a lot more flexible as we can now construct things like bit flats using TupleTypeNodes or even StructTypeNodes if we wanted them to be named. The issue with the approach is we now need to handle bit-granularity at every level (Codecs for JS, custom types for Rust, etc.) and ensure we do not end up with incomplete bytes.