Bcs

Pure Elixir encoder for BCS format.

Features

Encoder for:

Installation

If available in Hex, the package can be installed by adding bcs to your list of dependencies in mix.exs:

def deps do
  [
    {:bcs, "~> 0.1.0"}
  ]
end

Usage

# Define the struct
defmodule MyStruct do
  @derive {Bcs.Struct,
    label: :string,
    chars: [:u8 | 4],  # <<= we use improper list for fixed length array
    boolean: :bool,
    maps: %{:u8 => :string},
  }

  defstruct [:label, :chars, :boolean, :maps, :field]
end

# Encode
%MyStruct{
  label: "hello",
  chars: &#39;abcd&#39;,
  boolean: true,
  maps: %{1 => "1", 2 => "2"},
  field: "this field will be ignored"
} |> Bcs.encode()

Define field Types

Rust Type | Syntax -------------|------------- u8, s8, u16, u256, ... | :u8, :s8, :u16, :u256, ... bool | :boolOption<T> | [t \| nil][T] | [t][T; N] | [t \| n]String | :string(T1, T2) | {t1, t2}MyStruct | MyStructenum E | EMap<K, V> | %{k => v}

Define Tagged Enums

defmodule Foo do
  use Bcs.TaggedEnum, [
    {:variant0, :u16},
    {:variant1, :u8},
    {:variant2, :string},
    :variant3
  ]
end

Some valid values for type Foo: {:variant0, 42}, {:variant2, "hello"}, :variant3, etc.