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(** Yamlrw Unix - Channel and file I/O for YAML
7
8 This module provides channel and file operations for parsing and emitting
9 YAML using bytesrw for efficient streaming I/O. *)
10
11open Bytesrw
12open Yamlrw
13
14(** {1 Types} *)
15
16type value = Value.t
17type yaml = Yaml.t
18type document = Document.t
19
20(** {1 Channel Input} *)
21
22let value_of_channel ?(resolve_aliases = true)
23 ?(max_nodes = Yaml.default_max_alias_nodes)
24 ?(max_depth = Yaml.default_max_alias_depth) ic =
25 let reader = Bytes.Reader.of_in_channel ic in
26 Loader.value_of_reader ~resolve_aliases ~max_nodes ~max_depth reader
27
28let yaml_of_channel ?(resolve_aliases = false)
29 ?(max_nodes = Yaml.default_max_alias_nodes)
30 ?(max_depth = Yaml.default_max_alias_depth) ic =
31 let reader = Bytes.Reader.of_in_channel ic in
32 Loader.yaml_of_reader ~resolve_aliases ~max_nodes ~max_depth reader
33
34let documents_of_channel ic =
35 let reader = Bytes.Reader.of_in_channel ic in
36 Loader.documents_of_reader reader
37
38(** {1 Channel Output} *)
39
40let value_to_channel ?(encoding = `Utf8) ?(scalar_style = `Any)
41 ?(layout_style = `Any) oc (v : value) =
42 let config =
43 { Emitter.default_config with encoding; scalar_style; layout_style }
44 in
45 let writer = Bytes.Writer.of_out_channel oc in
46 Serialize.value_to_writer ~config writer v
47
48let yaml_to_channel ?(encoding = `Utf8) ?(scalar_style = `Any)
49 ?(layout_style = `Any) oc (v : yaml) =
50 let config =
51 { Emitter.default_config with encoding; scalar_style; layout_style }
52 in
53 let writer = Bytes.Writer.of_out_channel oc in
54 Serialize.yaml_to_writer ~config writer v
55
56let documents_to_channel ?(encoding = `Utf8) ?(scalar_style = `Any)
57 ?(layout_style = `Any) ?(resolve_aliases = true) oc docs =
58 let config =
59 { Emitter.default_config with encoding; scalar_style; layout_style }
60 in
61 let writer = Bytes.Writer.of_out_channel oc in
62 Serialize.documents_to_writer ~config ~resolve_aliases writer docs
63
64(** {1 File Input} *)
65
66let value_of_file ?(resolve_aliases = true)
67 ?(max_nodes = Yaml.default_max_alias_nodes)
68 ?(max_depth = Yaml.default_max_alias_depth) path =
69 In_channel.with_open_bin path (fun ic ->
70 value_of_channel ~resolve_aliases ~max_nodes ~max_depth ic)
71
72let yaml_of_file ?(resolve_aliases = false)
73 ?(max_nodes = Yaml.default_max_alias_nodes)
74 ?(max_depth = Yaml.default_max_alias_depth) path =
75 In_channel.with_open_bin path (fun ic ->
76 yaml_of_channel ~resolve_aliases ~max_nodes ~max_depth ic)
77
78let documents_of_file path = In_channel.with_open_bin path documents_of_channel
79
80(** {1 File Output} *)
81
82let value_to_file ?(encoding = `Utf8) ?(scalar_style = `Any)
83 ?(layout_style = `Any) path v =
84 Out_channel.with_open_bin path (fun oc ->
85 value_to_channel ~encoding ~scalar_style ~layout_style oc v)
86
87let yaml_to_file ?(encoding = `Utf8) ?(scalar_style = `Any)
88 ?(layout_style = `Any) path v =
89 Out_channel.with_open_bin path (fun oc ->
90 yaml_to_channel ~encoding ~scalar_style ~layout_style oc v)
91
92let documents_to_file ?(encoding = `Utf8) ?(scalar_style = `Any)
93 ?(layout_style = `Any) ?(resolve_aliases = true) path docs =
94 Out_channel.with_open_bin path (fun oc ->
95 documents_to_channel ~encoding ~scalar_style ~layout_style
96 ~resolve_aliases oc docs)