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
//! Comment-preserving YAML composer

use crate::{
    parser::EventType, BasicParser, BasicScanner, CommentedValue, Comments, Error, Limits, Parser,
    Position, ResourceTracker, Result, Scanner, Style, TokenType, Value,
};
use indexmap::IndexMap;
use std::collections::HashMap;

/// A composer that preserves comments during parsing
#[derive(Debug)]
pub struct CommentPreservingComposer {
    parser: BasicParser,
    scanner: BasicScanner,
    limits: Limits,
    resource_tracker: ResourceTracker,
    anchors: HashMap<String, CommentedValue>,
    current_depth: usize,
    alias_expansion_stack: Vec<String>,
    /// Map of positions to comments (position -> comment text)
    comment_map: HashMap<Position, String>,
    /// Stack of pending comments that might belong to the next value
    pending_comments: Vec<String>,
}

impl CommentPreservingComposer {
    /// Create a new comment-preserving composer
    pub fn new(input: String) -> Self {
        Self::with_limits(input, Limits::default())
    }

    /// Create a new comment-preserving composer with limits
    pub fn with_limits(input: String, limits: Limits) -> Self {
        // Use comment-preserving scanner
        let scanner = BasicScanner::new_with_comments_and_limits(input.clone(), limits.clone());
        let parser = BasicParser::new_eager_with_limits(input, limits.clone());

        Self {
            parser,
            scanner,
            limits,
            resource_tracker: ResourceTracker::new(),
            anchors: HashMap::new(),
            current_depth: 0,
            alias_expansion_stack: Vec::new(),
            comment_map: HashMap::new(),
            pending_comments: Vec::new(),
        }
    }

    /// Extract comments from the scanner and build a position map
    fn extract_comments(&mut self) -> Result<()> {
        // Scan all tokens to extract comments
        while self.scanner.check_token() {
            if let Some(token) = self.scanner.get_token()? {
                if let TokenType::Comment(comment_text) = token.token_type {
                    // Store comment associated with its position
                    self.comment_map
                        .insert(token.start_position, comment_text.trim().to_string());
                }
            } else {
                break;
            }
        }
        Ok(())
    }

    /// Get comments that should be associated with a value at the given position
    fn get_comments_for_position(&self, position: Position) -> Comments {
        let mut comments = Comments::new();

        // Enhanced comment correlation algorithm
        for (comment_pos, comment_text) in &self.comment_map {
            let line_diff = comment_pos.line as i32 - position.line as i32;

            // Comments on the same line after the value (trailing)
            if comment_pos.line == position.line && comment_pos.column > position.column {
                comments.set_trailing(comment_text.clone());
            }
            // Comments on lines before the value (leading)
            else if (-3..0).contains(&line_diff) {
                // Allow up to 3 lines before as leading comments
                comments.add_leading(comment_text.clone());
            }
            // Comments on the same line before the value (also leading)
            else if comment_pos.line == position.line && comment_pos.column < position.column {
                comments.add_leading(comment_text.clone());
            }
            // Comments immediately after (next line) could be inner comments
            else if line_diff == 1 {
                comments.add_inner(comment_text.clone());
            }
        }

        comments
    }

    /// Compose a single document with comment preservation
    pub fn compose_document(&mut self) -> Result<Option<CommentedValue>> {
        // First, extract all comments from the scanner
        self.extract_comments()?;

        // Reset state
        self.current_depth = 0;
        self.anchors.clear();
        self.alias_expansion_stack.clear();
        self.resource_tracker.reset();

        // Compose the document
        self.compose_node()
    }

    /// Compose a single node (value) with comments
    fn compose_node(&mut self) -> Result<Option<CommentedValue>> {
        // Check resource limits
        self.resource_tracker.add_complexity(&self.limits, 1)?;
        self.current_depth += 1;

        if self.current_depth > self.limits.max_depth {
            return Err(Error::limit_exceeded(format!(
                "Maximum nesting depth {} exceeded",
                self.limits.max_depth
            )));
        }

        // Get the next event from the parser
        let event = match self.parser.get_event()? {
            Some(event) => event,
            None => {
                self.current_depth -= 1;
                return Ok(None);
            }
        };

        let position = event.position;
        let result = match event.event_type {
            EventType::Scalar { value, anchor, .. } => self.compose_scalar(value, anchor, position),
            EventType::SequenceStart { anchor, .. } => self.compose_sequence(anchor, position),
            EventType::MappingStart { anchor, .. } => self.compose_mapping(anchor, position),
            EventType::Alias { anchor } => self.compose_alias(anchor, position),
            EventType::StreamStart | EventType::StreamEnd => {
                // Skip structural events and try next
                self.compose_node()
            }
            EventType::DocumentStart { .. } | EventType::DocumentEnd { .. } => {
                // Skip document markers and try next
                self.compose_node()
            }
            EventType::SequenceEnd | EventType::MappingEnd => {
                // These should be handled by their respective start handlers
                // If we encounter them here, it means unbalanced structure
                Ok(None)
            }
        };

        self.current_depth -= 1;
        result
    }

    /// Compose a scalar value
    fn compose_scalar(
        &mut self,
        value: String,
        anchor: Option<String>,
        position: Position,
    ) -> Result<Option<CommentedValue>> {
        // Resolve the scalar type properly
        let resolved_value = self.resolve_scalar_type(value);

        let commented_value = CommentedValue {
            value: resolved_value,
            comments: self.get_comments_for_position(position),
            style: Style::default(),
        };

        // Store anchor if present
        if let Some(anchor_name) = anchor {
            self.anchors.insert(anchor_name, commented_value.clone());
        }

        Ok(Some(commented_value))
    }

    /// Resolve scalar type from string value
    fn resolve_scalar_type(&self, value: String) -> Value {
        // Empty string
        if value.is_empty() {
            return Value::String(value);
        }

        // Try integer parsing
        if let Ok(int_value) = value.parse::<i64>() {
            return Value::Int(int_value);
        }

        // Try float parsing
        if let Ok(float_value) = value.parse::<f64>() {
            return Value::Float(float_value);
        }

        // Try boolean parsing
        match value.to_lowercase().as_str() {
            "true" | "yes" | "on" => return Value::Bool(true),
            "false" | "no" | "off" => return Value::Bool(false),
            "null" | "~" => return Value::Null,
            _ => {}
        }

        // Default to string
        Value::String(value)
    }

    /// Compose a sequence
    fn compose_sequence(
        &mut self,
        anchor: Option<String>,
        position: Position,
    ) -> Result<Option<CommentedValue>> {
        let mut sequence = Vec::new();
        let mut inner_comments = Vec::new();

        // Collect sequence items
        while let Some(item_event) = self.parser.peek_event()? {
            if matches!(item_event.event_type, EventType::SequenceEnd) {
                self.parser.get_event()?; // consume SequenceEnd
                break;
            }

            if let Some(item) = self.compose_node()? {
                self.collect_item_comments(&item, &mut inner_comments);
                sequence.push(item.value);
            }
        }

        let mut comments = self.get_comments_for_position(position);
        comments.inner = inner_comments;

        let commented_value = CommentedValue {
            value: Value::Sequence(sequence),
            comments,
            style: Style::default(),
        };

        // Store anchor if present
        if let Some(anchor_name) = anchor {
            self.anchors.insert(anchor_name, commented_value.clone());
        }

        Ok(Some(commented_value))
    }

    /// Compose a mapping
    fn compose_mapping(
        &mut self,
        anchor: Option<String>,
        position: Position,
    ) -> Result<Option<CommentedValue>> {
        let mut mapping = IndexMap::new();
        let mut inner_comments = Vec::new();

        // Collect mapping items
        while let Some(event) = self.parser.peek_event()? {
            if matches!(event.event_type, EventType::MappingEnd) {
                self.parser.get_event()?; // consume MappingEnd
                break;
            }

            // Get key
            let (key, key_comments) = match self.compose_node()? {
                Some(key_commented) => (key_commented.value, key_commented.comments),
                None => break,
            };

            // Get value
            let (value, value_comments) = match self.compose_node()? {
                Some(value_commented) => (value_commented.value, value_commented.comments),
                None => (Value::Null, Comments::new()),
            };

            // Collect comments from key-value pairs
            self.collect_comments(&key_comments, &mut inner_comments);
            self.collect_comments(&value_comments, &mut inner_comments);

            // Handle merge keys
            if let Value::String(key_str) = &key {
                if key_str == "<<" {
                    self.process_merge_key(&mut mapping, &value)?;
                    continue;
                }
            }

            mapping.insert(key, value);
        }

        let mut comments = self.get_comments_for_position(position);
        comments.inner.extend(inner_comments);

        let commented_value = CommentedValue {
            value: Value::Mapping(mapping),
            comments,
            style: Style::default(),
        };

        // Store anchor if present
        if let Some(anchor_name) = anchor {
            self.anchors.insert(anchor_name, commented_value.clone());
        }

        Ok(Some(commented_value))
    }

    /// Compose an alias reference
    fn compose_alias(
        &mut self,
        anchor: String,
        position: Position,
    ) -> Result<Option<CommentedValue>> {
        // Prevent cyclic references
        if self.alias_expansion_stack.contains(&anchor) {
            return Err(Error::parse(
                position,
                format!("Cyclic alias reference detected: '{}'", anchor),
            ));
        }

        self.alias_expansion_stack.push(anchor.clone());

        let result = match self.anchors.get(&anchor) {
            Some(value) => Ok(Some(value.clone())),
            None => Err(Error::parse(
                position,
                format!("Unknown anchor '{}'", anchor),
            )),
        };

        self.alias_expansion_stack.pop();
        result
    }

    /// Collect comments from a commented value's comments into inner comments
    fn collect_item_comments(&self, item: &CommentedValue, inner_comments: &mut Vec<String>) {
        if item.has_comments() {
            for leading in &item.comments.leading {
                inner_comments.push(leading.clone());
            }
            if let Some(ref trailing) = item.comments.trailing {
                inner_comments.push(trailing.clone());
            }
        }
    }

    /// Collect comments from a Comments struct into inner comments
    fn collect_comments(&self, comments: &Comments, inner_comments: &mut Vec<String>) {
        if !comments.leading.is_empty() || comments.trailing.is_some() {
            for leading in &comments.leading {
                inner_comments.push(leading.clone());
            }
            if let Some(ref trailing) = comments.trailing {
                inner_comments.push(trailing.clone());
            }
        }
    }

    /// Process a merge key by merging values into the current mapping
    fn process_merge_key(
        &self,
        mapping: &mut IndexMap<Value, Value>,
        merge_value: &Value,
    ) -> Result<()> {
        match merge_value {
            Value::Mapping(source_map) => {
                for (key, value) in source_map {
                    mapping.entry(key.clone()).or_insert_with(|| value.clone());
                }
            }
            Value::Sequence(sources) => {
                for source in sources {
                    if let Value::Mapping(source_map) = source {
                        for (key, value) in source_map {
                            mapping.entry(key.clone()).or_insert_with(|| value.clone());
                        }
                    }
                }
            }
            _ => {
                // Invalid merge value - ignore
            }
        }
        Ok(())
    }
}

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

    #[test]
    fn test_comment_preservation() {
        let yaml = r#"
# Leading comment
key: value  # Trailing comment
# Another comment
nested:
  # Nested comment
  item: data
"#;

        let mut composer = CommentPreservingComposer::new(yaml.to_string());
        let result = composer.compose_document().unwrap();

        assert!(result.is_some());
        let commented_value = result.unwrap();

        // Should have preserved some comments
        println!("Preserved comments: {:?}", commented_value.comments);
    }
}