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 document with directives and content *)
7
8type t = {
9 version : (int * int) option;
10 tags : (string * string) list;
11 root : Yaml.t option;
12 implicit_start : bool;
13 implicit_end : bool;
14}
15
16let make
17 ?(version : (int * int) option)
18 ?(tags : (string * string) list = [])
19 ?(implicit_start = true)
20 ?(implicit_end = true)
21 root =
22 { version; tags; root; implicit_start; implicit_end }
23
24let version t = t.version
25let tags t = t.tags
26let root t = t.root
27let implicit_start t = t.implicit_start
28let implicit_end t = t.implicit_end
29
30let with_version version t = { t with version = Some version }
31let with_tags tags t = { t with tags }
32let with_root root t = { t with root = Some root }
33
34let pp fmt t =
35 Format.fprintf fmt "@[<v 2>document(@,";
36 (match t.version with
37 | Some (maj, min) -> Format.fprintf fmt "version=%d.%d,@ " maj min
38 | None -> ());
39 if t.tags <> [] then begin
40 Format.fprintf fmt "tags=[";
41 List.iteri (fun i (h, p) ->
42 if i > 0 then Format.fprintf fmt ", ";
43 Format.fprintf fmt "%s -> %s" h p
44 ) t.tags;
45 Format.fprintf fmt "],@ "
46 end;
47 Format.fprintf fmt "implicit_start=%b,@ " t.implicit_start;
48 Format.fprintf fmt "implicit_end=%b,@ " t.implicit_end;
49 (match t.root with
50 | Some root -> Format.fprintf fmt "root=%a" Yaml.pp root
51 | None -> Format.fprintf fmt "root=<empty>");
52 Format.fprintf fmt "@]@,)"
53
54let equal a b =
55 Option.equal (fun (a1, a2) (b1, b2) -> a1 = b1 && a2 = b2) a.version b.version &&
56 List.equal (fun (h1, p1) (h2, p2) -> h1 = h2 && p1 = p2) a.tags b.tags &&
57 Option.equal Yaml.equal a.root b.root &&
58 a.implicit_start = b.implicit_start &&
59 a.implicit_end = b.implicit_end