open StdLabels module Make (S : Decoders.Decode.S) = struct let ( let* ) = S.( let* ) let ( and* ) = S.( and* ) (** Recursive type declaring a content and the subcontents *) type declared_content = | C of Model.Identifier.t * (S.value * Model.Identifier.t) Model.Section.t list * declared_content list let rec content_builder : declared_content -> (S.value * Model.Identifier.t) Model.Content.t = fun c -> let (C (content_id, sections, declared_subcontents)) = c in let children = List.map declared_subcontents ~f:content_builder in Model.Content.v content_id sections children (** [keep decoder] transform a decoder and keep the initial value with the decoded value. This helps to build custom error message, if we want to report an error from a different place. *) let keep : 'a S.decoder -> (S.value * 'a) S.decoder = fun decoder value -> S.map (fun v -> (value, v)) decoder value let textI18n : Model.Field.textI18n S.decoder = S.key_value_pairs S.string let dsl : string list Model.Dsl.t S.decoder = let* content = S.string in let dsl = Parser.ExpressionParser.of_string content in Result.map_error Decoders.Error.make dsl |> S.from_result let identifier : Model.Identifier.t S.decoder = let* id = S.string in if String.equal String.empty id then S.fail "Expected a non empty identifier" else match Model.Identifier.of_string_opt id with | Some id -> S.succeed id | None -> S.fail "Expected an identifier with only the accepted characters" let field_type : (S.value * Model.Identifier.t) Model.Field.field_ex S.decoder = let text_type : (S.value * Model.Identifier.t) Model.Field.field_ex S.decoder = S.and_then (fun value -> match String.lowercase_ascii value with | "text" -> S.succeed (Model.Field.T Text) | "integer" -> S.succeed (Model.Field.T Integer) | other -> S.fail other) S.string and content : (S.value * Model.Identifier.t) Model.Field.field_ex S.decoder = let* content_t = keep @@ S.field "content" identifier and* multivaluated = S.field_opt_or ~default:false "multivaluated" S.bool and* constraints = S.field_opt "constraints" dsl in let reference = content_t in match multivaluated with | true -> S.succeed (Model.Field.T (MultiReference { t = reference; constraints })) | false -> S.succeed (Model.Field.T (MonoReference { t = reference; constraints })) in S.one_of [ ("text", text_type); ("content", content) ] let validation : Model.Field.validation S.decoder = let* rule = S.field "rule" dsl and* message = S.field "message" textI18n in S.succeed Model.Field.{ rule; message } let field : (S.value * Model.Identifier.t) Model.Field.t S.decoder = let* name = S.field "name" identifier and* label = S.field "label" textI18n and* type' = S.field "type" field_type and* required = S.field_opt_or ~default:false "required" S.bool and* placeholder = S.field_opt "placeholder" textI18n and* validation = S.field_opt_or ~default:[] "validation" (S.list validation) and* criteria = S.field_opt "criteria" dsl in let icon = None in S.succeed (Model.Field.create type' label ~required ?placeholder ~validation ?criteria ?icon name) let section : (S.value * Model.Identifier.t) Model.Section.t S.decoder = let* label = S.field_opt_or ~default:[] "label" textI18n and* criteria = S.field_opt "criteria" dsl and* fields = S.field "field" (S.list field) in let icon = None in S.succeed Model.Section.{ label; criteria; fields; icon } let content : (S.value * Model.Identifier.t) Model.Content.t S.decoder = (* Build a temporary type without the parent information *) let* c = S.fix (fun content_decoder -> let* id = S.field "id" identifier and* sections = S.field_opt_or ~default:[] "section" (S.list section) and* subcontents = S.field_opt_or ~default:[] "subcontent" (S.list content_decoder) in let content = C (id, sections, subcontents) in S.succeed content) in S.succeed (content_builder c) let list' : Ast.list' S.decoder = let* id = S.field "id" S.string and* source = S.field "source" S.string in S.succeed Ast.{ id; source } let t : Ast.setting S.decoder = (* Register the data type to fetch it later *) let ty : S.value Type.Id.t = Type.Id.make () in let settings = Ast.init () in let* contents = S.field "content" (S.list content) (* Register the lists *) and* lists = S.field_opt_or ~default:[] "list" (S.list list') in let settings = List.fold_left lists ~init:settings ~f:Ast.register_list in let () = List.iter contents ~f:(Ast.register_content ty settings) in (* Ensure the settings are valid in the configuration *) match Ast.check settings with | Ok () -> S.succeed settings | Error (Ast.BrokenLink (ty', context, name) as e) -> ( match Type.Id.provably_equal ty ty' with | None -> raise e | Some Type.Equal -> let message = "Undefined target \"" ^ Model.Identifier.to_string name ^ "\". Expected existing list or content" in S.fail_with (Decoders.Error.make ~context message)) | Error (Ast.NotImplemented (ty', context, name) as e) -> ( match Type.Id.provably_equal ty ty' with | None -> raise e | Some Type.Equal -> let message = "Unsupported target \"" ^ Model.Identifier.to_string name ^ "\". Lists as target are not implemented yet" in S.fail_with (Decoders.Error.make ~context message)) | Error (Graph_tree.Cycle contents) -> let content_name = List.map contents ~f:(fun c -> Model.Identifier.to_string (Model.Content.id c)) in let message = String.concat ~sep:" " ("Dependency circle found between:" :: content_name) in S.fail_with (Decoders.Error.make message) | Error other -> raise other end module ConfFormat = Json5.Decode include Make (ConfFormat) type error = ConfFormat.error let read_conf : string -> (Ast.setting, error) result = fun file -> ConfFormat.decode_file t file let pp_error = ConfFormat.pp_error let read_string_conf : string -> (Ast.setting, error) result = fun file -> ConfFormat.decode_string t file