rust-yaml 0.0.5

A fast, safe YAML 1.2 library for Rust
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
//! Scalar scanning functionality for YAML scanner

use super::{QuoteStyle, Token, TokenType};
use crate::{Error, Position, Result};

/// Trait for scanning scalar values
pub trait ScalarScanner {
    /// Scan a plain scalar (unquoted string)
    fn scan_plain_scalar(&mut self) -> Result<Token>;

    /// Scan a quoted string (single or double quotes)
    fn scan_quoted_string(&mut self, quote_char: char) -> Result<Token>;

    /// Scan a number (integer or float)
    fn scan_number(&mut self) -> Result<Token>;

    /// Scan a literal block scalar (|)
    fn scan_literal_block_scalar(&mut self) -> Result<Token>;

    /// Scan a folded block scalar (>)
    fn scan_folded_block_scalar(&mut self) -> Result<Token>;

    /// Scan block scalar header for chomping and indentation
    fn scan_block_scalar_header(&mut self) -> Result<(bool, Option<usize>)>;

    /// Helper: get current position
    fn current_position(&self) -> Position;

    /// Helper: peek at current character
    fn current_char(&self) -> Option<char>;

    /// Helper: advance to next character
    fn advance_char(&mut self) -> Option<char>;

    /// Helper: peek at next character
    fn peek_char(&self, offset: usize) -> Option<char>;

    /// Helper: check if at line start
    fn at_line_start(&self) -> bool;
}

/// Helper functions for scalar processing
pub(super) fn is_plain_scalar_char(ch: char) -> bool {
    !matches!(
        ch,
        ':' | ','
            | '['
            | ']'
            | '{'
            | '}'
            | '#'
            | '&'
            | '*'
            | '!'
            | '|'
            | '>'
            | '\''
            | '"'
            | '%'
            | '@'
            | '`'
    )
}

pub(super) fn process_escape_sequence(ch: char) -> Result<String> {
    match ch {
        'n' => Ok("\n".to_string()),
        'r' => Ok("\r".to_string()),
        't' => Ok("\t".to_string()),
        '\\' => Ok("\\".to_string()),
        '"' => Ok("\"".to_string()),
        '\'' => Ok("'".to_string()),
        '0' => Ok("\0".to_string()),
        'a' => Ok("\x07".to_string()), // Bell
        'b' => Ok("\x08".to_string()), // Backspace
        'f' => Ok("\x0C".to_string()), // Form feed
        'v' => Ok("\x0B".to_string()), // Vertical tab
        'e' => Ok("\x1B".to_string()), // Escape
        ' ' => Ok(" ".to_string()),
        'N' => Ok("\u{85}".to_string()),   // Next line (NEL)
        '_' => Ok("\u{A0}".to_string()),   // Non-breaking space
        'L' => Ok("\u{2028}".to_string()), // Line separator
        'P' => Ok("\u{2029}".to_string()), // Paragraph separator
        _ => Err(Error::scan(
            Position::new(),
            format!("Invalid escape sequence: \\{}", ch),
        )),
    }
}

/// Implementation of ScalarScanner for BasicScanner
impl ScalarScanner for super::BasicScanner {
    fn scan_plain_scalar(&mut self) -> Result<Token> {
        let start_pos = self.position;
        let mut value = String::new();

        while let Some(ch) = self.current_char {
            // Stop at structural characters in block context
            if self.flow_level == 0 {
                match ch {
                    '\n' | '\r' => break,
                    ':' if self.peek_char(1).map_or(true, |c| c.is_whitespace()) => break,
                    '#' if value.is_empty()
                        || self.peek_char(-1).map_or(false, |c| c.is_whitespace()) =>
                    {
                        break;
                    }
                    _ => {}
                }
            } else {
                // In flow context, stop at flow indicators
                match ch {
                    ',' | '[' | ']' | '{' | '}' => break,
                    ':' if self
                        .peek_char(1)
                        .map_or(true, |c| c.is_whitespace() || "]}".contains(c)) =>
                    {
                        break;
                    }
                    '#' if value.is_empty()
                        || self.peek_char(-1).map_or(false, |c| c.is_whitespace()) =>
                    {
                        break;
                    }
                    _ => {}
                }
            }

            value.push(ch);
            self.advance();
        }

        // Check string length limit
        self.resource_tracker
            .check_string_length(&self.limits, value.len())?;

        // Trim trailing whitespace from plain scalars
        let value = value.trim_end().to_string();
        let normalized_value = Self::normalize_scalar(value);

        Ok(Token::new(
            TokenType::Scalar(normalized_value, QuoteStyle::Plain),
            start_pos,
            self.position,
        ))
    }

    fn scan_quoted_string(&mut self, quote_char: char) -> Result<Token> {
        let start_pos = self.position;
        let mut value = String::new();

        // Skip opening quote
        self.advance();

        while let Some(ch) = self.current_char {
            if ch == quote_char {
                // End quote found
                self.advance();
                break;
            } else if ch == '\\' && quote_char == '"' {
                // Handle escape sequences in double quotes
                self.advance();
                if let Some(escaped_char) = self.current_char {
                    match escaped_char {
                        'n' => value.push('\n'),
                        'r' => value.push('\r'),
                        't' => value.push('\t'),
                        '\\' => value.push('\\'),
                        '"' => value.push('"'),
                        '\'' => value.push('\''),
                        '0' => value.push('\0'),
                        'a' => value.push('\x07'), // Bell
                        'b' => value.push('\x08'), // Backspace
                        'f' => value.push('\x0C'), // Form feed
                        'v' => value.push('\x0B'), // Vertical tab
                        'e' => value.push('\x1B'), // Escape
                        ' ' => value.push(' '),
                        'N' => value.push('\u{85}'),   // Next line (NEL)
                        '_' => value.push('\u{A0}'),   // Non-breaking space
                        'L' => value.push('\u{2028}'), // Line separator
                        'P' => value.push('\u{2029}'), // Paragraph separator
                        _ => {
                            // Invalid escape sequence
                            return Err(Error::scan(
                                self.position,
                                format!("Invalid escape sequence: \\{}", escaped_char),
                            ));
                        }
                    }
                    self.advance();
                } else {
                    return Err(Error::scan(
                        self.position,
                        "Unterminated escape sequence".to_string(),
                    ));
                }
            } else {
                value.push(ch);
                self.advance();
            }
        }

        // Check string length limit
        self.resource_tracker
            .check_string_length(&self.limits, value.len())?;

        let quote_style = match quote_char {
            '\'' => QuoteStyle::Single,
            '"' => QuoteStyle::Double,
            _ => QuoteStyle::Plain,
        };

        Ok(Token::new(
            TokenType::Scalar(value, quote_style),
            start_pos,
            self.position,
        ))
    }

    fn scan_number(&mut self) -> Result<Token> {
        let start_pos = self.position;
        let mut value = String::new();

        // Handle negative numbers
        if self.current_char == Some('-') {
            value.push('-');
            self.advance();
        }

        // Scan digits
        while let Some(ch) = self.current_char {
            if ch.is_ascii_digit() {
                value.push(ch);
                self.advance();
            } else if ch == '.' {
                value.push(ch);
                self.advance();
                // Scan fractional part
                while let Some(ch) = self.current_char {
                    if ch.is_ascii_digit() {
                        value.push(ch);
                        self.advance();
                    } else {
                        break;
                    }
                }
                break;
            } else {
                break;
            }
        }

        Ok(Token::new(
            TokenType::Scalar(value, QuoteStyle::Plain),
            start_pos,
            self.position,
        ))
    }

    fn scan_literal_block_scalar(&mut self) -> Result<Token> {
        let start_pos = self.position;

        // Skip the '|' character
        self.advance();

        // Scan block scalar header for chomping and indentation
        let (keep_chomping, explicit_indent) = self.scan_block_scalar_header()?;

        // Find the base indentation level
        let mut base_indent = None;
        let mut lines = Vec::new();
        let mut current_line = String::new();

        // Skip to end of header line
        while let Some(ch) = self.current_char {
            if ch == '\n' || ch == '\r' {
                self.advance();
                break;
            }
            self.advance();
        }

        // Collect lines
        while let Some(ch) = self.current_char {
            if ch == '\n' || ch == '\r' {
                lines.push(current_line.clone());
                current_line.clear();
                self.advance();

                // Check if next line has content to determine if we should continue
                let mut temp_indent = 0usize;
                let mut has_content = false;

                while let Some(next_ch) = self.peek_char(temp_indent as isize) {
                    if next_ch == ' ' || next_ch == '\t' {
                        temp_indent += 1;
                    } else if next_ch == '\n' || next_ch == '\r' {
                        // Empty line, continue collecting
                        break;
                    } else {
                        has_content = true;
                        break;
                    }
                }

                if !has_content {
                    // No more content lines
                    break;
                }

                // Set base indentation from first content line
                if base_indent.is_none() && has_content {
                    base_indent = Some(explicit_indent.unwrap_or(temp_indent));
                }
            } else {
                current_line.push(ch);
                self.advance();
            }
        }

        // Add final line if not empty
        if !current_line.is_empty() {
            lines.push(current_line);
        }

        // Join lines with newlines (literal style preserves line breaks)
        let mut value = lines.join("\n");

        // Apply chomping rules
        if !keep_chomping {
            value = value.trim_end_matches('\n').to_string();
        }

        // Check string length limit
        self.resource_tracker
            .check_string_length(&self.limits, value.len())?;

        Ok(Token::new(
            TokenType::BlockScalarLiteral(value),
            start_pos,
            self.position,
        ))
    }

    fn scan_folded_block_scalar(&mut self) -> Result<Token> {
        let start_pos = self.position;

        // Skip the '>' character
        self.advance();

        // Scan block scalar header for chomping and indentation
        let (keep_chomping, explicit_indent) = self.scan_block_scalar_header()?;

        // Similar to literal but fold newlines
        let mut base_indent = None;
        let mut lines = Vec::new();
        let mut current_line = String::new();

        // Skip to end of header line
        while let Some(ch) = self.current_char {
            if ch == '\n' || ch == '\r' {
                self.advance();
                break;
            }
            self.advance();
        }

        // Collect lines
        while let Some(ch) = self.current_char {
            if ch == '\n' || ch == '\r' {
                lines.push(current_line.clone());
                current_line.clear();
                self.advance();

                // Check if next line has content
                let mut temp_indent = 0usize;
                let mut has_content = false;

                while let Some(next_ch) = self.peek_char(temp_indent as isize) {
                    if next_ch == ' ' || next_ch == '\t' {
                        temp_indent += 1;
                    } else if next_ch == '\n' || next_ch == '\r' {
                        break;
                    } else {
                        has_content = true;
                        break;
                    }
                }

                if !has_content {
                    break;
                }

                if base_indent.is_none() && has_content {
                    base_indent = Some(explicit_indent.unwrap_or(temp_indent));
                }
            } else {
                current_line.push(ch);
                self.advance();
            }
        }

        if !current_line.is_empty() {
            lines.push(current_line);
        }

        // Fold lines: join non-empty lines with spaces, preserve empty lines
        let mut value = String::new();
        let mut prev_was_empty = false;

        for (i, line) in lines.iter().enumerate() {
            if line.trim().is_empty() {
                if !prev_was_empty && i > 0 {
                    value.push('\n');
                }
                prev_was_empty = true;
            } else {
                if i > 0 && !prev_was_empty {
                    value.push(' ');
                } else if prev_was_empty && i > 0 {
                    value.push('\n');
                }
                value.push_str(line.trim());
                prev_was_empty = false;
            }
        }

        // Apply chomping rules
        if !keep_chomping {
            value = value.trim_end_matches('\n').to_string();
        }

        // Check string length limit
        self.resource_tracker
            .check_string_length(&self.limits, value.len())?;

        Ok(Token::new(
            TokenType::BlockScalarFolded(value),
            start_pos,
            self.position,
        ))
    }

    fn scan_block_scalar_header(&mut self) -> Result<(bool, Option<usize>)> {
        let mut keep_chomping = true;
        let mut explicit_indent = None;

        // Skip whitespace after '|' or '>'
        while let Some(ch) = self.current_char {
            if ch == ' ' || ch == '\t' {
                self.advance();
            } else {
                break;
            }
        }

        // Check for explicit indentation indicator (digit)
        if let Some(ch) = self.current_char {
            if ch.is_ascii_digit() {
                explicit_indent = Some(ch.to_digit(10).unwrap() as usize);
                self.advance();
            }
        }

        // Check for chomping indicator
        if let Some(ch) = self.current_char {
            match ch {
                '-' => {
                    keep_chomping = false; // Strip final newlines
                    self.advance();
                }
                '+' => {
                    keep_chomping = true; // Keep final newlines
                    self.advance();
                }
                _ => {}
            }
        }

        Ok((keep_chomping, explicit_indent))
    }

    // Helper trait methods
    fn current_position(&self) -> Position {
        self.position
    }

    fn current_char(&self) -> Option<char> {
        self.current_char
    }

    fn advance_char(&mut self) -> Option<char> {
        self.advance()
    }

    fn peek_char(&self, offset: usize) -> Option<char> {
        self.peek_char(offset as isize)
    }

    fn at_line_start(&self) -> bool {
        self.position.column == 1
    }
}