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