open StdLabels let name = __FILE__ let description = "Resolve a path from a content and display the target" let speclist () = [] type t = (string list, 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_list ~pp_sep:Format.pp_print_newline Format.pp_print_string) let ( let+ ) opt cont = Result.map cont opt let string_of_target : Model.target -> string = function | Model.Reference.Content ref -> "content " ^ Model.Identifier.to_string (Model.Content.id (Model.Reference.get ref)) | Model.Reference.List list -> "list " ^ Model.Identifier.to_string list.Model.DataList.id let describe_field : Model.field -> string = fun field -> let name = Model.Identifier.to_string (Model.Field.name field) in let (Model.Field.T ty) = Model.Field.type_ field in match ty with | Model.Typ.Text -> "field " ^ name ^ " (text)" | Model.Typ.Integer -> "field " ^ name ^ " (integer)" | Model.Typ.MonoReference reference -> "field " ^ name ^ " (reference -> " ^ string_of_target reference.Model.Typ.t ^ ")" | Model.Typ.MultiReference reference -> "field " ^ name ^ " (multireference -> " ^ string_of_target reference.Model.Typ.t ^ ")" let exec : string list -> t = fun args -> let stdin = In_channel.input_all stdin in let+ configuration = Configuration.Read_conf.read_string_conf stdin in (* Anonymous arguments arrive reversed: [content; path] *) match List.rev args with | [] -> [ "missing argument: " ] | content_name :: path -> let path = String.concat ~sep:"." path |> String.split_on_char ~sep:'.' |> List.map ~f:Model.Identifier.of_string in let id = Model.Identifier.of_string content_name in let result = match Configuration.Ast.get_content configuration id with | None -> "unknown content " ^ content_name | Some content -> ( match Model.Path.resolve_from_content content path with | None -> "unresolved path " ^ String.concat ~sep:"." (List.map ~f:Model.Identifier.to_string path) | Some field -> describe_field field) in [ result ]