diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-19 08:47:29 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-19 08:47:29 +0100 |
| commit | a9628fda0fc027c54b5ed5313a0c41731d79e41f (patch) | |
| tree | 9240bc709d0f93336b20fb3d6b3b6cb2e8a35423 /lib/configuration/read_conf.ml | |
| parent | 36e895119c80d52e0d3a3695a3cf3d93e41bcd3e (diff) | |
Create all the datatypes in a single time.
Use aliases for the type name for clarification
Diffstat (limited to 'lib/configuration/read_conf.ml')
| -rw-r--r-- | lib/configuration/read_conf.ml | 116 |
1 files changed, 58 insertions, 58 deletions
diff --git a/lib/configuration/read_conf.ml b/lib/configuration/read_conf.ml index 4150295..bcd8f8a 100644 --- a/lib/configuration/read_conf.ml +++ b/lib/configuration/read_conf.ml @@ -4,20 +4,6 @@ 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. @@ -40,10 +26,30 @@ module Make (S : Decoders.Decode.S) = struct 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 = + | None -> + S.fail "Expected an identifier with only the accepted characters" + + type env = { ty : S.value Type.Id.t; settings : Ast.setting } + (** The decoders below need the setting being built (to resolve the reference + targets) and a type id (to attribute broken links to this parsing pass in + the error messages). *) + + (** Build the reference target for a field: a lazy thunk closing over the + setting. The thunk is only forced by [Ast.check], once every content has + been registered — this is what allows forward and cyclic references + between contents. The raw value is kept to report broken links. *) + let link (env : env) (value : S.value) (name : Model.Identifier.t) : + Model.Reference.t = + Model.Reference.v (fun () -> + match Ast.get_content env.settings name with + | Some content -> content + | None -> + if Ast.has_list env.settings name then + raise_notrace (Ast.NotImplemented (env.ty, value, name)) + else raise_notrace (Ast.BrokenLink (env.ty, value, name))) + + let field_type (env : env) : Model.field_ex S.decoder = + let text_type : Model.field_ex S.decoder = S.and_then (fun value -> match String.lowercase_ascii value with @@ -51,32 +57,27 @@ module Make (S : Decoders.Decode.S) = struct | "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 = + and content_ref : Model.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 - + let value, name = content_t in + let t = link env value name in match multivaluated with - | true -> - S.succeed - (Model.Field.T (MultiReference { t = reference; constraints })) - | false -> - S.succeed - (Model.Field.T (MonoReference { t = reference; constraints })) + | true -> S.succeed (Model.Field.T (MultiReference { t; constraints })) + | false -> S.succeed (Model.Field.T (MonoReference { t; constraints })) in - S.one_of [ ("text", text_type); ("content", content) ] + S.one_of [ ("text", text_type); ("content", content_ref) ] 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 field (env : env) : Model.field S.decoder = let* name = S.field "name" identifier and* label = S.field "label" textI18n - and* type' = S.field "type" field_type + and* type' = S.field "type" (field_type env) and* required = S.field_opt_or ~default:false "required" S.bool and* placeholder = S.field_opt "placeholder" textI18n and* validation = @@ -87,43 +88,40 @@ module Make (S : Decoders.Decode.S) = struct (Model.Field.create type' label ~required ?placeholder ~validation ?criteria ?icon name) - let section : (S.value * Model.Identifier.t) Model.Section.t S.decoder = + let section (env : env) : Model.section 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 + and* fields = S.field "field" (S.list (field env)) 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 content (env : env) : Model.content S.decoder = + S.fix (fun self -> + let* id = S.field "id" identifier + and* sections = + S.field_opt_or ~default:[] "section" (S.list (section env)) + and* subcontents = + S.field_opt_or ~default:[] "subcontent" (S.list self) + in + S.succeed (Model.Content.v id sections subcontents)) 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 *) + (* Register a type id to attribute broken links to this parsing pass *) 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 + (* Decode the lists first, then the contents: the reference targets are + lazy thunks closing over [settings], forced only by [Ast.check] once + every content has been registered. *) + let* lists = S.field_opt_or ~default:[] "list" (S.list list') in + let settings = + List.fold_left lists ~init:(Ast.init ()) ~f:Ast.register_list + in + let env = { ty; settings } in + let* contents = S.field "content" (S.list (content env)) in + let () = List.iter contents ~f:(Ast.register_content settings) in (* Ensure the settings are valid in the configuration *) match Ast.check settings with @@ -133,7 +131,8 @@ module Make (S : Decoders.Decode.S) = struct | None -> raise e | Some Type.Equal -> let message = - "Undefined target \"" ^ Model.Identifier.to_string name + "Undefined target \"" + ^ Model.Identifier.to_string name ^ "\". Expected existing list or content" in S.fail_with (Decoders.Error.make ~context message)) @@ -142,7 +141,8 @@ module Make (S : Decoders.Decode.S) = struct | None -> raise e | Some Type.Equal -> let message = - "Unsupported target \"" ^ Model.Identifier.to_string name + "Unsupported target \"" + ^ Model.Identifier.to_string name ^ "\". Lists as target are not implemented yet" in S.fail_with (Decoders.Error.make ~context message)) |
