Movatterモバイル変換


[0]ホーム

URL:


packagefarfadet

  1. Overview
  2. Docs

You can search for identifiers within the package.

in-package search v0.2.0

A printf-like for [Faraday](https://github.com/inhabitedtype/faraday) library

Install

Dune Dependency

Authors

Maintainers

Sources

farfadet-0.3.tbz
sha256=52649a6f1ff3440d4e87925c03468d29e2cdb63553dfaf15b1a0f30bae758a5a
md5=033abca3a94e893fce2713ec43866029

Description

To use this library, you can pin it withfaraday:

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

Quick look

Firstly, you need to understand whatFaraday is. Then, you can serializesomething like this data:

type t =   [ `Bool of bool  | `Float of float  | `Null  | `String of string  | `A of t list  | `O of (string * t) list ]

You could write an encoder function with Faraday, like this:

let rec write_json enc = function  | `Bool true -> Faraday.write_string enc "true"  | `Bool false -> Faraday.write_string enc "false"  ...  | `A lst ->    Faraday.write_char enc '[';        let rec aux = function      | [] -> ()      | [ x ] -> write_json x      | x :: r -> write_json x; Faraday.write_char ','; aux r    in         aux lst;    Faraday.write_char enc ']'

And it's boring... Yes.

So,Farfadet can help you to write a serializer in a type-safe way.This is an example:

let comma =  let open Farfadet in   (fun e () -> string e ","), ()  let rec value : t Farfadet.t = fun e x ->  let open Farfadet in     let arr = list ~sep:comma value in    match x with  | `Bool true -> string e "true"  | `Bool false -> string e "false"  ...  | `A lst -> eval e [ char $ '['; !!arr; char $ ']'] lst

Much better. And it's like aprintf function in OCaml with a littleoverhead to facilitate the serialization of any data with aFaraday backend. Andyou can do more.

Another example is to use amemcpy implementation instead amemmoveimplementation (provided by the standard library).

In fact, you can create yourblitter and use it insideFaraday like:

let memcpy s soff d doff len =  for i = 0 to len - 1  do Bigarray.Array1.set dst (doff + i) (String.get s (soff + i)) donelet string' : string Farfadet.t = fun e -> eval e [ !!(whole @@ blitter String.length memcpy) ]

You can see the documentation to understand this snippet. A good example isprovided in thetest to serialize aEzjsonm.tvalue.

Build Requirements

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

Feedback

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

Published:05 Jul 2018

README

Farfadet, a printf-like forFaraday library

To use this library, you can pin it withfaraday:

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

Quick look

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

type t =   [ `Bool of bool  | `Float of float  | `Null  | `String of string  | `A of t list  | `O of (string * t) list ]

You could write an encoder function with Faraday, like this:

let rec write_json enc = function  | `Bool true -> Faraday.write_string enc "true"  | `Bool false -> Faraday.write_string enc "false"  ...  | `A lst ->    Faraday.write_char enc '[';        let rec aux = function      | [] -> ()      | [ x ] -> write_json x      | x :: r -> write_json x; Faraday.write_char ','; aux r    in         aux lst;    Faraday.write_char enc ']'

And it's boring... Yes.

So,Farfadet can help you to write a serializer in a type-safe way. This is an example:

let comma =  let open Farfadet in   (fun e () -> string e ","), ()  let rec value : t Farfadet.t = fun e x ->  let open Farfadet in     let arr = list ~sep:comma value in    match x with  | `Bool true -> string e "true"  | `Bool false -> string e "false"  ...  | `A lst -> eval e [ char $ '['; !!arr; char $ ']'] lst

Much better. And it's like aprintf function in OCaml with a little overhead to facilitate the serialization of any data with aFaraday backend. And you can do more.

Another example is to use amemcpy implementation instead amemmove implementation (provided by the standard library).

In fact, you can create yourblitter and use it insideFaraday like:

let memcpy s soff d doff len =  for i = 0 to len - 1  do Bigarray.Array1.set dst (doff + i) (String.get s (soff + i)) donelet string' : string Farfadet.t = fun e -> eval e [ !!(whole @@ blitter String.length memcpy) ]

You can see the documentation to understand this snippet. A good example is provided in thetest to serialize aEzjsonm.t value.

Build Requirements

  • Faraday (dev version)

  • OCaml (>= 4.03.0)

  • A MirageOS hackathon

Feedback

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

Dependencies (5)

  1. faraday>= "0.6.0"
  2. topkgbuild
  3. ocamlfindbuild
  4. ocamlbuildbuild
  5. ocaml>= "4.03.0"

Dev Dependencies (2)

  1. ezjsonmwith-test
  2. alcotestwith-test

Used by

None

Conflicts

None


[8]ページ先頭

©2009-2025 Movatter.jp