Giter Site home page Giter Site logo

bcs's Introduction

Bcs

Pure Elixir encoder/decoder for BCS format.

Features

Encoder/Decoder for:

  • Booleans
  • Signed 8-bit, 16-bit, 32-bit, 64-bit, and 128-bit integers
  • Unsigned 8-bit, 16-bit, 32-bit, 64-bit, and 128-bit integers
  • Option
  • Unit (an empty value)
  • Fixed and variable length sequences
  • UTF-8 Encoded Strings
  • Tuples
  • Structures (aka “structs”)
  • Externally tagged enumerations (aka “enums”)
  • Maps

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

my_struct = %MyStruct{
  label: "hello",
  chars: 'abcd',
  boolean: true,
  maps: %{1 => "1", 2 => "2"},
  field: "this field will be ignored"
}

# encode
my_struct
|> Bcs.encode!()
# then decode
|> Bcs.decode!(MyStruct)

Define field Types

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

Also we have a special type for treating [u8] as binary instead of charlist

[u8] | [:byte] [u8; N] | [:byte \| n]

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.

bcs's People

Contributors

kabie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.