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
9 and emitting 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
23 ?(resolve_aliases = true)
24 ?(max_nodes = Yaml.default_max_alias_nodes)
25 ?(max_depth = Yaml.default_max_alias_depth)
26 ic =
27 let reader = Bytes.Reader.of_in_channel ic in
28 Loader.value_of_reader ~resolve_aliases ~max_nodes ~max_depth reader
29
30let yaml_of_channel
31 ?(resolve_aliases = false)
32 ?(max_nodes = Yaml.default_max_alias_nodes)
33 ?(max_depth = Yaml.default_max_alias_depth)
34 ic =
35 let reader = Bytes.Reader.of_in_channel ic in
36 Loader.yaml_of_reader ~resolve_aliases ~max_nodes ~max_depth reader
37
38let documents_of_channel ic =
39 let reader = Bytes.Reader.of_in_channel ic in
40 Loader.documents_of_reader reader
41
42(** {1 Channel Output} *)
43
44let value_to_channel
45 ?(encoding = `Utf8)
46 ?(scalar_style = `Any)
47 ?(layout_style = `Any)
48 oc
49 (v : value) =
50 let config = { Emitter.default_config with encoding; scalar_style; layout_style } in
51 let writer = Bytes.Writer.of_out_channel oc in
52 Serialize.value_to_writer ~config writer v
53
54let yaml_to_channel
55 ?(encoding = `Utf8)
56 ?(scalar_style = `Any)
57 ?(layout_style = `Any)
58 oc
59 (v : yaml) =
60 let config = { Emitter.default_config with encoding; scalar_style; layout_style } in
61 let writer = Bytes.Writer.of_out_channel oc in
62 Serialize.yaml_to_writer ~config writer v
63
64let documents_to_channel
65 ?(encoding = `Utf8)
66 ?(scalar_style = `Any)
67 ?(layout_style = `Any)
68 ?(resolve_aliases = true)
69 oc
70 docs =
71 let config = { Emitter.default_config with encoding; scalar_style; layout_style } in
72 let writer = Bytes.Writer.of_out_channel oc in
73 Serialize.documents_to_writer ~config ~resolve_aliases writer docs
74
75(** {1 File Input} *)
76
77let value_of_file
78 ?(resolve_aliases = true)
79 ?(max_nodes = Yaml.default_max_alias_nodes)
80 ?(max_depth = Yaml.default_max_alias_depth)
81 path =
82 In_channel.with_open_bin path (fun ic ->
83 value_of_channel ~resolve_aliases ~max_nodes ~max_depth ic)
84
85let yaml_of_file
86 ?(resolve_aliases = false)
87 ?(max_nodes = Yaml.default_max_alias_nodes)
88 ?(max_depth = Yaml.default_max_alias_depth)
89 path =
90 In_channel.with_open_bin path (fun ic ->
91 yaml_of_channel ~resolve_aliases ~max_nodes ~max_depth ic)
92
93let documents_of_file path =
94 In_channel.with_open_bin path documents_of_channel
95
96(** {1 File Output} *)
97
98let value_to_file
99 ?(encoding = `Utf8)
100 ?(scalar_style = `Any)
101 ?(layout_style = `Any)
102 path
103 v =
104 Out_channel.with_open_bin path (fun oc ->
105 value_to_channel ~encoding ~scalar_style ~layout_style oc v)
106
107let yaml_to_file
108 ?(encoding = `Utf8)
109 ?(scalar_style = `Any)
110 ?(layout_style = `Any)
111 path
112 v =
113 Out_channel.with_open_bin path (fun oc ->
114 yaml_to_channel ~encoding ~scalar_style ~layout_style oc v)
115
116let documents_to_file
117 ?(encoding = `Utf8)
118 ?(scalar_style = `Any)
119 ?(layout_style = `Any)
120 ?(resolve_aliases = true)
121 path
122 docs =
123 Out_channel.with_open_bin path (fun oc ->
124 documents_to_channel ~encoding ~scalar_style ~layout_style ~resolve_aliases oc docs)