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 (match t.anchor with 40 | Some a -> Format.fprintf fmt ", anchor=%s" a 41 | None -> ()); 42 (match t.tag with 43 | Some tag -> Format.fprintf fmt ", tag=%s" tag 44 | None -> ()); 45 Format.fprintf fmt ", style=%a)" Scalar_style.pp t.style 46 47let equal a b = 48 Option.equal String.equal a.anchor b.anchor && 49 Option.equal String.equal a.tag b.tag && 50 String.equal a.value b.value && 51 a.plain_implicit = b.plain_implicit && 52 a.quoted_implicit = b.quoted_implicit && 53 Scalar_style.equal a.style b.style 54 55let compare a b = 56 let c = Option.compare String.compare a.anchor b.anchor in 57 if c <> 0 then c else 58 let c = Option.compare String.compare a.tag b.tag in 59 if c <> 0 then c else 60 let c = String.compare a.value b.value in 61 if c <> 0 then c else 62 let c = Bool.compare a.plain_implicit b.plain_implicit in 63 if c <> 0 then c else 64 let c = Bool.compare a.quoted_implicit b.quoted_implicit in 65 if c <> 0 then c else 66 Scalar_style.compare a.style b.style