Pure OCaml Yaml 1.2 reader and writer using Bytesrw
1(*---------------------------------------------------------------------------
2 Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
3 SPDX-License-Identifier: ISC
4 ---------------------------------------------------------------------------*)
5
6(** YAML sequence (array) values with metadata *)
7
8type 'a t = {
9 anchor : string option;
10 tag : string option;
11 implicit : bool;
12 style : Layout_style.t;
13 members : 'a list;
14}
15
16let make
17 ?(anchor : string option)
18 ?(tag : string option)
19 ?(implicit = true)
20 ?(style = `Any)
21 members =
22 { anchor; tag; implicit; style; members }
23
24let members t = t.members
25let anchor t = t.anchor
26let tag t = t.tag
27let implicit t = t.implicit
28let style t = t.style
29
30let with_anchor anchor t = { t with anchor = Some anchor }
31let with_tag tag t = { t with tag = Some tag }
32let with_style style t = { t with style }
33
34let map f t = { t with members = List.map f t.members }
35
36let length t = List.length t.members
37
38let is_empty t = t.members = []
39
40let nth t n = List.nth t.members n
41
42let nth_opt t n = List.nth_opt t.members n
43
44let iter f t = List.iter f t.members
45
46let fold f init t = List.fold_left f init t.members
47
48let pp pp_elem fmt t =
49 Format.fprintf fmt "@[<hv 2>sequence(@,";
50 (match t.anchor with
51 | Some a -> Format.fprintf fmt "anchor=%s,@ " a
52 | None -> ());
53 (match t.tag with
54 | Some tag -> Format.fprintf fmt "tag=%s,@ " tag
55 | None -> ());
56 Format.fprintf fmt "style=%a,@ " Layout_style.pp t.style;
57 Format.fprintf fmt "members=[@,%a@]@,)"
58 (Format.pp_print_list ~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ") pp_elem)
59 t.members
60
61let equal eq a b =
62 Option.equal String.equal a.anchor b.anchor &&
63 Option.equal String.equal a.tag b.tag &&
64 a.implicit = b.implicit &&
65 Layout_style.equal a.style b.style &&
66 List.equal eq a.members b.members
67
68let compare cmp a b =
69 let c = Option.compare String.compare a.anchor b.anchor in
70 if c <> 0 then c else
71 let c = Option.compare String.compare a.tag b.tag in
72 if c <> 0 then c else
73 let c = Bool.compare a.implicit b.implicit in
74 if c <> 0 then c else
75 let c = Layout_style.compare a.style b.style in
76 if c <> 0 then c else
77 List.compare cmp a.members b.members