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 ?(anchor : string option) ?(tag : string option) ?(implicit = true)
17 ?(style = `Any) members =
18 { anchor; tag; implicit; style; members }
19
20let members t = t.members
21let anchor t = t.anchor
22let tag t = t.tag
23let implicit t = t.implicit
24let style t = t.style
25let with_anchor anchor t = { t with anchor = Some anchor }
26let with_tag tag t = { t with tag = Some tag }
27let with_style style t = { t with style }
28let map f t = { t with members = List.map f t.members }
29let length t = List.length t.members
30let is_empty t = t.members = []
31let nth t n = List.nth t.members n
32let nth_opt t n = List.nth_opt t.members n
33let iter f t = List.iter f t.members
34let fold f init t = List.fold_left f init t.members
35
36let pp pp_elem fmt t =
37 Format.fprintf fmt "@[<hv 2>sequence(@,";
38 Option.iter (Format.fprintf fmt "anchor=%s,@ ") t.anchor;
39 Option.iter (Format.fprintf fmt "tag=%s,@ ") t.tag;
40 Format.fprintf fmt "style=%a,@ " Layout_style.pp t.style;
41 Format.fprintf fmt "members=[@,%a@]@,)"
42 (Format.pp_print_list
43 ~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
44 pp_elem)
45 t.members
46
47let equal eq a b =
48 Option.equal String.equal a.anchor b.anchor
49 && Option.equal String.equal a.tag b.tag
50 && a.implicit = b.implicit
51 && Layout_style.equal a.style b.style
52 && List.equal eq a.members b.members
53
54let compare cmp a b =
55 let c = Option.compare String.compare a.anchor b.anchor in
56 if c <> 0 then c
57 else
58 let c = Option.compare String.compare a.tag b.tag in
59 if c <> 0 then c
60 else
61 let c = Bool.compare a.implicit b.implicit in
62 if c <> 0 then c
63 else
64 let c = Layout_style.compare a.style b.style in
65 if c <> 0 then c else List.compare cmp a.members b.members