bincode-typescript 0.1.0

Generates TypeScript serialisation and deserialisation code from Rust structs and enums
Documentation

export interface {{ident}} {
{%- if self.is_tuple() -%}
  {% for (id, ty) in fields %}
  {{ loop.index - 1 }}: {{ ty.ts_type() }};
  {%- endfor %} 
{%- else -%}
  {% for (id, ty) in fields %}
  {{ id.clone().unwrap() }}: {{ ty.ts_type() }};
  {%- endfor %} 
{%- endif %}
}

export function write{{ident}}(value: {{ident}}, sinkOrBuf?: SinkOrBuf): Sink {
  const sink = Sink.create(sinkOrBuf);
{%- for (id, ty) in fields -%}
  {%- match id -%}
    {%- when Some with (id) %}
  {{ ty.writer() }}(value.{{id}}, sink);
    {%- when None %}
  {{ ty.writer() }}(value[{{loop.index - 1}}], sink);
  {%- endmatch%}
{%- endfor %} 
  return sink;
}

export function read{{ident}}(sinkOrBuf: SinkOrBuf): {{ident}} {
  const sink = Sink.create(sinkOrBuf);
  {% if self.is_tuple() -%}
  return [
    {%- for (id, ty) in fields %}
    {{ ty.reader() }}(sink),
    {%- endfor %} 
  ];
  {%- else -%}
  return {
    {%- for (id, ty) in fields %}
    {{id.clone().unwrap()}}: {{ ty.reader() }}(sink),
    {%- endfor %} 
  };
  {%- endif %}
}