rapidyaml 0.1.3-arm-linux-musl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4bd3f64f12f677d80a9f469e6c8a593d6f923acb8948c6c5cee4936a92546270
4
+ data.tar.gz: a2e12fa85f580ea53f370067deb23443a990c8003ce609c13b9c8e7c7c9366e7
5
+ SHA512:
6
+ metadata.gz: eef8ef2afd6d2ce2ee7de86907e4f22a3fb04678e09960e0e75e128e3579305990471fef1a67bb3ac410e1e021d9e6bfb097e2ba15cd88624c9105e99fa65da6
7
+ data.tar.gz: 2f8e4d5585ad50a2b72bc3aa2a455743d8cdefc1a794719a6ed96aeb81b3da5818fd71547ebf137f9425cd149bebe3a31d4768aa0de8ace763319771121c9746
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Durable Programming LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkmf-rice'
4
+
5
+ # rapidyaml is vendored as a single-header amalgamation at ext/rapidyaml/ryml_all.hpp
6
+ # No system library required.
7
+
8
+ $CXXFLAGS += ' -std=c++17 -O2'
9
+
10
+ unless RbConfig::CONFIG['host_os'] =~ /darwin/
11
+ $LDFLAGS += ' -static-libstdc++ -static-libgcc'
12
+ end
13
+
14
+ create_makefile('rapidyaml/rapidyaml')
15
+
16
+ # Copyright (c) 2026 Durable Programming, LLC. All rights reserved.
@@ -0,0 +1,614 @@
1
+ // Rice 4.x binding layer.
2
+ // Converts ryml tree nodes to Ruby objects; no Ruby concerns in core logic.
3
+
4
+ #include <rice/rice.hpp>
5
+ #include <rice/stl.hpp>
6
+
7
+ #define RYML_SINGLE_HDR_DEFINE_NOW
8
+ #include "ryml_all.hpp"
9
+
10
+ #include <limits>
11
+ #include <stdexcept>
12
+ #include <string>
13
+
14
+ using Rice::Array;
15
+ using Rice::Hash;
16
+ using Rice::Module;
17
+ using Rice::Object;
18
+ using Rice::String;
19
+
20
+ // --------------------------------------------------------------------------
21
+ // ryml error callbacks — throw C++ exceptions instead of calling abort()
22
+ // --------------------------------------------------------------------------
23
+
24
+ struct RymlParseError : public std::runtime_error {
25
+ using std::runtime_error::runtime_error;
26
+ };
27
+
28
+ struct RymlError : public std::runtime_error {
29
+ using std::runtime_error::runtime_error;
30
+ };
31
+
32
+ [[noreturn]] static void
33
+ throwing_error_parse(c4::csubstr msg, ryml::ErrorDataParse const & /*errdata*/,
34
+ void * /*user_data*/) {
35
+ throw RymlParseError(std::string(msg.str, msg.len));
36
+ }
37
+
38
+ [[noreturn]] static void
39
+ throwing_error_basic(c4::csubstr msg, ryml::ErrorDataBasic const & /*errdata*/,
40
+ void * /*user_data*/) {
41
+ throw RymlError(std::string(msg.str, msg.len));
42
+ }
43
+
44
+ [[noreturn]] static void
45
+ throwing_error_visit(c4::csubstr msg, ryml::ErrorDataVisit const & /*errdata*/,
46
+ void * /*user_data*/) {
47
+ throw RymlError(std::string(msg.str, msg.len));
48
+ }
49
+
50
+ // Ruby exception VALUE handles — set during Init_
51
+ static VALUE rb_eRapidYAMLError = Qnil;
52
+ static VALUE rb_eRapidYAMLSyntaxError = Qnil;
53
+
54
+ // --------------------------------------------------------------------------
55
+ // Forward declarations
56
+ // --------------------------------------------------------------------------
57
+
58
+ static Object node_to_ruby(ryml::ConstNodeRef const &node);
59
+ static Object scalar_to_ruby(c4::csubstr val, bool is_null);
60
+ static Object tagged_scalar_to_ruby(c4::csubstr tag, c4::csubstr val,
61
+ bool is_null);
62
+ static Object node_val_to_ruby(ryml::ConstNodeRef const &node);
63
+ static Object node_key_to_ruby(ryml::ConstNodeRef const &child);
64
+ static Object map_to_ruby(ryml::ConstNodeRef const &node);
65
+ static Object seq_to_ruby(ryml::ConstNodeRef const &node);
66
+
67
+ // --------------------------------------------------------------------------
68
+ // Scalar coercion
69
+ // --------------------------------------------------------------------------
70
+
71
+ static Object scalar_to_ruby(c4::csubstr val, bool is_null) {
72
+ if (is_null || val == "~" || val == "null" || val == "Null" ||
73
+ val == "NULL") {
74
+ return Object(Qnil);
75
+ }
76
+
77
+ if (val == "true" || val == "True" || val == "TRUE") {
78
+ return Object(Qtrue);
79
+ }
80
+ if (val == "false" || val == "False" || val == "FALSE") {
81
+ return Object(Qfalse);
82
+ }
83
+
84
+ // YAML 1.2 float specials
85
+ if (val == ".inf" || val == ".Inf" || val == ".INF") {
86
+ return Rice::detail::To_Ruby<double>().convert(
87
+ std::numeric_limits<double>::infinity());
88
+ }
89
+ if (val == "-.inf" || val == "-.Inf" || val == "-.INF") {
90
+ return Rice::detail::To_Ruby<double>().convert(
91
+ -std::numeric_limits<double>::infinity());
92
+ }
93
+ if (val == ".nan" || val == ".NaN" || val == ".NAN") {
94
+ return Rice::detail::To_Ruby<double>().convert(
95
+ std::numeric_limits<double>::quiet_NaN());
96
+ }
97
+
98
+ if (val.len == 0) {
99
+ return Rice::detail::To_Ruby<std::string>().convert(std::string{});
100
+ }
101
+
102
+ std::string s(val.str, val.len);
103
+
104
+ // Detect leading zeros: "090", "00.5", etc. are strings in YAML 1.2.
105
+ // "0" and "-0" are fine; "0x..." goes to the hex path below.
106
+ {
107
+ const char *p = s.c_str();
108
+ bool negative = (*p == '-' || *p == '+');
109
+ const char *digits = p + (negative ? 1 : 0);
110
+ bool leading_zero =
111
+ (digits[0] == '0' && digits[1] != '\0' && digits[1] != 'x' &&
112
+ digits[1] != 'X' && digits[1] != '.');
113
+ if (leading_zero) {
114
+ return Rice::detail::To_Ruby<std::string>().convert(s);
115
+ }
116
+ }
117
+
118
+ // Reject sexagesimal-style scalars (YAML 1.1 only). "20:03:20" is a plain
119
+ // string in YAML 1.2; strtoll would parse only the leading digits and stop.
120
+ // A colon anywhere after the optional sign means this is not a bare integer.
121
+ {
122
+ const char *colon_check =
123
+ s.c_str() + (*s.c_str() == '-' || *s.c_str() == '+' ? 1 : 0);
124
+ if (std::strchr(colon_check, ':') != nullptr) {
125
+ return Rice::detail::To_Ruby<std::string>().convert(s);
126
+ }
127
+ }
128
+
129
+ // Integer?
130
+ {
131
+ char *end = nullptr;
132
+ long long iv = std::strtoll(s.c_str(), &end, 10);
133
+ if (end == s.c_str() + s.size()) {
134
+ return Rice::detail::To_Ruby<long long>().convert(iv);
135
+ }
136
+ }
137
+
138
+ // Hex integer?
139
+ if (s.size() > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
140
+ char *end = nullptr;
141
+ long long iv = std::strtoll(s.c_str(), &end, 16);
142
+ if (end == s.c_str() + s.size()) {
143
+ return Rice::detail::To_Ruby<long long>().convert(iv);
144
+ }
145
+ }
146
+
147
+ // Float?
148
+ {
149
+ char *end = nullptr;
150
+ double dv = std::strtod(s.c_str(), &end);
151
+ if (end == s.c_str() + s.size()) {
152
+ return Rice::detail::To_Ruby<double>().convert(dv);
153
+ }
154
+ }
155
+
156
+ return Rice::detail::To_Ruby<std::string>().convert(s);
157
+ }
158
+
159
+ // Decode base64 (YAML !!binary). Ignores whitespace per RFC 4648.
160
+ static std::string base64_decode(c4::csubstr encoded) {
161
+ static const signed char tbl[256] = {
162
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
163
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
164
+ -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
165
+ 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
166
+ 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
167
+ 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
168
+ 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
169
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
170
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
171
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
172
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
173
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
174
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
175
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
176
+ -1, -1, -1, -1,
177
+ };
178
+ std::string out;
179
+ out.reserve((encoded.len * 3) / 4);
180
+ int buf = 0, bits = 0;
181
+ for (size_t i = 0; i < encoded.len; ++i) {
182
+ signed char v = tbl[(unsigned char)encoded.str[i]];
183
+ if (v == -1) {
184
+ continue; // whitespace
185
+ }
186
+ if (v == -2) {
187
+ break; // padding '='
188
+ }
189
+ buf = (buf << 6) | v;
190
+ bits += 6;
191
+ if (bits >= 8) {
192
+ bits -= 8;
193
+ out += (char)((buf >> bits) & 0xFF);
194
+ }
195
+ }
196
+ return out;
197
+ }
198
+
199
+ // Handle an explicit YAML tag on a scalar value.
200
+ static Object tagged_scalar_to_ruby(c4::csubstr tag, c4::csubstr val,
201
+ bool is_null) {
202
+ std::string t(tag.str, tag.len);
203
+
204
+ // Non-specific tag "!" — treat as string per YAML 1.2 core schema.
205
+ // Psych disagrees (it coerces), but the test suite JSON expects strings.
206
+ if (t == "!" || t == "<!>") {
207
+ return Rice::detail::To_Ruby<std::string>().convert(
208
+ std::string(val.str, val.len));
209
+ }
210
+
211
+ auto tag_is = [&](const char *a, const char *b, const char *c = nullptr,
212
+ const char *d = nullptr) -> bool {
213
+ return t == a || t == b || (c && t == c) || (d && t == d);
214
+ };
215
+
216
+ if (tag_is("!!str", "tag:yaml.org,2002:str", "<tag:yaml.org,2002:str>")) {
217
+ return Rice::detail::To_Ruby<std::string>().convert(
218
+ std::string(val.str, val.len));
219
+ }
220
+
221
+ if (tag_is("!!null", "tag:yaml.org,2002:null", "<tag:yaml.org,2002:null>")) {
222
+ return Object(Qnil);
223
+ }
224
+
225
+ if (tag_is("!!bool", "tag:yaml.org,2002:bool", "<tag:yaml.org,2002:bool>")) {
226
+ std::string v(val.str, val.len);
227
+ if (v == "true" || v == "True" || v == "TRUE") {
228
+ return Object(Qtrue);
229
+ }
230
+ if (v == "false" || v == "False" || v == "FALSE") {
231
+ return Object(Qfalse);
232
+ }
233
+ return scalar_to_ruby(val, is_null);
234
+ }
235
+
236
+ if (tag_is("!!int", "tag:yaml.org,2002:int", "<tag:yaml.org,2002:int>")) {
237
+ std::string s(val.str, val.len);
238
+ char *end = nullptr;
239
+ long long iv = std::strtoll(s.c_str(), &end, 10);
240
+ if (end == s.c_str() + s.size()) {
241
+ return Rice::detail::To_Ruby<long long>().convert(iv);
242
+ }
243
+ return scalar_to_ruby(val, is_null);
244
+ }
245
+
246
+ if (tag_is("!!float", "tag:yaml.org,2002:float",
247
+ "<tag:yaml.org,2002:float>")) {
248
+ std::string s(val.str, val.len);
249
+ char *end = nullptr;
250
+ double dv = std::strtod(s.c_str(), &end);
251
+ if (end == s.c_str() + s.size()) {
252
+ return Rice::detail::To_Ruby<double>().convert(dv);
253
+ }
254
+ return scalar_to_ruby(val, is_null);
255
+ }
256
+
257
+ if (tag_is("!!binary", "tag:yaml.org,2002:binary",
258
+ "<tag:yaml.org,2002:binary>")) {
259
+ std::string decoded = base64_decode(val);
260
+ VALUE rb_str = rb_str_new(decoded.data(), (long)decoded.size());
261
+ rb_enc_associate(rb_str, rb_ascii8bit_encoding());
262
+ return Object(rb_str);
263
+ }
264
+
265
+ // Unknown or unhandled tag — fall through to normal coercion.
266
+ return scalar_to_ruby(val, is_null);
267
+ }
268
+
269
+ static Object node_val_to_ruby(ryml::ConstNodeRef const &node) {
270
+ bool is_null = node.type().has_any(ryml::VALNIL);
271
+ if (node.has_val_tag()) {
272
+ return tagged_scalar_to_ruby(node.val_tag(), node.val(), is_null);
273
+ }
274
+ if (node.type().has_any(ryml::VAL_DQUO | ryml::VAL_SQUO)) {
275
+ return Rice::detail::To_Ruby<std::string>().convert(
276
+ std::string(node.val().str, node.val().len));
277
+ }
278
+ return scalar_to_ruby(node.val(), is_null);
279
+ }
280
+
281
+ static Object node_key_to_ruby(ryml::ConstNodeRef const &child) {
282
+ bool key_null = child.type().has_any(ryml::KEYNIL);
283
+ if (child.has_key_tag()) {
284
+ return tagged_scalar_to_ruby(child.key_tag(), child.key(), key_null);
285
+ }
286
+ if (child.type().has_any(ryml::KEY_DQUO | ryml::KEY_SQUO)) {
287
+ return Rice::detail::To_Ruby<std::string>().convert(
288
+ std::string(child.key().str, child.key().len));
289
+ }
290
+ return scalar_to_ruby(child.key(), key_null);
291
+ }
292
+
293
+ static bool tag_is_omap(c4::csubstr tag) {
294
+ std::string t(tag.str, tag.len);
295
+ return t == "!!omap" || t == "tag:yaml.org,2002:omap" ||
296
+ t == "<tag:yaml.org,2002:omap>";
297
+ }
298
+
299
+ static Object map_to_ruby(ryml::ConstNodeRef const &node) {
300
+ Hash hash;
301
+ for (ryml::ConstNodeRef child : node.children()) {
302
+ hash[node_key_to_ruby(child)] = node_to_ruby(child);
303
+ }
304
+ return hash;
305
+ }
306
+
307
+ static Object seq_omap_to_ruby(ryml::ConstNodeRef const &node) {
308
+ // !!omap: each child is a single-key mapping; emit as array of hashes.
309
+ Array arr;
310
+ for (ryml::ConstNodeRef child : node.children()) {
311
+ if (child.is_map()) {
312
+ arr.push(map_to_ruby(child));
313
+ } else {
314
+ arr.push(node_to_ruby(child));
315
+ }
316
+ }
317
+ return arr;
318
+ }
319
+
320
+ static Object seq_to_ruby(ryml::ConstNodeRef const &node) {
321
+ if (node.has_val_tag() && tag_is_omap(node.val_tag())) {
322
+ return seq_omap_to_ruby(node);
323
+ }
324
+ Array arr;
325
+ for (ryml::ConstNodeRef child : node.children()) {
326
+ arr.push(node_to_ruby(child));
327
+ }
328
+ return arr;
329
+ }
330
+
331
+ // --------------------------------------------------------------------------
332
+ // Tree walker
333
+ // --------------------------------------------------------------------------
334
+
335
+ static Object node_to_ruby(ryml::ConstNodeRef const &node) {
336
+ // STREAM — wrap multiple documents in an array; unwrap single doc
337
+ if (node.is_stream()) {
338
+ ryml::id_type ndocs = node.num_children();
339
+ if (ndocs == 0) {
340
+ return Object(Qnil);
341
+ }
342
+ if (ndocs == 1) {
343
+ return node_to_ruby(node[0]);
344
+ }
345
+ Array arr;
346
+ for (ryml::ConstNodeRef child : node.children()) {
347
+ arr.push(node_to_ruby(child));
348
+ }
349
+ return arr;
350
+ }
351
+
352
+ // DOC wrapper — the doc IS the map/seq/scalar; don't descend into node[0]
353
+ if (node.is_doc()) {
354
+ if (node.has_val()) {
355
+ return node_val_to_ruby(node);
356
+ }
357
+ if (node.is_map()) {
358
+ return map_to_ruby(node);
359
+ }
360
+ if (node.is_seq()) {
361
+ return seq_to_ruby(node);
362
+ }
363
+ return Object(Qnil);
364
+ }
365
+
366
+ if (node.is_map()) {
367
+ return map_to_ruby(node);
368
+ }
369
+
370
+ if (node.is_seq()) {
371
+ return seq_to_ruby(node);
372
+ }
373
+
374
+ if (node.has_val()) {
375
+ return node_val_to_ruby(node);
376
+ }
377
+
378
+ return Object(Qnil);
379
+ }
380
+
381
+ // --------------------------------------------------------------------------
382
+ // Ruby → ryml tree
383
+ // --------------------------------------------------------------------------
384
+
385
+ // Returns an arena-interned csubstr for a Ruby string/symbol value.
386
+ static c4::csubstr ruby_str_to_arena(ryml::Tree &tree, VALUE v) {
387
+ if (rb_type(v) == T_SYMBOL) {
388
+ v = rb_sym_to_s(v);
389
+ }
390
+ std::string s(RSTRING_PTR(v), (size_t)RSTRING_LEN(v));
391
+ return tree.copy_to_arena(ryml::to_csubstr(s));
392
+ }
393
+
394
+ // Forward declaration
395
+ static void ruby_val_to_node(ryml::NodeRef node, VALUE val);
396
+
397
+ // Writes val into node, which may already have a key set (map child).
398
+ // Sets MAP/SEQ/VAL type flags and populates children recursively.
399
+ static void ruby_val_to_node(ryml::NodeRef node, VALUE val) {
400
+ int type = rb_type(val);
401
+
402
+ if (type == T_NIL) {
403
+ node |= ryml::VAL;
404
+ node.set_val(node.tree()->copy_to_arena(ryml::to_csubstr("~")));
405
+ return;
406
+ }
407
+ if (type == T_TRUE) {
408
+ node |= ryml::VAL;
409
+ node.set_val(node.tree()->copy_to_arena(ryml::to_csubstr("true")));
410
+ return;
411
+ }
412
+ if (type == T_FALSE) {
413
+ node |= ryml::VAL;
414
+ node.set_val(node.tree()->copy_to_arena(ryml::to_csubstr("false")));
415
+ return;
416
+ }
417
+ if (type == T_FIXNUM || type == T_BIGNUM) {
418
+ node |= ryml::VAL;
419
+ node.set_val(node.tree()->to_arena(NUM2LL(val)));
420
+ return;
421
+ }
422
+ if (type == T_FLOAT) {
423
+ node |= ryml::VAL;
424
+ node.set_val(node.tree()->to_arena(NUM2DBL(val)));
425
+ return;
426
+ }
427
+ if (type == T_SYMBOL) {
428
+ // Emit as ":name" plain scalar — matches Psych's wire format.
429
+ // Empty symbol uses single-quote to prevent misreading as a tag.
430
+ VALUE name = rb_sym_to_s(val);
431
+ std::string sym_name(RSTRING_PTR(name), (size_t)RSTRING_LEN(name));
432
+ if (sym_name.empty()) {
433
+ // Psych emits `--- !ruby/symbol\n` for empty symbol; we approximate
434
+ // with a single-quoted empty-colon string that round-trips correctly.
435
+ c4::csubstr s = node.tree()->copy_to_arena(ryml::to_csubstr(":"));
436
+ node |= ryml::VAL | ryml::VAL_SQUO;
437
+ node.set_val(s);
438
+ } else {
439
+ std::string colon_name = ":" + sym_name;
440
+ c4::csubstr s = node.tree()->copy_to_arena(ryml::to_csubstr(colon_name));
441
+ node |= ryml::VAL;
442
+ node.set_val(s);
443
+ }
444
+ return;
445
+ }
446
+ if (type == T_STRING) {
447
+ c4::csubstr s = ruby_str_to_arena(*node.tree(), val);
448
+ if (s.len == 0) {
449
+ // Empty string must be quoted so it round-trips as "" not null.
450
+ node |= ryml::VAL | ryml::VAL_SQUO;
451
+ } else {
452
+ node |= ryml::VAL;
453
+ }
454
+ node.set_val(s);
455
+ return;
456
+ }
457
+ // Date and Time — emit via strftime so they round-trip cleanly.
458
+ // T_DATA covers both; distinguish by class name.
459
+ if (type == T_DATA || type == T_OBJECT) {
460
+ VALUE klass = CLASS_OF(val);
461
+ VALUE klass_name = rb_class_name(klass);
462
+ std::string class_name(RSTRING_PTR(klass_name),
463
+ (size_t)RSTRING_LEN(klass_name));
464
+ if (class_name == "Date") {
465
+ VALUE str = rb_funcall(val, rb_intern("strftime"), 1,
466
+ rb_str_new_cstr("%Y-%m-%d"));
467
+ c4::csubstr s = ruby_str_to_arena(*node.tree(), str);
468
+ node |= ryml::VAL;
469
+ node.set_val(s);
470
+ return;
471
+ }
472
+ if (class_name == "Time") {
473
+ VALUE result = rb_funcall(val, rb_intern("strftime"), 1,
474
+ rb_str_new_cstr("%Y-%m-%d %H:%M:%S.%N %z"));
475
+ std::string ts(RSTRING_PTR(result), (size_t)RSTRING_LEN(result));
476
+ // Reformat trailing offset "+HHMM" → "+HH:MM", "+0000" → "Z"
477
+ if (ts.size() >= 5) {
478
+ std::string offset = ts.substr(ts.size() - 5);
479
+ ts = ts.substr(0, ts.size() - 5);
480
+ if (offset == "+0000" || offset == "-0000") {
481
+ ts += "Z";
482
+ } else {
483
+ ts += offset.substr(0, 3) + ":" + offset.substr(3);
484
+ }
485
+ }
486
+ c4::csubstr s = node.tree()->copy_to_arena(ryml::to_csubstr(ts));
487
+ node |= ryml::VAL;
488
+ node.set_val(s);
489
+ return;
490
+ }
491
+ }
492
+ if (type == T_ARRAY) {
493
+ node |= ryml::SEQ;
494
+ long len = RARRAY_LEN(val);
495
+ for (long i = 0; i < len; ++i) {
496
+ ruby_val_to_node(node.append_child(), rb_ary_entry(val, i));
497
+ }
498
+ return;
499
+ }
500
+ if (type == T_HASH) {
501
+ node |= ryml::MAP;
502
+ VALUE keys = rb_funcall(val, rb_intern("keys"), 0);
503
+ long len = RARRAY_LEN(keys);
504
+ for (long i = 0; i < len; ++i) {
505
+ VALUE k = rb_ary_entry(keys, i);
506
+ VALUE v = rb_hash_aref(val, k);
507
+ ryml::NodeRef child = node.append_child();
508
+ bool k_is_str = (rb_type(k) == T_STRING || rb_type(k) == T_SYMBOL);
509
+ child |= k_is_str ? ryml::KEY : (ryml::KEY | ryml::KEY_SQUO);
510
+ VALUE k_str = k_is_str ? k : rb_funcall(k, rb_intern("to_s"), 0);
511
+ child.set_key(ruby_str_to_arena(*node.tree(), k_str));
512
+ ruby_val_to_node(child, v);
513
+ }
514
+ return;
515
+ }
516
+ // Fallback: to_s
517
+ VALUE str = rb_funcall(val, rb_intern("to_s"), 0);
518
+ node |= ryml::VAL;
519
+ node.set_val(ruby_str_to_arena(*node.tree(), str));
520
+ }
521
+
522
+ // --------------------------------------------------------------------------
523
+ // Ruby-facing functions
524
+ // --------------------------------------------------------------------------
525
+
526
+ static Object ext_parse(String input) {
527
+ std::string src = input.str();
528
+ ryml::Tree tree;
529
+ try {
530
+ tree = ryml::parse_in_arena(ryml::to_csubstr(src));
531
+ // Expand all anchors and aliases in-place so node_to_ruby sees plain
532
+ // values.
533
+ tree.resolve();
534
+ } catch (RymlParseError const &e) {
535
+ throw Rice::Exception(rb_eRapidYAMLSyntaxError, "%s", e.what());
536
+ } catch (std::exception const &e) {
537
+ throw Rice::Exception(rb_eRapidYAMLError, "%s", e.what());
538
+ }
539
+ return node_to_ruby(tree.rootref());
540
+ }
541
+
542
+ // Always returns an Array of documents, even for a single-document stream.
543
+ static Array ext_parse_stream(String input) {
544
+ std::string src = input.str();
545
+ ryml::Tree tree;
546
+ try {
547
+ tree = ryml::parse_in_arena(ryml::to_csubstr(src));
548
+ tree.resolve();
549
+ } catch (RymlParseError const &e) {
550
+ throw Rice::Exception(rb_eRapidYAMLSyntaxError, "%s", e.what());
551
+ } catch (std::exception const &e) {
552
+ throw Rice::Exception(rb_eRapidYAMLError, "%s", e.what());
553
+ }
554
+ Array arr;
555
+ ryml::ConstNodeRef root = tree.rootref();
556
+ if (root.is_stream()) {
557
+ for (ryml::ConstNodeRef child : root.children()) {
558
+ // Skip empty/comment-only documents (DOC node with no real content).
559
+ if (child.is_doc() && !child.has_val() && !child.is_map() &&
560
+ !child.is_seq()) {
561
+ continue;
562
+ }
563
+ arr.push(node_to_ruby(child));
564
+ }
565
+ } else if (root.num_children() > 0 || root.is_map() || root.is_seq() ||
566
+ (root.has_val() && !root.type().has_any(ryml::VALNIL))) {
567
+ arr.push(node_to_ruby(root));
568
+ }
569
+ return arr;
570
+ }
571
+
572
+ static String ext_ryml_version() { return String(RYML_VERSION); }
573
+
574
+ static String ext_emit(Object obj) {
575
+ try {
576
+ ryml::Tree tree;
577
+ ruby_val_to_node(tree.rootref(), obj.value());
578
+ std::string yaml = ryml::emitrs_yaml<std::string>(tree);
579
+ return String(yaml.c_str());
580
+ } catch (std::exception const &e) {
581
+ throw Rice::Exception(rb_eRapidYAMLError, "%s", e.what());
582
+ }
583
+ }
584
+
585
+ // --------------------------------------------------------------------------
586
+ // Init
587
+ // --------------------------------------------------------------------------
588
+
589
+ extern "C" void Init_rapidyaml() {
590
+ // Install throwing error callbacks so parse errors raise Ruby exceptions
591
+ // rather than calling abort(). Must happen before any ryml operations.
592
+ ryml::Callbacks cb = ryml::get_callbacks();
593
+ cb.m_error_basic = throwing_error_basic;
594
+ cb.m_error_parse = throwing_error_parse;
595
+ cb.m_error_visit = throwing_error_visit;
596
+ ryml::set_callbacks(cb);
597
+
598
+ Module rb_mRapidYAML = Rice::define_module("RapidYAML");
599
+
600
+ // RapidYAML::Error — base for all library errors (mirrors Psych::Exception)
601
+ rb_eRapidYAMLError =
602
+ rb_define_class_under(rb_mRapidYAML.value(), "Error", rb_eStandardError);
603
+
604
+ // RapidYAML::SyntaxError — raised on invalid YAML input (mirrors
605
+ // Psych::SyntaxError)
606
+ rb_eRapidYAMLSyntaxError = rb_define_class_under(
607
+ rb_mRapidYAML.value(), "SyntaxError", rb_eRapidYAMLError);
608
+
609
+ Module rb_mExt = Rice::define_module_under(rb_mRapidYAML, "Ext");
610
+ rb_mExt.define_module_function("parse", &ext_parse);
611
+ rb_mExt.define_module_function("parse_stream", &ext_parse_stream);
612
+ rb_mExt.define_module_function("emit", &ext_emit);
613
+ rb_mExt.define_module_function("ryml_version", &ext_ryml_version);
614
+ }