mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
aesthetic in Bencode
This commit is contained in:
parent
b70c94377e
commit
4dd3c63ba5
3 changed files with 18 additions and 0 deletions
|
|
@ -6,5 +6,6 @@
|
|||
#require "threads";;
|
||||
#load "thread_containers.cma";;
|
||||
open Gen.Infix;;
|
||||
#install_printer Bencode.pretty;;
|
||||
(* vim:syntax=ocaml:
|
||||
*)
|
||||
|
|
|
|||
14
bencode.ml
14
bencode.ml
|
|
@ -83,6 +83,20 @@ let fmt formatter t =
|
|||
to_buf b t;
|
||||
Format.pp_print_string formatter (Buffer.contents b)
|
||||
|
||||
let rec pretty fmt t = match t with
|
||||
| I i -> Format.fprintf fmt "%d" i
|
||||
| S s -> Format.fprintf fmt "@[<h>\"%s\"@]" s
|
||||
| L l ->
|
||||
Format.fprintf fmt "@[<hov 2>[@,";
|
||||
List.iter (fun t' -> Format.fprintf fmt "%a@ " pretty t') l;
|
||||
Format.fprintf fmt "]@]";
|
||||
| D d ->
|
||||
Format.fprintf fmt "@[<hov 2>{@,";
|
||||
SMap.iter
|
||||
(fun k t' -> Format.fprintf fmt "%a -> %a@ " pretty (S k) pretty t')
|
||||
d;
|
||||
Format.fprintf fmt "}@]";
|
||||
|
||||
(** {2 Deserialization (decoding)} *)
|
||||
|
||||
(** Deserialization is based on the {! decoder} type. Parsing can be
|
||||
|
|
|
|||
|
|
@ -49,6 +49,9 @@ val to_string : t -> string
|
|||
val to_chan : out_channel -> t -> unit
|
||||
val fmt : Format.formatter -> t -> unit
|
||||
|
||||
val pretty : Format.formatter -> t -> unit
|
||||
(** Print the tree itself, not its encoding *)
|
||||
|
||||
(** {2 Deserialization (decoding)} *)
|
||||
|
||||
(** Deserialization is based on the {! decoder} type. Parsing can be
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue