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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
open StdLabels
module Make (S : Decoders.Decode.S) = struct
let ( let* ) = S.( let* )
let ( and* ) = S.( and* )
(** [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"
type env = {
ty : S.value Type.Id.t;
settings : Ast.setting;
allow_content : bool;
(** Whether a reference may target a content. References inside a list
can only target other lists. *)
}
(** 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. Lists are decoded before the
contents and registered as soon as they are decoded, so a declared list
is matched eagerly; a content target is a lazy thunk closing over the
setting, 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.target S.decoder =
match Ast.get_list env.settings name with
| Some list -> S.succeed (Model.Reference.List list)
| None -> (
if env.allow_content then
S.succeed
(Model.Reference.Content
(Model.Reference.v (fun () ->
match Ast.get_content env.settings name with
| Some content -> content
| None ->
raise_notrace (Ast.BrokenLink (env.ty, value, name)))))
else
S.fail_with
(Decoders.Error.make ~context:value
"References in a list can only target a list declared before it"))
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
| "text" -> S.succeed (Model.Field.T Text)
| "integer" -> S.succeed (Model.Field.T Integer)
| other -> S.fail other)
S.string
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 value, name = content_t in
let* t = link env value name in
match multivaluated with
| 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_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 (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 env)
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 (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 env)) in
let icon = None in
S.succeed Model.Section.{ label; criteria; fields; icon }
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 (env : env) : Model.list S.decoder =
(* The list has its own fields, declared directly under "field" — unlike
contents, there is no section grouping them. Only the id and the fields
are part of the model: where the entries come from is the concern of the
data provider, not of the configuration model. *)
let* id = S.field "id" identifier
and* fields = S.field_opt_or ~default:[] "field" (S.list (field env)) in
S.succeed Model.DataList.{ id; fields }
let t : Ast.setting S.decoder =
(* 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
(* Decode the lists first, registering each one as soon as it is decoded so
that a list can reference the lists declared before it. References in a
list can only target lists ([allow_content = false]). *)
let list_env = { ty; settings; allow_content = false } in
let* _lists =
S.field_opt_or ~default:[] "list"
(S.list
(S.and_then
(fun list ->
let _ = Ast.register_list settings list in
S.succeed list)
(list list_env)))
in
(* Then the contents: the reference targets are lazy thunks closing over
[settings], forced only by [Ast.check] once every content has been
registered. *)
let env = { ty; settings; allow_content = true } 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
| 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 (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
|