Skip to content

Commit 0d9d17d

Browse files
committed
add CCFormat.Dump for easy debugging (see #82)
1 parent 4ff174c commit 0d9d17d

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Diff for: src/core/CCFormat.ml

+28
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,31 @@ let ksprintf ~f fmt =
301301
Format.kfprintf
302302
(fun _ -> Format.pp_print_flush out (); f (Buffer.contents buf))
303303
out fmt
304+
305+
306+
module Dump = struct
307+
type 'a t = 'a printer
308+
let unit = unit
309+
let int = int
310+
let string = string_quoted
311+
let bool = bool
312+
let float = float
313+
let char = char
314+
let int32 = int32
315+
let int64 = int64
316+
let nativeint = nativeint
317+
let list pp = hovbox (list ~start:"[" ~stop:"]" ~sep:";" pp)
318+
let array pp = hovbox (array ~start:"[|" ~stop:"|]" ~sep:";" pp)
319+
let option pp out x = match x with
320+
| None -> Format.pp_print_string out "None"
321+
| Some x -> Format.fprintf out "Some %a" pp x
322+
let pair p1 p2 = pair p1 p2
323+
let triple p1 p2 p3 = triple p1 p2 p3
324+
let quad p1 p2 p3 p4 = quad p1 p2 p3 p4
325+
end
326+
327+
(*$= & ~printer:(fun s->s)
328+
"[1;2;3]" (to_string Dump.(list int) [1;2;3])
329+
"Some 1" (to_string Dump.(option int) (Some 1))
330+
"[None;Some \"a b\"]" (to_string Dump.(list (option string)) [None; Some "a b"])
331+
*)

Diff for: src/core/CCFormat.mli

+29
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,32 @@ val ksprintf :
172172

173173
val to_file : string -> ('a, t, unit, unit) format4 -> 'a
174174
(** Print to the given file *)
175+
176+
(** {2 Dump}
177+
178+
Print structures as OCaml values, so that they can be parsed back
179+
by OCaml (typically, in the toplevel, for debugging)
180+
181+
@since NEXT_RELEASE *)
182+
183+
module Dump : sig
184+
type 'a t = 'a printer
185+
val unit : unit t
186+
val int : int t
187+
val string : string t
188+
val bool : bool t
189+
val float : float t
190+
val char : char t (** @since 0.14 *)
191+
val int32 : int32 t (** @since 0.14 *)
192+
val int64 : int64 t (** @since 0.14 *)
193+
val nativeint : nativeint t (** @since 0.14 *)
194+
195+
val list : 'a t -> 'a list t
196+
val array : 'a t -> 'a array t
197+
val option : 'a t -> 'a option t
198+
val pair : 'a t -> 'b t -> ('a * 'b) t
199+
val triple : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t
200+
val quad :
201+
'a t -> 'b t -> 'c t -> 'd t ->
202+
('a * 'b * 'c * 'd) t
203+
end

0 commit comments

Comments
 (0)