🐫 Faraday is a library for writing fast and memory-efficient serializers. Its core type and related operation gives the user fine-grained control over copying and allocation behavior while serializing user-defined types, and presents the output in a form that makes it possible to use vectorized write operations, such as the writev system call, or any other platform or application-specific output APIs. https://opam.ocaml.org/packages/faraday/

Spiros Eliopoulos 3ea082b23b Merge pull request #74 from inhabitedtype/fix-badge-markup 1 year ago
.github c8a29b7d09 github-actions: udpate min ocaml version to 4.03.0 1 year ago
async 50643d4210 Give explicit arguments to Monitor.try_with. 2 years ago
examples 43fac435ec dune: fix build 3 years ago
lib d8e8d80f8c fix-markup: use source code style for write_* 1 year ago
lib_test 878eaaa410 remove-check-prime: use check with unlabeled arguments. 1 year ago
lwt e521d4f703 dune: port to dune 3 years ago
lwt_unix 182dd1a9aa faraday-lwt-unix: handle EPIPE the same way as BADFD is handled 1 year ago
.gitignore 579ba2f07b update .gitignore 5 years ago
LICENSE ed5424428e initial commit 6 years ago
META.faraday.template 863f913c1a switch to jbuilder 5 years ago
Makefile e521d4f703 dune: port to dune 3 years ago
README.md 20829ef0a4 fix-badge-markup: remove trailing ] in badge markup 1 year ago
dune-project 44006ded08 Add generated dune-project 2 years ago
faraday-async.opam b5fc9750be update-subst-runtest: subst in dev, runtest with -j jobs 1 year ago
faraday-lwt-unix.opam b5fc9750be update-subst-runtest: subst in dev, runtest with -j jobs 1 year ago
faraday-lwt.opam b5fc9750be update-subst-runtest: subst in dev, runtest with -j jobs 1 year ago
faraday.opam cbce71bf0f Merge pull request #71 from inhabitedtype/update-subst-runtest 1 year ago

README.md

Faraday

Faraday is a library for writing fast and memory-efficient serializers. Its core type and related operation gives the user fine-grained control over copying and allocation behavior while serializing user-defined types, and presents the output in a form that makes it possible to use vectorized write operations, such as the writev system call, or any other platform or application-specific output APIs.

Build Status

Installation

Install the library and its depenencies via OPAM:

opam install faraday

Usage

Like its sister project Angstrom, Faraday is written with network protocols and serialization formats in mind. As such, its source distribution inclues implementations of various RFCs that are illustrative of real-world applications of the library. This includes a JSON serializer.

In addition, it's appropriate here to include a serializer for the simple arithmetic expression language described in Angstrom's README.

open Faraday

type 'a binop = [
  | `Sub of 'a * 'a
  | `Add of 'a * 'a
  | `Div of 'a * 'a
  | `Mul of 'a * 'a
]
;;

type t = [ `Num of int | t binop ]

let rec serialize ?(prec=0) t expr =
  match expr with
  | `Num n          -> write_string t (Printf.sprintf "%d" n)
  | #binop as binop ->
    let prec', op, l, r =
      match binop with
      | `Sub(l, r) -> 2, '-', l, r
      | `Add(l, r) -> 3, '+', l, r
      | `Div(l, r) -> 4, '/', l, r
      | `Mul(l, r) -> 5, '*', l, r
    in
    if prec' < prec then write_char t '(';
    serialize t ~prec:prec' l;
    write_char t op;
    serialize t ~prec:prec' r;
    if prec' < prec then write_char t ')'

let to_string expr =
  let t = create 0x1000 in
  serialize t expr;
  serialize_to_string t

Development

To install development dependencies, pin the package from the root of the repository:

opam pin add -n faraday .
opam install --deps-only faraday

After this, you may install a development version of the library using the install command as usual.

For building and running the tests during development, you will need to install the alcotest package:

opam install alcotest
make test

License

BSD3, see LICENSE file for its text.