blob: 4fe8e925c8b99a620bec5a9e93cb76a98e52b200 (
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
|
open StdLabels
type 'a t = {
id : string;
sections : 'a Section.t list;
subcontents : 'a t list;
parent : 'a t Weak.t;
}
let v : string -> 'a Section.t list -> 'a t list -> 'a t =
fun id sections subcontents ->
let parent = Weak.create 1 in
let content = { id; sections; subcontents; parent } in
(* Fill the parent for all existing subcontent declared in the type
There is no need to repeat the same for the subcontents as they all are
already created in the same way.
*)
List.iter content.subcontents ~f:(fun v -> Weak.set v.parent 0 (Some content));
content
let id t = t.id
let sections t = t.sections
let subcontents t = t.subcontents
let parent t = Weak.get t.parent 0
let rec root t = match parent t with None -> t | Some t -> root t
let fields : 'a t -> 'a Field.t Seq.t =
fun t ->
List.to_seq t.sections
|> Seq.flat_map (fun section -> List.to_seq section.Section.fields)
let find_target : 'a t -> string -> 'a option =
fun t name ->
let feature_opt =
Seq.find (fun f -> String.equal (Field.name f) name) (fields t)
in
let ( let* ) = Option.bind in
let* feature : 'a Field.t = feature_opt in
let (T feature_type) = Field.type_ feature in
match feature_type with
| MonoReference t | MultiReference t -> Some t.Typ.t
| Integer | Text -> None
|