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)