Coder Social home page Coder Social logo

farfadet's Introduction

Farfadet, a printf-like for Faraday library

To use this library, you can pin it with faraday:

opam pin add faraday https://github.com/inhabitedtyped/faraday.git
opam pin add farfadet https://github.com/oklm-wsh/Farfadet.git

Quick look

Firstly, you need to understand what is Faraday. Then, you can serialize something like a this data:

  type tz_offset =
    { sign    : [ `Plus | `Minus ]
    ; hours   : int
    ; minutes : int }
  type user =
    { name  : string
    ; email : string
    ; date  : int64 * tz_offset option }

You can write something with Faraday, like that:

let write_user enc user =
  Faraday.write_string enc user.name;
  Faraday.write_char enc ' ';
  Faraday.write_string enc user.email;
  Faraday.write_char enc ' ';

  ...

  Faraday.yield enc

And it's boring ... Yes. So, Farfadet can help you to write a serializer in a type safe way. This is an example:

open Farfadet
open Farfadet.Infix

let sp = Const.char ' '

let write_user enc user =
  let fmt = string **! sp ** string **! sp ** ... yield in
  let fmt = finalize (make user) in

  eval enc fmt user.name user.email ...

It's much better. And it's like a printf function in OCaml with a little overhead to facilite the serialization of any data with a Faraday backend. And you can do more.

Another example is to use a memcpy implementation instead a memmove implementation (provided by the standard library) and it's why we need to [finalize] an [fmt].

In fact, you can create your blitter and use it inside [Faraday] like:

(* a memcpy blitter for string *)
let my_blitter = Blitter.{ blit = fun src src_off dst dst_off len -> ...
                         ; length = String.length }

let write_string_with_memcpy enc str =
  let fmt = string **? yield in
  let fmt = finalize (make fmt) my_blitter in

  eval enc fmt None str

The [None] argument can be used to specified a [vec] value and specify an offset and a length of what you want to blit (so we repercute theses values in [src_off] and [len] as parameter of your blit function).

Build Requirements

  • Faraday (dev version)
  • OCaml (>= 4.02.0)
  • A MirageOS hackathon

Feedback

It's a Proof of Concept and you can improve the library like you want!

farfadet's People

Contributors

dinosaure avatar drup avatar

Watchers

 avatar  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.