@@ -71,7 +71,13 @@ type non_det = Nd_output | Nd_command
7171
7272let default_non_det = Nd_output
7373
74- type block_kind = OCaml | Cram | Toplevel | Include
74+ type block_type = OCaml | Cram | Toplevel | Include
75+
76+ let pp_block_type ppf = function
77+ | OCaml -> Fmt. string ppf " ocaml"
78+ | Cram -> Fmt. string ppf " cram"
79+ | Toplevel -> Fmt. string ppf " toplevel"
80+ | Include -> Fmt. string ppf " include"
7581
7682type t =
7783 | Dir of string
@@ -84,13 +90,7 @@ type t =
8490 | Version of Relation .t * Ocaml_version .t
8591 | Set of string * string
8692 | Unset of string
87- | Block_kind of block_kind
88-
89- let pp_block_kind ppf = function
90- | OCaml -> Fmt. string ppf " ocaml"
91- | Cram -> Fmt. string ppf " cram"
92- | Toplevel -> Fmt. string ppf " toplevel"
93- | Include -> Fmt. string ppf " include"
93+ | Block_type of block_type
9494
9595let pp ppf = function
9696 | Dir d -> Fmt. pf ppf " dir=%s" d
@@ -106,7 +106,7 @@ let pp ppf = function
106106 Fmt. pf ppf " version%a%a" Relation. pp op Ocaml_version. pp v
107107 | Set (v , x ) -> Fmt. pf ppf " set-%s=%s" v x
108108 | Unset x -> Fmt. pf ppf " unset-%s" x
109- | Block_kind bk -> pp_block_kind ppf bk
109+ | Block_type bt -> Fmt. pf ppf " block-type=%a " pp_block_type bt
110110
111111let is_prefix ~prefix s =
112112 let len_prefix = String. length prefix in
@@ -140,41 +140,65 @@ let requires_value ~label ~value f =
140140
141141let requires_eq_value ~label ~value f =
142142 requires_value ~label ~value (fun op value ->
143- match op with Relation. Eq -> Ok (f value) | _ -> non_eq_op ~label )
143+ match op with Relation. Eq -> f value | _ -> non_eq_op ~label )
144+
145+ let version_of_string s =
146+ match Ocaml_version. of_string s with
147+ | Ok v -> Ok v
148+ | Error (`Msg e ) -> Util.Result. errorf " Invalid version: %s." e
149+
150+ let parse_non_det_value ~label s =
151+ match s with
152+ | "output" -> Ok Nd_output
153+ | "command" -> Ok Nd_command
154+ | s ->
155+ let allowed_values = [ " <none>" ; {| " command" | }; {| " output" | } ] in
156+ invalid_value ~label ~allowed_values s
157+
158+ let parse_block_type_value ~label s =
159+ match s with
160+ | "ocaml" -> Ok OCaml
161+ | "cram" -> Ok Cram
162+ | "toplevel" -> Ok Toplevel
163+ | "include" -> Ok Include
164+ | s ->
165+ let allowed_values =
166+ [ {| " ocaml" | }; {| " cram" | }; {| " toplevel" | }; {| " include" | } ]
167+ in
168+ invalid_value ~label ~allowed_values s
144169
145170let interpret label value =
171+ let open Util.Result.Infix in
146172 match label with
147173 | "skip" -> doesnt_accept_value ~label ~value Skip
148- | "ocaml" -> doesnt_accept_value ~label ~value (Block_kind OCaml )
149- | "cram" -> doesnt_accept_value ~label ~value (Block_kind Cram )
150- | "toplevel" -> doesnt_accept_value ~label ~value (Block_kind Toplevel )
151- | "include" -> doesnt_accept_value ~label ~value (Block_kind Include )
174+ | "ocaml" -> doesnt_accept_value ~label ~value (Block_type OCaml )
175+ | "cram" -> doesnt_accept_value ~label ~value (Block_type Cram )
176+ | "toplevel" -> doesnt_accept_value ~label ~value (Block_type Toplevel )
177+ | "include" -> doesnt_accept_value ~label ~value (Block_type Include )
152178 | v when is_prefix ~prefix: " unset-" v ->
153179 doesnt_accept_value ~label ~value
154180 (Unset (split_prefix ~prefix: " unset-" v))
155181 | "version" ->
156182 requires_value ~label ~value (fun op v ->
157- match Ocaml_version. of_string v with
158- | Ok v -> Ok (Version (op, v))
159- | Error (`Msg e ) ->
160- Util.Result. errorf " Invalid `version` label value: %s." e)
183+ version_of_string v >> = fun v -> Ok (Version (op, v)))
161184 | "non-deterministic" -> (
162185 match value with
163186 | None -> Ok (Non_det None )
164- | Some (Relation. Eq, "output" ) -> Ok (Non_det (Some Nd_output ))
165- | Some (Relation. Eq, "command" ) -> Ok (Non_det (Some Nd_command ))
166- | Some (Relation. Eq, v ) ->
167- let allowed_values = [ " <none>" ; {| " command" | }; {| " output" | } ] in
168- invalid_value ~label ~allowed_values v
187+ | Some (Relation. Eq, s ) ->
188+ parse_non_det_value ~label s >> = fun nd -> Ok (Non_det (Some nd))
169189 | Some _ -> non_eq_op ~label )
170- | "dir" -> requires_eq_value ~label ~value (fun x -> Dir x)
171- | "source-tree" -> requires_eq_value ~label ~value (fun x -> Source_tree x)
172- | "file" -> requires_eq_value ~label ~value (fun x -> File x)
173- | "part" -> requires_eq_value ~label ~value (fun x -> Part x)
174- | "env" -> requires_eq_value ~label ~value (fun x -> Env x)
190+ | "dir" -> requires_eq_value ~label ~value (fun x -> Ok (Dir x))
191+ | "source-tree" ->
192+ requires_eq_value ~label ~value (fun x -> Ok (Source_tree x))
193+ | "file" -> requires_eq_value ~label ~value (fun x -> Ok (File x))
194+ | "part" -> requires_eq_value ~label ~value (fun x -> Ok (Part x))
195+ | "env" -> requires_eq_value ~label ~value (fun x -> Ok (Env x))
196+ | "block-type" ->
197+ requires_eq_value ~label ~value (fun x ->
198+ parse_block_type_value ~label x >> = fun bt -> Ok (Block_type bt))
175199 | l when is_prefix ~prefix: " set-" l ->
176200 requires_eq_value ~label ~value (fun x ->
177- Set (split_prefix ~prefix: " set-" l, x))
201+ Ok ( Set (split_prefix ~prefix: " set-" l, x) ))
178202 | l -> Error (`Msg (Format. sprintf " `%s` is not a valid label." l))
179203
180204let of_string s =
0 commit comments