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