From daa4be3dd8e690ca1e39ec827f2872762ac3adf1 Mon Sep 17 00:00:00 2001 From: Jeremie Dimino Date: Mon, 25 Jun 2018 08:11:54 +0100 Subject: [PATCH] Add Stanza.file_kind Signed-off-by: Jeremie Dimino --- src/action.ml | 16 ++++++++++++---- src/stanza.ml | 9 +++++++++ src/stanza.mli | 7 +++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/action.ml b/src/action.ml index 24870fed..ce4f5cba 100644 --- a/src/action.ml +++ b/src/action.ml @@ -89,14 +89,22 @@ struct ; "diff", (path >>= fun file1 -> path >>= fun file2 -> - Syntax.get_exn Stanza.syntax >>| fun ver -> - let mode = if ver < (1, 0) then Diff_mode.Text_jbuild else Text in + Stanza.file_kind () >>| fun kind -> + let mode = + match kind with + | Jbuild -> Diff_mode.Text_jbuild + | Dune -> Text + in Diff { optional = false; file1; file2; mode }) ; "diff?", (path >>= fun file1 -> path >>= fun file2 -> - Syntax.get_exn Stanza.syntax >>| fun ver -> - let mode = if ver < (1, 0) then Diff_mode.Text_jbuild else Text in + Stanza.file_kind () >>| fun kind -> + let mode = + match kind with + | Jbuild -> Diff_mode.Text_jbuild + | Dune -> Text + in Diff { optional = true; file1; file2; mode }) ; "cmp", (Syntax.since Stanza.syntax (1, 0) >>= fun () -> diff --git a/src/stanza.ml b/src/stanza.ml index a82b9f9e..478870f5 100644 --- a/src/stanza.ml +++ b/src/stanza.ml @@ -11,3 +11,12 @@ let syntax = [ (0, 0) (* Jbuild syntax *) ; (1, 0) ] + +module File_kind = struct + type t = Jbuild | Dune +end + +let file_kind () = + let open Sexp.Of_sexp in + Syntax.get_exn syntax >>| fun ver -> + if ver < (1, 0) then File_kind.Jbuild else Dune diff --git a/src/stanza.mli b/src/stanza.mli index d7d1800d..76e15002 100644 --- a/src/stanza.mli +++ b/src/stanza.mli @@ -16,3 +16,10 @@ end the Jbuild language while versions from [(1, 0)] correspond to the Dune one. *) val syntax : Syntax.t + +module File_kind : sig + type t = Jbuild | Dune +end + +(** Whether we are parsing a [jbuild] or [dune] file. *) +val file_kind : unit -> (File_kind.t, _) Sexp.Of_sexp.parser