babbel_yaml 0.1.0

Fast, modular YAML 1.2 parser and emitter with anchors, aliases, and tags
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! Default YAML Stringification
//!
//! Provides functions for converting YAML nodes to their default string representations,
//! including escaping, formatting, and output to destinations.
//!
//! Copyright (c) 2026 YAML Library Developers

use crate::constants::*;
use crate::error::YamlError;
use crate::io::traits::IDestination;
use crate::nodes::node::*;
use crate::stringify::traits::NodeSerializer;

/// Default YAML Serializer implementing `NodeSerializer` (OCP & DIP)
#[derive(Debug, Default, Clone, Copy)]
pub struct YamlSerializer;

impl NodeSerializer for YamlSerializer {
    fn serialize(&self, node: &Node, dest: &mut dyn IDestination) -> crate::error::Result<()> {
        stringify(node, dest)
    }
}

/// Escapes special characters in a string for double-quoted YAML representation.
///
/// Processes characters that need escaping in double-quoted strings including
/// newlines, carriage returns, tabs, and backslashes. Preserves existing
/// escape sequences when appropriate.
///
/// # Arguments
///
/// * `s` - The string to escape
///
/// # Returns
///
/// A new String with appropriate escape sequences
fn escape_double(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut iter = s.chars().peekable();
    while let Some(c) = iter.next() {
        match c {
            CHAR_NEWLINE => out.push(CHAR_NEWLINE),
            CHAR_CARRIAGE_RETURN => {
                out.push(CHAR_BACKSLASH);
                out.push('r');
            }
            CHAR_TAB => {
                out.push(CHAR_BACKSLASH);
                out.push('t');
            }
            CHAR_BACKSLASH => {
                if let Some(&next) = iter.peek() {
                    match next {
                        'n' | 'r' | 't' | 'b' | 'x' => {
                            out.push(CHAR_BACKSLASH);
                            out.push(next);
                            iter.next();
                        }
                        _ => {
                            out.push(CHAR_BACKSLASH);
                            out.push(CHAR_BACKSLASH);
                        }
                    }
                } else {
                    out.push(CHAR_BACKSLASH);
                    out.push(CHAR_BACKSLASH);
                }
            }
            '"' => {
                out.push(CHAR_BACKSLASH);
                out.push(CHAR_DOUBLE_QUOTE);
            }
            c if (c as u32) < 0x20 || (c as u32) == 0x7f => {
                out.push_str(&format!("\\u{:04x}", c as u32));
            }
            other => out.push(other),
        }
    }
    out
}

/// Escapes single quotes in a string for single-quoted YAML representation.
///
/// Handles the single quote escaping rule where single quotes are escaped
/// by doubling them ('') in single-quoted YAML strings.
///
/// # Arguments
///
/// * `s` - The string to escape
///
/// # Returns
///
/// A new String with single quotes properly escaped
fn escape_single(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        if c == CHAR_SINGLE_QUOTE {
            out.push(CHAR_SINGLE_QUOTE);
            out.push(CHAR_SINGLE_QUOTE);
        } else {
            out.push(c);
        }
    }
    out
}

/// Normalizes newline characters in a string to use Unix-style line endings.
///
/// Converts Windows-style CRLF sequences to LF for consistent output.
///
/// # Arguments
///
/// * `s` - The string to normalize
///
/// # Returns
///
/// A new String with normalized line endings
use crate::nodes::node::{BlockStyle, Node, QuoteType};

fn normalize_newlines(s: &str) -> String {
    // Restore original CR removal logic for test compatibility
    s.replace(CHAR_CARRIAGE_RETURN, "")
}

/// Recursively stringifies a YAML node with the specified indentation level.
///
/// Handles all node types including scalars, arrays, mappings, documents,
/// anchors, aliases, and comments. Applies proper indentation and formatting
/// rules based on the node type and content.
///
/// # Arguments
///
/// * `node` - The Node to stringify
/// * `destination` - The output destination for the YAML content
/// * `indent` - The current indentation level (number of spaces)
///
/// # Returns
///
/// Result indicating success or an error string
fn stringify_document_with_indent(
    node: &Node,
    destination: &mut dyn IDestination,
    indent: usize,
) -> Result<(), YamlError> {
    let indent_str = "  ".repeat(indent);
    match node {
        Node::None => destination.add_bytes(&format!("{indent_str}null")),
        Node::Boolean(b) => destination.add_bytes(&format!("{indent_str}{b}")),
        Node::Str(s, qt, style) => {
            let s = normalize_newlines(s);
            match qt {
                QuoteType::Double => destination.add_bytes(&format!(
                    "{}{}{}{}",
                    indent_str,
                    CHAR_DOUBLE_QUOTE,
                    escape_double(&s),
                    CHAR_DOUBLE_QUOTE
                )),
                QuoteType::Single => {
                    if !s.contains(CHAR_NEWLINE)
                        && (s.contains(CHAR_SINGLE_QUOTE) || s.contains(CHAR_BACKSLASH))
                    {
                        destination.add_bytes(&format!(
                            "{}{}{}{}",
                            indent_str,
                            CHAR_DOUBLE_QUOTE,
                            escape_double(&s),
                            CHAR_DOUBLE_QUOTE
                        ))
                    } else {
                        destination.add_bytes(&format!(
                            "{}{}{}{}",
                            indent_str,
                            CHAR_SINGLE_QUOTE,
                            escape_single(&s),
                            CHAR_SINGLE_QUOTE
                        ))
                    }
                }
                QuoteType::Unquoted => {
                    if s.contains(CHAR_NEWLINE) || matches!(style, BlockStyle::Literal) {
                        let is_literal = matches!(style, BlockStyle::Literal);

                        let lines: Vec<&str> = s.split(CHAR_NEWLINE).collect();
                        let needs_indent = if is_literal {
                            lines.iter().any(|l| !l.is_empty() && !l.starts_with(' '))
                        } else {
                            true
                        };
                        let content_indent = if needs_indent {
                            "  ".repeat(indent + 1)
                        } else {
                            String::new()
                        };
                        destination
                            .add_bytes(&format!("{indent_str}{STR_LITERAL_BLOCK}{CHAR_NEWLINE}"));

                        if !s.contains(CHAR_NEWLINE) && is_literal {
                            destination.add_bytes(&format!("{content_indent}{s}{CHAR_NEWLINE}"));
                        } else {
                            for line in lines {
                                if line.is_empty() {
                                    destination.add_bytes(&CHAR_NEWLINE.to_string());
                                } else {
                                    destination.add_bytes(&format!(
                                        "{content_indent}{line}{CHAR_NEWLINE}"
                                    ));
                                }
                            }
                        }
                    } else {
                        destination.add_bytes(&format!("{indent_str}{s}"))
                    }
                }
            }
        }
        Node::Comment(c) => {
            let c = normalize_newlines(c);
            destination.add_bytes(&format!("{indent_str}{CHAR_HASH}{CHAR_SPACE}{c}"))
        }
        Node::Number(num) => match num {
            Numeric::Integer(i) => destination.add_bytes(&format!("{indent_str}{i}")),
            Numeric::Float(f) => destination.add_bytes(&format!("{indent_str}{f}")),
            _ => destination.add_bytes(&format!("{indent_str}{num:?}")),
        },
        Node::Array(items) => {
            for item in items {
                destination.add_bytes(&format!("{indent_str}{CHAR_DASH}{CHAR_SPACE}"));
                match item {
                    Node::Mapping(_) => {
                        let mut buf = crate::io::destinations::buffer::Buffer::new();
                        stringify_document_with_indent(item, &mut buf, indent + 1)?;
                        let mut out = buf.to_string();
                        let child_indent = "  ".repeat(indent + 1);
                        if out.starts_with(&child_indent) {
                            out = out.split_off(child_indent.len());
                        }
                        destination.add_bytes(&out);
                    }
                    Node::Array(_) => {
                        let mut buf = crate::io::destinations::buffer::Buffer::new();
                        stringify_document_with_indent(item, &mut buf, indent + 1)?;
                        let mut out = buf.to_string();
                        let child_indent = "  ".repeat(indent + 1);
                        if out.starts_with(&child_indent) {
                            out = out.split_off(child_indent.len());
                        }
                        destination.add_bytes(&out);
                    }
                    _ => {
                        stringify_document_with_indent(item, destination, 0)?;
                        destination.add_bytes(&CHAR_NEWLINE.to_string());
                    }
                }
            }
        }

        Node::Set(items) => {
            // Render sets as plain sequences (without !!set tag)
            for item in items {
                destination.add_bytes(&format!("{indent_str}{CHAR_DASH}{CHAR_SPACE}"));
                match item {
                    Node::Mapping(_) | Node::Array(_) | Node::Set(_) => {
                        destination.add_bytes(&CHAR_NEWLINE.to_string());
                        stringify_document_with_indent(item, destination, indent + 1)?;
                    }
                    _ => {
                        stringify_document_with_indent(item, destination, 0)?;
                        destination.add_bytes(&CHAR_NEWLINE.to_string());
                    }
                }
            }
        }

        Node::Mapping(pairs) => {
            for (key_node, value) in pairs {
                let key_str = match key_node {
                    Node::Number(Numeric::Float(f)) => format!("\"{}\"", f),
                    Node::Number(Numeric::Integer(i)) => format!("{}", i),
                    _ => {
                        let mut key_buf = crate::io::destinations::buffer::Buffer::new();
                        stringify_document_with_indent(key_node, &mut key_buf, 0)?;
                        key_buf.to_string()
                    }
                };

                destination.add_bytes(&format!("{indent_str}{key_str}{CHAR_COLON}{CHAR_SPACE}"));

                match value {
                    Node::Array(_) | Node::Mapping(_) | Node::Set(_) => {
                        destination.add_bytes(&CHAR_NEWLINE.to_string());
                        stringify_document_with_indent(value, destination, indent + 1)?;
                    }
                    Node::Str(_, QuoteType::Unquoted, BlockStyle::Literal) => {
                        stringify_document_with_indent(value, destination, 0)?;
                    }
                    _ => {
                        stringify_document_with_indent(value, destination, 0)?;
                        destination.add_bytes(&CHAR_NEWLINE.to_string());
                    }
                }
            }
        }
        Node::Document(nodes) => {
            for node in nodes {
                stringify_document_with_indent(node, destination, indent)?;
            }
        }
        Node::Anchored(inner, name) => {
            destination.add_bytes(&format!("{CHAR_AMPERSAND}{name}{CHAR_SPACE}"));
            stringify_document_with_indent(inner, destination, indent)?;
        }
        Node::Tagged(inner, tag) => {
            destination.add_bytes(&format!("{indent_str}{tag}{CHAR_SPACE}"));
            stringify_document_with_indent(inner, destination, indent)?;
        }
        Node::Alias(name) => {
            destination.add_bytes(&format!("{CHAR_ASTERISK}{name}"));
        }
        _ => {
            return Err(crate::error::messages::ERR_UNSUPPORTED_NODE_TYPE
                .to_string()
                .into());
        }
    }
    Ok(())
}

/// Determines if a node represents blank content that should be omitted.
///
/// Checks if a node is considered blank for stringification purposes,
/// such as None nodes, empty arrays, or empty strings.
///
/// # Arguments
///
/// * `node` - The Node to check
///
/// # Returns
///
/// true if the node is considered blank, false otherwise

/// Stringifies a single YAML document to the destination.
///
/// Converts a document node to its YAML string representation,
/// starting with zero indentation. This is typically used for
/// individual documents within a multi-document stream.
///
/// # Arguments
///
/// * `node` - The Document Node to stringify
/// * `destination` - The output destination for the YAML content
///
/// # Returns
///
/// Result indicating success or an error string
pub fn stringify_document(
    node: &Node,
    destination: &mut dyn IDestination,
) -> Result<(), YamlError> {
    stringify_document_with_indent(node, destination, 0)
}

/// Main entry point for stringifying YAML nodes to their text representation.
///
/// Converts any YAML node structure (documents, individual nodes) to
/// properly formatted YAML text. Handles multi-document streams by
/// adding appropriate document separators.
///
/// # Arguments
///
/// * `node` - The root Node to stringify
/// * `destination` - The output destination for the YAML content
///
/// # Returns
///
/// Result indicating success or an error string
pub fn stringify(node: &Node, destination: &mut dyn IDestination) -> Result<(), YamlError> {
    match node {
        Node::Documents(docs) => {
            for doc in docs {
                if let Node::Document(nodes) = doc {
                    if nodes.iter().all(|n| n.is_blank()) {
                        destination.add_bytes(&format!(
                            "{}{}",
                            crate::constants::STR_DOC_START,
                            crate::constants::CHAR_NEWLINE
                        ));
                        continue;
                    }
                }

                if let Node::Document(nodes) = doc {
                    if nodes.len() == 1 {
                        if let Node::Str(s, QuoteType::Unquoted, BlockStyle::Literal) = &nodes[0] {
                            let s = normalize_newlines(s);
                            destination.add_bytes(&format!(
                                "{} {}{}",
                                crate::constants::STR_DOC_START,
                                STR_LITERAL_BLOCK,
                                CHAR_NEWLINE
                            ));
                            for line in s.split(CHAR_NEWLINE) {
                                destination.add_bytes(&format!("{line}{CHAR_NEWLINE}"));
                            }
                            destination.add_bytes(&format!(
                                "{}{}",
                                crate::constants::STR_DOC_END,
                                CHAR_NEWLINE
                            ));
                            continue;
                        }
                    }
                }

                destination.add_bytes(&format!(
                    "{}{}",
                    crate::constants::STR_DOC_START,
                    crate::constants::CHAR_NEWLINE
                ));
                stringify_document(doc, destination)?;
                // Add newline before ... if not already present
                if destination.last() != Some(b'\n') {
                    destination.add_bytes("\n");
                }
                destination.add_bytes(&format!(
                    "{}{}",
                    crate::constants::STR_DOC_END,
                    crate::constants::CHAR_NEWLINE
                ));
            }
        }
        _ => {
            stringify_document(node, destination)?;
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_stringify_empty_document() {
        let node = Node::Documents(vec![Node::Document(vec![])]);
        let mut buf = Buffer::new();
        stringify(&node, &mut buf).expect("stringify failed");
        assert_eq!(buf.to_string(), "---\n");
    }

    #[test]
    fn test_stringify_boolean_values() {
        let node = Node::Documents(vec![Node::Document(vec![
            Node::Boolean(true),
            Node::Boolean(false),
        ])]);
        let mut buf = Buffer::new();
        stringify(&node, &mut buf).expect("stringify failed");
        assert_eq!(buf.to_string(), "---\ntruefalse\n...\n");
    }

    #[test]
    fn test_stringify_quoted_strings() {
        let node = Node::Documents(vec![Node::Document(vec![
            Node::Str("quoted".to_string(), QuoteType::Double, BlockStyle::None),
            Node::Str("single".to_string(), QuoteType::Single, BlockStyle::None),
        ])]);
        let mut buf = Buffer::new();
        stringify(&node, &mut buf).expect("stringify failed");
        assert!(buf.to_string().contains("\"quoted\""));
        assert!(buf.to_string().contains("'single'"));
    }

    #[test]
    fn test_stringify_nested_array_and_mapping() {
        let node = Node::Documents(vec![Node::Document(vec![Node::Array(vec![
            Node::Mapping(vec![
                (Node::from("key1"), Node::from("val1")),
                (Node::from("key2"), Node::from("val2")),
            ]),
            Node::Array(vec![
                Node::Number(Numeric::Integer(10)),
                Node::Number(Numeric::Integer(20)),
            ]),
        ])])]);
        let mut buf = Buffer::new();
        stringify(&node, &mut buf).expect("stringify failed");
        let out = buf.to_string();
        assert!(out.contains("key1: val1"));
        assert!(out.contains("key2: val2"));
        assert!(out.contains("- 10"));
        assert!(out.contains("- 20"));
    }

    #[test]
    fn test_stringify_tagged_and_anchored() {
        let tagged = Node::Tagged(
            Box::new(Node::Str(
                "tagged".to_string(),
                QuoteType::Unquoted,
                BlockStyle::None,
            )),
            "!tag".to_string(),
        );
        let anchored = Node::Anchored(
            Box::new(Node::Str(
                "anchored".to_string(),
                QuoteType::Unquoted,
                BlockStyle::None,
            )),
            "anchor1".to_string(),
        );
        let node = Node::Documents(vec![Node::Document(vec![tagged, anchored])]);
        let mut buf = Buffer::new();
        stringify(&node, &mut buf).expect("stringify failed");
        let out = buf.to_string();
        assert!(out.contains("!tag tagged"));
        assert!(out.contains("&anchor1 anchored"));
    }

    #[test]
    fn test_stringify_alias() {
        let alias = Node::Alias("anchor1".to_string());
        let node = Node::Documents(vec![Node::Document(vec![alias])]);
        let mut buf = Buffer::new();
        stringify(&node, &mut buf).expect("stringify failed");
        assert!(buf.to_string().contains("*anchor1"));
    }
    use super::*;
    use crate::io::destinations::buffer::Buffer;
    use crate::nodes::node::{BlockStyle, Node, Numeric, QuoteType};

    #[test]
    fn test_escape_double_basic() {
        assert_eq!(escape_double("a\"b"), "a\\\"b");

        assert_eq!(escape_double("\\n"), "\\n");

        assert_eq!(escape_double("\u{0001}"), "\\u0001");
    }

    #[test]
    fn test_escape_single_basic() {
        assert_eq!(escape_single("a'b"), "a''b");
        assert_eq!(escape_single("noquote"), "noquote");
    }

    #[test]
    fn test_normalize_newlines_removes_cr() {
        assert_eq!(normalize_newlines("line1\r\nline2\r"), "line1\nline2");
    }

    #[test]
    fn test_stringify_integer_sequence() {
        let docs = Node::Documents(vec![Node::Document(vec![Node::Array(vec![
            Node::Number(Numeric::Integer(1)),
            Node::Number(Numeric::Integer(2)),
            Node::Number(Numeric::Integer(3)),
        ])])]);

        let mut buf = Buffer::new();
        stringify(&docs, &mut buf).expect("stringify failed");
        assert_eq!(buf.to_string(), "---\n- 1\n- 2\n- 3\n...\n");
    }

    #[test]
    fn test_stringify_mapping_simple() {
        let mapping = Node::Documents(vec![Node::Document(vec![Node::Mapping(vec![(
            Node::from("key"),
            Node::from("value"),
        )])])]);

        let mut buf = Buffer::new();
        stringify(&mapping, &mut buf).expect("stringify failed");
        assert_eq!(buf.to_string(), "---\nkey: value\n...\n");
    }

    #[test]
    fn test_stringify_single_line_literal_document_emits_pipe() {
        let lit = Node::Documents(vec![Node::Document(vec![Node::Str(
            "line1\nline2".to_string(),
            QuoteType::Unquoted,
            BlockStyle::Literal,
        )])]);

        let mut buf = Buffer::new();
        stringify(&lit, &mut buf).expect("stringify failed");
        assert_eq!(buf.to_string(), "--- |\nline1\nline2\n...\n");
    }

    #[test]
    fn test_stringify_set_simple() {
        let set_doc = Node::Documents(vec![Node::Document(vec![Node::Set(vec![
            Node::from("item1"),
            Node::from("item2"),
            Node::from("item3"),
        ])])]);

        let mut buf = Buffer::new();
        stringify(&set_doc, &mut buf).expect("stringify failed");
        assert_eq!(buf.to_string(), "---\n- item1\n- item2\n- item3\n...\n");
    }

    #[test]
    fn test_stringify_set_empty() {
        let set_doc = Node::Documents(vec![Node::Document(vec![Node::Set(vec![])])]);

        let mut buf = Buffer::new();
        stringify(&set_doc, &mut buf).expect("stringify failed");
        assert_eq!(buf.to_string(), "---\n...\n");
    }

    #[test]
    fn test_stringify_set_with_numbers() {
        let set_doc = Node::Documents(vec![Node::Document(vec![Node::Set(vec![
            Node::Number(Numeric::Integer(1)),
            Node::Number(Numeric::Integer(2)),
            Node::Number(Numeric::Integer(3)),
        ])])]);

        let mut buf = Buffer::new();
        stringify(&set_doc, &mut buf).expect("stringify failed");
        assert_eq!(buf.to_string(), "---\n- 1\n- 2\n- 3\n...\n");
    }
}