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
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 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 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
Defines a schema to serialize / deserialize the struct
Defines a field on the schema with given name and type.