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
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
//! Zero-copy parsing optimizations for YAML processing
//!
//! This module provides data structures and utilities for minimizing allocations
//! during YAML parsing by using string slices where possible instead of owned strings.

use crate::{Position, Result};
use std::borrow::Cow;

/// A zero-copy string that can either borrow from the input or own its data
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ZeroString<'a> {
    data: Cow<'a, str>,
}

impl<'a> ZeroString<'a> {
    /// Create a borrowed zero-copy string
    pub fn borrowed(s: &'a str) -> Self {
        Self {
            data: Cow::Borrowed(s),
        }
    }

    /// Create an owned zero-copy string
    pub fn owned(s: String) -> Self {
        Self {
            data: Cow::Owned(s),
        }
    }

    /// Get the string content as a &str
    pub fn as_str(&self) -> &str {
        &self.data
    }

    /// Convert to owned String
    pub fn into_owned(self) -> String {
        self.data.into_owned()
    }

    /// Check if this is borrowed data
    pub fn is_borrowed(&self) -> bool {
        matches!(self.data, Cow::Borrowed(_))
    }

    /// Get the length in bytes
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }
}

impl<'a> From<&'a str> for ZeroString<'a> {
    fn from(s: &'a str) -> Self {
        Self::borrowed(s)
    }
}

impl<'a> From<String> for ZeroString<'a> {
    fn from(s: String) -> Self {
        Self::owned(s)
    }
}

impl<'a> AsRef<str> for ZeroString<'a> {
    fn as_ref(&self) -> &str {
        &self.data
    }
}

/// Zero-copy token types that use string slices where possible
#[derive(Debug, Clone, PartialEq)]
pub enum ZeroTokenType<'a> {
    /// Stream start marker
    StreamStart,
    /// Stream end marker
    StreamEnd,
    /// Document start marker (---)
    DocumentStart,
    /// Document end marker (...)
    DocumentEnd,
    /// Block sequence start ([)
    BlockSequenceStart,
    /// Block mapping start ({)
    BlockMappingStart,
    /// Block end marker
    BlockEnd,
    /// Flow sequence start ([)
    FlowSequenceStart,
    /// Flow sequence end (])
    FlowSequenceEnd,
    /// Flow mapping start ({)
    FlowMappingStart,
    /// Flow mapping end (})
    FlowMappingEnd,
    /// Block entry marker (-)
    BlockEntry,
    /// Flow entry separator (,)
    FlowEntry,
    /// Key marker (?)
    Key,
    /// Value separator (:)
    Value,
    /// Scalar value with quote style
    Scalar(ZeroString<'a>, crate::scanner::QuoteStyle),
    /// Literal block scalar (|)
    BlockScalarLiteral(ZeroString<'a>),
    /// Folded block scalar (>)
    BlockScalarFolded(ZeroString<'a>),
    /// Anchor definition (&name)
    Anchor(ZeroString<'a>),
    /// Alias reference (*name)
    Alias(ZeroString<'a>),
    /// Tag (!tag)
    Tag(ZeroString<'a>),
    /// Comment (# comment)
    Comment(ZeroString<'a>),
}

/// Zero-copy token with position information
#[derive(Debug, Clone, PartialEq)]
pub struct ZeroToken<'a> {
    /// The type of token
    pub token_type: ZeroTokenType<'a>,
    /// Starting position in the input
    pub start_position: Position,
    /// Ending position in the input
    pub end_position: Position,
}

impl<'a> ZeroToken<'a> {
    /// Create a new zero-copy token
    pub fn new(
        token_type: ZeroTokenType<'a>,
        start_position: Position,
        end_position: Position,
    ) -> Self {
        Self {
            token_type,
            start_position,
            end_position,
        }
    }

    /// Create a simple token without data
    pub fn simple(token_type: ZeroTokenType<'a>, position: Position) -> Self {
        Self::new(token_type, position, position)
    }

    /// Convert to owned token (for compatibility)
    pub fn into_owned(self) -> crate::scanner::Token {
        use crate::scanner::{Token, TokenType};

        let token_type = match self.token_type {
            ZeroTokenType::StreamStart => TokenType::StreamStart,
            ZeroTokenType::StreamEnd => TokenType::StreamEnd,
            ZeroTokenType::DocumentStart => TokenType::DocumentStart,
            ZeroTokenType::DocumentEnd => TokenType::DocumentEnd,
            ZeroTokenType::BlockSequenceStart => TokenType::BlockSequenceStart,
            ZeroTokenType::BlockMappingStart => TokenType::BlockMappingStart,
            ZeroTokenType::BlockEnd => TokenType::BlockEnd,
            ZeroTokenType::FlowSequenceStart => TokenType::FlowSequenceStart,
            ZeroTokenType::FlowSequenceEnd => TokenType::FlowSequenceEnd,
            ZeroTokenType::FlowMappingStart => TokenType::FlowMappingStart,
            ZeroTokenType::FlowMappingEnd => TokenType::FlowMappingEnd,
            ZeroTokenType::BlockEntry => TokenType::BlockEntry,
            ZeroTokenType::FlowEntry => TokenType::FlowEntry,
            ZeroTokenType::Key => TokenType::Key,
            ZeroTokenType::Value => TokenType::Value,
            ZeroTokenType::Scalar(s, style) => TokenType::Scalar(s.into_owned(), style),
            ZeroTokenType::BlockScalarLiteral(s) => TokenType::BlockScalarLiteral(s.into_owned()),
            ZeroTokenType::BlockScalarFolded(s) => TokenType::BlockScalarFolded(s.into_owned()),
            ZeroTokenType::Anchor(s) => TokenType::Anchor(s.into_owned()),
            ZeroTokenType::Alias(s) => TokenType::Alias(s.into_owned()),
            ZeroTokenType::Tag(s) => TokenType::Tag(s.into_owned()),
            ZeroTokenType::Comment(s) => TokenType::Comment(s.into_owned()),
        };

        Token::new(token_type, self.start_position, self.end_position)
    }
}

/// Memory pool for token allocation to reduce heap allocations
pub struct TokenPool<'a> {
    /// Pool of reusable tokens
    tokens: Vec<ZeroToken<'a>>,
    /// Current index in the pool
    index: usize,
}

impl<'a> TokenPool<'a> {
    /// Create a new token pool with initial capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            tokens: Vec::with_capacity(capacity),
            index: 0,
        }
    }

    /// Get a token from the pool or create a new one
    pub fn get_token(&mut self) -> &mut ZeroToken<'a> {
        if self.index >= self.tokens.len() {
            // Need to allocate a new token
            self.tokens.push(ZeroToken::simple(
                ZeroTokenType::StreamStart,
                Position::start(),
            ));
        }

        let token = &mut self.tokens[self.index];
        self.index += 1;
        token
    }

    /// Reset the pool for reuse
    pub fn reset(&mut self) {
        self.index = 0;
    }

    /// Get the number of tokens currently allocated
    pub fn allocated_count(&self) -> usize {
        self.tokens.len()
    }

    /// Get the number of tokens currently in use
    pub fn used_count(&self) -> usize {
        self.index
    }
}

/// Zero-copy string scanner that operates on slices
pub struct ZeroScanner<'a> {
    /// Reference to the input string
    input: &'a str,
    /// Current position in the input
    pub position: Position,
    /// Current character index
    char_index: usize,
    /// Cached character indices for faster access
    char_indices: Vec<(usize, char)>,
    /// Token pool for allocation optimization
    token_pool: TokenPool<'a>,
}

impl<'a> ZeroScanner<'a> {
    /// Create a new zero-copy scanner
    pub fn new(input: &'a str) -> Self {
        let char_indices: Vec<(usize, char)> = input.char_indices().collect();

        Self {
            input,
            position: Position::start(),
            char_index: 0,
            char_indices,
            token_pool: TokenPool::with_capacity(128), // Start with reasonable capacity
        }
    }

    /// Get the current character
    pub fn current_char(&self) -> Option<char> {
        self.char_indices.get(self.char_index).map(|(_, ch)| *ch)
    }

    /// Advance to the next character
    pub fn advance(&mut self) -> Option<char> {
        if let Some((_byte_index, ch)) = self.char_indices.get(self.char_index) {
            self.position = self.position.advance(*ch);
            self.char_index += 1;
            self.char_indices.get(self.char_index).map(|(_, ch)| *ch)
        } else {
            None
        }
    }

    /// Peek at a character at the given offset
    pub fn peek_char(&self, offset: isize) -> Option<char> {
        if offset >= 0 {
            let index = self.char_index + offset as usize;
            self.char_indices.get(index).map(|(_, ch)| *ch)
        } else {
            let offset_abs = (-offset) as usize;
            if self.char_index >= offset_abs {
                let index = self.char_index - offset_abs;
                self.char_indices.get(index).map(|(_, ch)| *ch)
            } else {
                None
            }
        }
    }

    /// Get a slice of the input from start position to current position
    pub fn slice_from(&self, start_position: Position) -> Result<&'a str> {
        let start_byte = start_position.index;
        let end_byte = self.position.index;

        if start_byte <= end_byte && end_byte <= self.input.len() {
            Ok(&self.input[start_byte..end_byte])
        } else {
            Err(crate::Error::parse(
                self.position,
                "Invalid slice bounds".to_string(),
            ))
        }
    }

    /// Get a slice between two positions
    pub fn slice_between(&self, start: Position, end: Position) -> Result<&'a str> {
        let start_byte = start.index;
        let end_byte = end.index;

        if start_byte <= end_byte && end_byte <= self.input.len() {
            Ok(&self.input[start_byte..end_byte])
        } else {
            Err(crate::Error::parse(
                self.position,
                "Invalid slice bounds".to_string(),
            ))
        }
    }

    /// Reset the scanner to the beginning
    pub fn reset(&mut self) {
        self.position = Position::start();
        self.char_index = 0;
        self.token_pool.reset();
    }

    /// Get scanner statistics for performance monitoring
    pub fn stats(&self) -> ScannerStats {
        ScannerStats {
            input_length: self.input.len(),
            chars_processed: self.char_index,
            tokens_allocated: self.token_pool.allocated_count(),
            tokens_used: self.token_pool.used_count(),
            position: self.position,
        }
    }

    /// Scan a plain scalar using zero-copy slicing
    pub fn scan_plain_scalar_zero_copy(&mut self) -> Result<ZeroToken<'a>> {
        let start_pos = self.position;

        // Find the end of the scalar without allocating
        while let Some(ch) = self.current_char() {
            // Stop at structural characters (same logic as regular scanner)
            match ch {
                '\n' | '\r' => break,
                ':' if self.peek_char(1).map_or(true, |c| c.is_whitespace()) => break,
                '#' if self.char_index == 0
                    || self.peek_char(-1).map_or(false, |c| c.is_whitespace()) =>
                {
                    break;
                }
                ',' | '[' | ']' | '{' | '}' => break,
                _ => {
                    self.advance();
                }
            }
        }

        // Get the slice without allocation
        let slice = self.slice_from(start_pos)?;
        let trimmed_slice = slice.trim_end();

        // Use borrowed string if possible
        let zero_string = if trimmed_slice.len() == slice.len() {
            // No trimming needed, can use borrowed slice directly
            ZeroString::borrowed(trimmed_slice)
        } else {
            // Need to allocate for trimmed version
            ZeroString::owned(trimmed_slice.to_string())
        };

        Ok(ZeroToken::new(
            ZeroTokenType::Scalar(zero_string, crate::scanner::QuoteStyle::Plain),
            start_pos,
            self.position,
        ))
    }

    /// Scan a simple identifier using zero-copy slicing (for anchors/aliases)
    pub fn scan_identifier_zero_copy(&mut self) -> Result<ZeroString<'a>> {
        let start_pos = self.position;

        // Scan identifier characters
        while let Some(ch) = self.current_char() {
            if ch.is_alphanumeric() || ch == '_' || ch == '-' {
                self.advance();
            } else {
                break;
            }
        }

        let slice = self.slice_from(start_pos)?;
        Ok(ZeroString::borrowed(slice))
    }

    /// Skip whitespace efficiently
    pub fn skip_whitespace(&mut self) {
        while let Some(ch) = self.current_char() {
            if ch == ' ' || ch == '\t' {
                self.advance();
            } else {
                break;
            }
        }
    }
}

/// Statistics about scanner performance
#[derive(Debug, Clone)]
pub struct ScannerStats {
    /// Total input length in bytes
    pub input_length: usize,
    /// Number of characters processed
    pub chars_processed: usize,
    /// Number of tokens allocated in the pool
    pub tokens_allocated: usize,
    /// Number of tokens currently used
    pub tokens_used: usize,
    /// Current position
    pub position: Position,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_zero_string_borrowed() {
        let s = "hello world";
        let zs = ZeroString::borrowed(s);

        assert!(zs.is_borrowed());
        assert_eq!(zs.as_str(), "hello world");
        assert_eq!(zs.len(), 11);
        assert!(!zs.is_empty());
    }

    #[test]
    fn test_zero_string_owned() {
        let s = String::from("hello world");
        let zs = ZeroString::owned(s);

        assert!(!zs.is_borrowed());
        assert_eq!(zs.as_str(), "hello world");
        assert_eq!(zs.len(), 11);
    }

    #[test]
    fn test_zero_scanner_basic() {
        let input = "hello: world";
        let mut scanner = ZeroScanner::new(input);

        assert_eq!(scanner.current_char(), Some('h'));
        assert_eq!(scanner.advance(), Some('e'));
        assert_eq!(scanner.current_char(), Some('e'));

        // Test peeking
        assert_eq!(scanner.peek_char(1), Some('l'));
        assert_eq!(scanner.peek_char(-1), Some('h'));
    }

    #[test]
    fn test_zero_scanner_slicing() {
        let input = "hello: world";
        let mut scanner = ZeroScanner::new(input);

        let start = scanner.position;

        // Advance past "hello"
        for _ in 0..5 {
            scanner.advance();
        }

        let slice = scanner.slice_from(start).unwrap();
        assert_eq!(slice, "hello");
    }

    #[test]
    fn test_token_pool() {
        let mut pool = TokenPool::with_capacity(2);

        assert_eq!(pool.allocated_count(), 0);
        assert_eq!(pool.used_count(), 0);

        let _token1 = pool.get_token();
        assert_eq!(pool.allocated_count(), 1);
        assert_eq!(pool.used_count(), 1);

        let _token2 = pool.get_token();
        assert_eq!(pool.allocated_count(), 2);
        assert_eq!(pool.used_count(), 2);

        pool.reset();
        assert_eq!(pool.allocated_count(), 2); // Still allocated
        assert_eq!(pool.used_count(), 0); // But not in use
    }

    #[test]
    fn test_zero_copy_scalar_scanning() {
        let input = "hello world: test";
        let mut scanner = ZeroScanner::new(input);

        let token = scanner.scan_plain_scalar_zero_copy().unwrap();

        if let ZeroTokenType::Scalar(value, _) = token.token_type {
            assert_eq!(value.as_str(), "hello world");
            assert!(value.is_borrowed()); // Should be zero-copy
        } else {
            panic!("Expected scalar token");
        }
    }

    #[test]
    fn test_zero_copy_identifier_scanning() {
        let input = "my_anchor_123 ";
        let mut scanner = ZeroScanner::new(input);

        let identifier = scanner.scan_identifier_zero_copy().unwrap();
        assert_eq!(identifier.as_str(), "my_anchor_123");
        assert!(identifier.is_borrowed()); // Should be zero-copy
    }

    #[test]
    fn test_zero_copy_trimming() {
        let input = "hello   \n";
        let mut scanner = ZeroScanner::new(input);

        let token = scanner.scan_plain_scalar_zero_copy().unwrap();

        if let ZeroTokenType::Scalar(value, _) = token.token_type {
            assert_eq!(value.as_str(), "hello");
            // Should be owned because trimming was needed
            assert!(!value.is_borrowed());
        } else {
            panic!("Expected scalar token");
        }
    }
}