From 6022b68c5f898dd0d1a8b515af3b1bf6a937ec3f Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 25 Nov 2019 20:51:32 -0600 Subject: [PATCH] feat: remove easy-format --- lib/pretty.ml | 66 +++++++++++++++++++++++--------------------------- lib/pretty.mli | 2 +- lib/write2.ml | 4 +-- lib/write2.mli | 7 ------ yojson.opam | 1 - 5 files changed, 32 insertions(+), 48 deletions(-) diff --git a/lib/pretty.ml b/lib/pretty.ml index 5d027f1f..fef7c715 100644 --- a/lib/pretty.ml +++ b/lib/pretty.ml @@ -1,69 +1,63 @@ -open Printf -let array = Easy_format.list -let record = Easy_format.list -let tuple = { Easy_format.list with - space_after_opening = false; - space_before_closing = false; - align_closing = false } -let variant = { Easy_format.list with - space_before_closing = false; } +let pp_list sep ppx out l = + let pp_sep out () = Format.fprintf out "%s@ " sep in + Format.pp_print_list ~pp_sep ppx out l -let rec format std (x : t) = +let rec format std (out:Format.formatter) (x : t) : unit = match x with - `Null -> Easy_format.Atom ("null", Easy_format.atom) - | `Bool x -> Easy_format.Atom ((if x then "true" else "false"), Easy_format.atom) - | `Int x -> Easy_format.Atom (json_string_of_int x, Easy_format.atom) + | `Null -> Format.pp_print_string out "null" + | `Bool x -> Format.pp_print_bool out x + | `Int x -> Format.pp_print_string out (json_string_of_int x) | `Float x -> let s = if std then std_json_string_of_float x else json_string_of_float x in - Easy_format.Atom (s, Easy_format.atom) - | `String s -> Easy_format.Atom (json_string_of_string s, Easy_format.atom) + Format.pp_print_string out s + | `String s -> Format.pp_print_string out (json_string_of_string s) | `Intlit s | `Floatlit s - | `Stringlit s -> Easy_format.Atom (s, Easy_format.atom) - | `List [] -> Easy_format.Atom ("[]", Easy_format.atom) - | `List l -> Easy_format.List (("[", ",", "]", array), List.map (format std) l) - | `Assoc [] -> Easy_format.Atom ("{}", Easy_format.atom) - | `Assoc l -> Easy_format.List (("{", ",", "}", record), List.map (format_field std) l) + | `Stringlit s -> Format.pp_print_string out s + | `List [] -> Format.pp_print_string out "[]" + | `List l -> Format.fprintf out "[@;<1 0>@[%a@]@;<1 -2>]" (pp_list "," (format std)) l + | `Assoc [] -> Format.pp_print_string out "{}" + | `Assoc l -> + Format.fprintf out "{@;<1 0>%a@;<1 -2>}" (pp_list "," (format_field std)) l | `Tuple l -> if std then - format std (`List l) + format std out (`List l) else if l = [] then - Easy_format.Atom ("()", Easy_format.atom) + Format.pp_print_string out "()" else - Easy_format.List (("(", ",", ")", tuple), List.map (format std) l) + Format.fprintf out "(@,%a@;<0 -2>)" (pp_list "," (format std)) l | `Variant (s, None) -> if std then - format std (`String s) + format std out (`String s) else - Easy_format.Atom ("<" ^ json_string_of_string s ^ ">", Easy_format.atom) + Format.fprintf out "<%s>" (json_string_of_string s) | `Variant (s, Some x) -> if std then - format std (`List [ `String s; x ]) + format std out (`List [ `String s; x ]) else - let op = "<" ^ json_string_of_string s ^ ":" in - Easy_format.List ((op, "", ">", variant), [format std x]) + let op = json_string_of_string s in + Format.fprintf out "<@[%s: %a@]>" op (format std) x -and format_field std (name, x) = - let s = sprintf "%s:" (json_string_of_string name) in - Easy_format.Label ((Easy_format.Atom (s, Easy_format.atom), Easy_format.label), format std x) +and format_field std out (name, x) = + Format.fprintf out "@[%s: %a@]" (json_string_of_string name) (format std) x - -let format ?(std = false) x = +let pp ?(std = false) out x = if std && not (is_object_or_array x) then json_error "Root is not an object or array as requested by the JSON standard" else - format std (x :> t) + Format.fprintf out "@[%a@]" (format std) (x :> t) let to_string ?std x = - Easy_format.Pretty.to_string (format ?std x) + Format.asprintf "%a" (pp ?std) x let to_channel ?std oc x = - Easy_format.Pretty.to_channel oc (format ?std x) + let fmt = Format.formatter_of_out_channel oc in + Format.fprintf fmt "%a@?" (pp ?std) x diff --git a/lib/pretty.mli b/lib/pretty.mli index 3a06ca9d..2d1ad6b1 100644 --- a/lib/pretty.mli +++ b/lib/pretty.mli @@ -1,3 +1,3 @@ -val format : ?std:bool -> json -> Easy_format.t +val pp : ?std:bool -> Format.formatter -> json -> unit val to_string : ?std:bool -> json -> string val to_channel : ?std:bool -> out_channel -> json -> unit diff --git a/lib/write2.ml b/lib/write2.ml index c05c1b76..3fda5644 100644 --- a/lib/write2.ml +++ b/lib/write2.ml @@ -1,8 +1,6 @@ -let pretty_format ?std (x : t) = - Pretty.format ?std (x :> json_max) let pretty_print ?std out (x : t) = - Easy_format.Pretty.to_formatter out (pretty_format ?std x) + Pretty.pp ?std out (x :> json_max) let pretty_to_string ?std (x : t) = Pretty.to_string ?std (x :> json_max) diff --git a/lib/write2.mli b/lib/write2.mli index a9562cfb..6efdc448 100644 --- a/lib/write2.mli +++ b/lib/write2.mli @@ -1,12 +1,5 @@ (** {2 JSON pretty-printing} *) -val pretty_format : ?std:bool -> t -> Easy_format.t - (** Convert into a pretty-printable tree. - See [to_string] for the role of the optional [std] argument. - - @see Easy-format - *) - val pretty_print : ?std:bool -> Format.formatter -> t -> unit (** Pretty-print into a {!Format.formatter}. See [to_string] for the role of the optional [std] argument. diff --git a/yojson.opam b/yojson.opam index 5bd9ecec..46b5ceaa 100644 --- a/yojson.opam +++ b/yojson.opam @@ -14,7 +14,6 @@ depends: [ "ocaml" {>= "4.02.3"} "dune" "cppo" {build} - "easy-format" "biniou" {>= "1.2.0"} "alcotest" {with-test & >= "0.8.5"} ]