Module Private.Codec

Topkg interprocess communication codec.

Codecs for communication between the topkg tool and topkg description files.

Decode errors

type error =
| Corrupted of string * string
| Version of int * int

The type for decode errors.

  • Corrupted (kind, data), an error occured while decoding data for kind.
  • Version (exp, fnd), a versioned decoder expected version exp but found fnd
val pp_error : Format.formatter -> error -> unit

pp_error ppf e prints an unspecified representation of e on ppf.

exception Error of error

Raised on decode errors.

Codecs

type 'a t

The type for codec for OCaml values of type 'a.

val v : kind:string -> enc:('a -> string) -> dec:(string -> 'a) -> 'a t

v kind enc dec is a codec for value identified as kind using enc to encode and dec to decode.

val kind : 'a t -> string

kind c is c's kind.

val enc : 'a t -> 'a -> string

enc c is c's encoder.

val dec : 'a t -> string -> 'a

dec c is c's decoder. The decoder

  • raises Error

    in case of decode error

val dec_result : 'a t -> string -> 'a result

dec c is like dec but doesn't raise. The exception is turned into an error message using pp_error.

val with_kind : string -> 'a t -> 'a t

with_kind k c is c with kind k.

val write : fpath -> 'a t -> 'a -> unit result

write f c v encodes value v with c to f.

val read : fpath -> 'a t -> 'a result

read f c reads a value with c from f.

Base type codecs

val unit : unit t

unit codecs a ().

val const : 'a -> 'a t

const v codecs the constant v.

val bool : bool t

bool codecs booleans.

val int : int t

int codecs integers.

val string : string t

string codecs strings.

val option : 'a t -> 'a option t

option el codecs el options.

val result : ok:'a t -> error:'b t -> ('a'b) r t

result ~ok ~error codecs ok, error results.

val list : 'a t -> 'a list t

list el codecs el lists.

val pair : 'a t -> 'b t -> ('a * 'b) t

pair c0 c1 codecs c0, c1 pairs.

val t3 : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t

t3 is like pair but for triples.

val t4 : 'a t -> 'b t -> 'c t -> 'd t -> ('a * 'b * 'c * 'd) t

t4 is like pair but for quadruples.

val t5 : 'a t -> 'b t -> 'c t -> 'd t -> 'e t -> ('a * 'b * 'c * 'd * 'e) t

t5 is like pair but for qintuples.

val alt : kind:string -> ('a -> int) -> 'a t array -> 'a t

alt tag cs codecs values by tagging them with tag and using the corresponding codec in cs.

  • raises Invalid_argument

    if Array.length cs > 256.

val version : int -> 'a t -> 'a t

version num c versions codec c with number num. On decode a version number mismatch raises an error see error.

val view : ?kind:string -> (('a -> 'b) * ('b -> 'a)) -> 'b t -> 'a t

view kind t c views t as c for codecing.

Topkg types

val msg : [ `Msg of string ] t

msg codecs error messages.

val result_error_msg : 'a t -> 'a result t

result_error_msg ok codecs ok or error message results.

val fpath : Fpath.t t

fpath codecs files paths.

val cmd : Cmd.t t

cmd codecs command line fragments.