DASL.DRISL.Encoder (dasl v0.1.0)

View Source

DRISL encoder.

Encodes Elixir terms into DRISL-compliant CBOR binary. See the spec for the full set of constraints enforced at encode time.

Spec: https://dasl.ing/drisl.html

Summary

Functions

Encodes an Elixir term into a DRISL-compliant CBOR binary.

Functions

encode(term)

@spec encode(any()) :: {:ok, binary()} | {:error, atom()}

Encodes an Elixir term into a DRISL-compliant CBOR binary.

Returns {:ok, binary} on success, or {:error, reason} on failure.

Examples

iex> DASL.DRISL.Encoder.encode(%{"a" => 1})
{:ok, <<0xa1, 0x61, 0x61, 0x01>>}

iex> DASL.DRISL.Encoder.encode([1, 2, 3])
{:ok, <<0x83, 0x01, 0x02, 0x03>>}

iex> DASL.DRISL.Encoder.encode(true)
{:ok, <<0xf5>>}

iex> DASL.DRISL.Encoder.encode(nil)
{:ok, <<0xf6>>}

iex> {:ok, cid} = DASL.CID.new("bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e")
iex> {:ok, bin} = DASL.DRISL.Encoder.encode(%{"link" => cid})
iex> is_binary(bin)
true

iex> DASL.DRISL.Encoder.encode(%{1 => "bad key"})
{:error, :non_string_map_key}