---
source: boltffi_bindgen/src/render/typescript/templates.rs
expression: template.render().unwrap()
---
export type Shape =
  | { readonly tag: "Circle"; readonly radius: number }
  | { readonly tag: "Rectangle"; readonly width: number; readonly height: number }
  | { readonly tag: "Nothing" };

const ShapeCodec: WireCodec<Shape> = {
  size: (v) => {
    switch (v.tag) {
      case "Circle": return 4 + 8;
      case "Rectangle": return 4 + 8 + 8;
      case "Nothing": return 4;
    }
  },
  encode: (writer, v) => {
    switch (v.tag) {
      case "Circle":
        writer.writeI32(0);
        writer.writeF64(v.radius);
        break;
      case "Rectangle":
        writer.writeI32(1);
        writer.writeF64(v.width);
        writer.writeF64(v.height);
        break;
      case "Nothing":
        writer.writeI32(2);
        break;
    }
  },
  decode: (reader) => {
    const tag = reader.readI32();
    switch (tag) {
      case 0:
        return { tag: "Circle", radius: reader.readF64() };
      case 1:
        return { tag: "Rectangle", width: reader.readF64(), height: reader.readF64() };
      case 2:
        return { tag: "Nothing" };
      default: throw new Error(`Unknown Shape wire tag: ${tag}`);
    }
  },
};
