BARE encoding library

I do not know how many of you are familiar with BARE encoding format which was designed by Drew DeVault. It is super simple format for binary encoding of messages with structure known ahead-of-time (so it isn’t self-descriptive like CBOR). Now it got Erlang implementation (currently without compiler for building structure definition form text definitions).

Library usage is pretty simple (here with Elixir syntax):

spec = {:struct, [foo: :uint, bar: :string]}

encoded = :bare.encode(%{foo: 10, bar: "Hełło"}, spec)
:bare.decode(encoded, spec)
#=> {:ok, %{foo: 10, bar: "Hełło"}, <<>>}

Supported types are:

  • :uint/:int - variable width unsigned/signed integer (specs limit size of these integers to max 64-bit integers)
  • :u8/:u16/:u32/:u64 - fixed width unsigned integers
  • :i8/:i16/:i32/:i64 - fixed width signed integers
  • :bool - boolean values (aka true and false atoms)
  • :f32/:f64 - floating points of given size, currently encoding is limited to finite values, but encoding supports not-finite values which are returned as atoms
  • {:enum, [atom(), ...]} - which will encode known set of atoms as uint
  • :data and data<N> - which will encode arbitrary/fixed-length binaries
  • :string - UTF-8 encoded chartist or binary
  • :void - empty type that can encode only []
  • {:optional, type} - optional value of type
  • {:array, type} - list of type
  • {:array, type, size} - fixed length list of type
  • {:map, key_type, value_type} - map with keys of key_type and values of value_type
  • {:union, [type(), ...]} - tagged union of types which can encode values in form of {type(), data} where data is of type()
  • {:struct, keyword(type())} - structure with given keys that can encode maps with atom keys and values of specified types

Let me know what you think about it, either here or via mailing list.


In future I plan to implement web tokens with BARE and PASETO to make web tokens implementation that for me will be near ideal solution. I will ping you back when I will have something done.

5 Likes