add CCFormat.Dump for easy debugging (see #82)

This commit is contained in:
Simon Cruanes 2016-11-03 15:36:25 +01:00
parent 4ff174ce18
commit 0d9d17d5db
2 changed files with 57 additions and 0 deletions

View file

@ -301,3 +301,31 @@ let ksprintf ~f fmt =
Format.kfprintf
(fun _ -> Format.pp_print_flush out (); f (Buffer.contents buf))
out fmt
module Dump = struct
type 'a t = 'a printer
let unit = unit
let int = int
let string = string_quoted
let bool = bool
let float = float
let char = char
let int32 = int32
let int64 = int64
let nativeint = nativeint
let list pp = hovbox (list ~start:"[" ~stop:"]" ~sep:";" pp)
let array pp = hovbox (array ~start:"[|" ~stop:"|]" ~sep:";" pp)
let option pp out x = match x with
| None -> Format.pp_print_string out "None"
| Some x -> Format.fprintf out "Some %a" pp x
let pair p1 p2 = pair p1 p2
let triple p1 p2 p3 = triple p1 p2 p3
let quad p1 p2 p3 p4 = quad p1 p2 p3 p4
end
(*$= & ~printer:(fun s->s)
"[1;2;3]" (to_string Dump.(list int) [1;2;3])
"Some 1" (to_string Dump.(option int) (Some 1))
"[None;Some \"a b\"]" (to_string Dump.(list (option string)) [None; Some "a b"])
*)

View file

@ -172,3 +172,32 @@ val ksprintf :
val to_file : string -> ('a, t, unit, unit) format4 -> 'a
(** Print to the given file *)
(** {2 Dump}
Print structures as OCaml values, so that they can be parsed back
by OCaml (typically, in the toplevel, for debugging)
@since NEXT_RELEASE *)
module Dump : sig
type 'a t = 'a printer
val unit : unit t
val int : int t
val string : string t
val bool : bool t
val float : float t
val char : char t (** @since 0.14 *)
val int32 : int32 t (** @since 0.14 *)
val int64 : int64 t (** @since 0.14 *)
val nativeint : nativeint t (** @since 0.14 *)
val list : 'a t -> 'a list t
val array : 'a t -> 'a array t
val option : 'a t -> 'a option t
val pair : 'a t -> 'b t -> ('a * 'b) t
val triple : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t
val quad :
'a t -> 'b t -> 'c t -> 'd t ->
('a * 'b * 'c * 'd) t
end