summaryrefslogtreecommitdiff
path: root/test/parent.ml
blob: a13afdaf2f0202d3894d1856706ae38e55fb644e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
let name = __FILE__

let description =
  "Return the parent of the given content. The configuration is read on the \
   standard input"

let parent = ref Model.Content.parent

let speclist =
  [
    ( "--root",
      Arg.Unit
        (fun () -> parent := fun content -> Some (Model.Content.root content)),
      "Return the root element instead of the parent" );
  ]

type t = (string option option, Configuration.Read_conf.error) result

let pp : Format.formatter -> t -> unit =
  Format.pp_print_result ~error:Configuration.Read_conf.pp_error
    ~ok:
      (Format.pp_print_option ~none:(fun f () ->
           Format.pp_print_string f "[Content not found]")
      @@ Format.pp_print_option ~none:(fun f () ->
          Format.pp_print_string f "[No parent]")
      @@ fun f -> Format.fprintf f "\"%s\"")

let exec : string list -> t = function
  | [ subcontent_id ] ->
      let parent_id =
        let stdin = In_channel.input_all stdin in
        let ( let+ ) opt cont = Result.map cont opt in
        let+ configuration = Configuration.Read_conf.read_string_conf stdin in
        let ( let+ ) opt cont = Option.map cont opt in
        let+ subcontent =
          Configuration.Ast.get_content configuration subcontent_id
        in
        let+ v = !parent subcontent in
        Model.Content.id v
      in
      parent_id
  | _ -> failwith "Invalid arguments"