View Source BorshEx.Schema behaviour (borsh_ex v0.1.0)

Define a Borsh schema for a given struct

Example

defmodule Data do
  use BorshEx.Data
  defstruct id: nil, sub_data: nil

  borsh_schema do
    field :id, "u16"
    field :sub_data, SubData
  end
end

Link to this section Summary

Callbacks

Deserialize bitstring into the struct

Serialize struct into a bitstring

Functions

Defines a schema to serialize / deserialize the struct

Defines a field on the schema with given name and type.

Link to this section Callbacks

Specs

deserialize(bitstring :: bitstring()) ::
  {:pk, struct()} | {:error, struct(), bitstring()}

Deserialize bitstring into the struct

Example

iex> FakeData.deserialize(<<255, 20, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 49, 50, 51>>)
iex> {:ok, %FakeData{a: 255, b: 20, c: "123"}}

iex> FakeData.deserialize(<<255, 20, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 49, 50, 51, 87>>)
iex> {:error, %FakeData{a: 255, b: 20, c: "123"}, <<87>>}

Specs

serialize(struct :: struct()) :: bitstring()

Serialize struct into a bitstring

Example

iex> fake_data = %FakeData{a: 255, b: 20, c: "123"}
iex> FakeData.serialize(fake_data)
<<255, 20, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 49, 50, 51>>

Link to this section Functions

Link to this macro

borsh_schema(list)

View Source (macro)

Defines a schema to serialize / deserialize the struct

Link to this macro

field(name, type)

View Source (macro)

Defines a field on the schema with given name and type.