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 scalar values with metadata *)
7
8type t = {
9 anchor : string option;
10 tag : string option;
11 value : string;
12 plain_implicit : bool;
13 quoted_implicit : bool;
14 style : Scalar_style.t;
15}
16
17let make ?(anchor : string option) ?(tag : string option)
18 ?(plain_implicit = true) ?(quoted_implicit = false) ?(style = `Plain) value
19 =
20 { anchor; tag; value; plain_implicit; quoted_implicit; style }
21
22let value t = t.value
23let anchor t = t.anchor
24let tag t = t.tag
25let style t = t.style
26let plain_implicit t = t.plain_implicit
27let quoted_implicit t = t.quoted_implicit
28let with_anchor anchor t = { t with anchor = Some anchor }
29let with_tag tag t = { t with tag = Some tag }
30let with_style style t = { t with style }
31
32let pp fmt t =
33 Format.fprintf fmt "scalar(%S" t.value;
34 Option.iter (Format.fprintf fmt ", anchor=%s") t.anchor;
35 Option.iter (Format.fprintf fmt ", tag=%s") t.tag;
36 Format.fprintf fmt ", style=%a)" Scalar_style.pp t.style
37
38let equal a b =
39 Option.equal String.equal a.anchor b.anchor
40 && Option.equal String.equal a.tag b.tag
41 && String.equal a.value b.value
42 && a.plain_implicit = b.plain_implicit
43 && a.quoted_implicit = b.quoted_implicit
44 && Scalar_style.equal a.style b.style
45
46let compare a b =
47 let c = Option.compare String.compare a.anchor b.anchor in
48 if c <> 0 then c
49 else
50 let c = Option.compare String.compare a.tag b.tag in
51 if c <> 0 then c
52 else
53 let c = String.compare a.value b.value in
54 if c <> 0 then c
55 else
56 let c = Bool.compare a.plain_implicit b.plain_implicit in
57 if c <> 0 then c
58 else
59 let c = Bool.compare a.quoted_implicit b.quoted_implicit in
60 if c <> 0 then c else Scalar_style.compare a.style b.style