blob: 607031e46e3ffe5e896788628f6329cc8d6da12e (
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
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
|
open StdLabels
module Splay = Splay.Make (Model.Identifier)
let escaped = String.escaped
type list' = { id : string; source : string }
type setting = { mutable contents : Model.content Splay.t; lists : list' list }
let init : unit -> setting = fun () -> { contents = Splay.empty (); lists = [] }
(** Get the content from the setting. *)
let get_content : setting -> Model.Identifier.t -> Model.content option =
fun settings name -> Splay.find name settings.contents
(** Check whether a list with the given name is declared in the setting *)
let has_list : setting -> Model.Identifier.t -> bool =
fun setting name ->
List.exists setting.lists ~f:(fun { id; _ } ->
String.equal id (Model.Identifier.to_string name))
exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
exception NotImplemented : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
(** Register a content and all the associated subcontent in the top level,
giving the possibility to fetch any content or subcontent by its name
without traversing the whole tree each time. *)
let register_content : setting -> Model.content -> unit =
fun setting content ->
let rec traverse content =
let () =
setting.contents <-
Splay.add (Model.Content.id content) content setting.contents
in
List.iter ~f:traverse (Model.Content.subcontents content)
in
traverse content
let register_list : setting -> list' -> setting =
fun t l -> { t with lists = l :: t.lists }
(** Force the evaluation of the field target and ensure it points to an existing
content in the application.
Return the content targeted *)
let get_reference_target : Model.field -> Model.content option =
fun field ->
let (T ty) = Model.Field.type_ field in
match ty with
| Text -> None
| Integer -> None
| MonoReference r | MultiReference r -> (
try Some (Model.Reference.get r.Model.Typ.t)
with BrokenLink _ as e -> raise_notrace e)
(** Extract the other contents pointed by a given content, and the field used
for it. *)
let targeted_contents : Model.content -> (Model.field * Model.content) Seq.t =
fun t ->
Seq.filter_map
(fun field ->
match get_reference_target field with
| None -> None
| Some ref -> Some (field, ref))
(Model.Content.fields t)
let tree : ?required:bool -> setting -> Graph_tree.t =
fun ?(required = false) setting ->
let tree = Graph_tree.create () in
let () =
Splay.fold
(fun () (_, src) ->
let targets = targeted_contents src in
let root = Model.Content.root src in
Graph_tree.add_content tree root;
Seq.iter
(fun (field, dst) ->
(* We build the dependencies tree with the root contents only.
This does not change the logic, and this makes things easer
too *)
let add_edge = (not required) || Model.Field.required field in
match add_edge with
| false -> ()
| true ->
Graph_tree.add_edge tree root field (Model.Content.root dst))
targets)
() setting.contents
in
tree
(** Return all the contents declared in the settings.
The list provide the root contents in the topological order *)
let root_contents : setting -> Model.content list =
fun setting -> Graph_tree.fold (fun a acc -> a :: acc) (tree setting) []
let check : setting -> (unit, exn) result =
fun setting ->
(* Ensure each content referenced exists.
We accept mutually recursive dependancy and need to load all the content
first before checking the settings *)
try
let _ = tree setting in
(* Check cycle dependencies *)
let required_tree = tree ~required:true setting in
Graph_tree.check_cycle required_tree;
Ok ()
with e -> Error e
|