yami 0.1.0

A lightweight, zero-copy, minimal-allocation YAML parser in 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
//! Abstract Syntax Tree (AST) representations for parsed YAML data.
//!
//! The AST is designed for zero-copy borrowing from the source string.
//! String scalars, booleans, and numeric representations are retained as
//! slices `&'a str` into the original input buffer, avoiding heap allocations.

use crate::error::{ErrorKind, Position, Result, YamlError};
use std::fmt;
use std::ops::Index;

// ==============================================================================
// AST Definitions
// ==============================================================================

/// A key-value association within a YAML mapping.
///
/// In YAML, keys are commonly string scalars, but can technically be any YAML
/// node. Both key and value share the lifetime `'a` of the source text.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Entry<'a> {
    /// The entry's key node.
    pub key: Yaml<'a>,
    /// The entry's associated value node.
    pub value: Yaml<'a>,
}

impl<'a> Entry<'a> {
    /// Creates a new mapping entry from a key and value pair.
    #[inline]
    pub const fn new(key: Yaml<'a>, value: Yaml<'a>) -> Self {
        Self { key, value }
    }

    /// Borrows the key node.
    #[inline]
    pub const fn key(&self) -> &Yaml<'a> {
        &self.key
    }

    /// Borrows the value node.
    #[inline]
    pub const fn value(&self) -> &Yaml<'a> {
        &self.value
    }

    /// Consumes the entry and returns the underlying key and value.
    #[inline]
    pub fn into_parts(self) -> (Yaml<'a>, Yaml<'a>) {
        (self.key, self.value)
    }
}

/// Represents any YAML node, borrowing string slices directly from the input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Yaml<'a> {
    /// A raw scalar value (unquoted text, quoted string, boolean literal, or number).
    Scalar(&'a str),

    /// A sequence of YAML nodes (block style `- item` or flow style `[a, b]`).
    Sequence(Vec<Yaml<'a>>),

    /// An ordered collection of key-value pairs (block style `k: v` or flow style `{k: v}`).
    Mapping(Vec<Entry<'a>>),
}

// ==============================================================================
// AST Inspection and Navigation Methods
// ==============================================================================

impl<'a> Yaml<'a> {
    /// Returns `true` if this node is a scalar.
    #[inline]
    pub const fn is_scalar(&self) -> bool {
        matches!(self, Self::Scalar(_))
    }

    /// Returns `true` if this node is a sequence.
    #[inline]
    pub const fn is_sequence(&self) -> bool {
        matches!(self, Self::Sequence(_))
    }

    /// Returns `true` if this node is a mapping.
    #[inline]
    pub const fn is_mapping(&self) -> bool {
        matches!(self, Self::Mapping(_))
    }

    /// Returns the borrowed scalar slice if this node is a `Yaml::Scalar`.
    #[inline]
    pub const fn as_scalar(&self) -> Option<&'a str> {
        match self {
            Self::Scalar(s) => Some(*s),
            _ => None,
        }
    }

    /// Alias for `as_scalar()` for ergonomic string access.
    #[inline]
    pub const fn as_str(&self) -> Option<&'a str> {
        self.as_scalar()
    }

    /// Returns a slice of sequence elements if this node is a `Yaml::Sequence`.
    #[inline]
    pub fn as_sequence(&self) -> Option<&[Yaml<'a>]> {
        match self {
            Self::Sequence(vec) => Some(vec.as_slice()),
            _ => None,
        }
    }

    /// Returns a slice of mapping entries if this node is a `Yaml::Mapping`.
    #[inline]
    pub fn as_mapping(&self) -> Option<&[Entry<'a>]> {
        match self {
            Self::Mapping(entries) => Some(entries.as_slice()),
            _ => None,
        }
    }

    /// Looks up a value in a mapping by its scalar key string.
    ///
    /// Performs a linear scan over the mapping entries. Returns `None` if the node
    /// is not a mapping or if no entry has a matching scalar key.
    pub fn get(&self, key: &str) -> Option<&Yaml<'a>> {
        match self {
            Self::Mapping(entries) => entries.iter().find_map(|entry| match &entry.key {
                Self::Scalar(k) if *k == key => Some(&entry.value),
                _ => None,
            }),
            _ => None,
        }
    }

    /// Looks up a mutable reference to a value in a mapping by its scalar key string.
    pub fn get_mut(&mut self, key: &str) -> Option<&mut Yaml<'a>> {
        match self {
            Self::Mapping(entries) => entries.iter_mut().find_map(|entry| match &entry.key {
                Self::Scalar(k) if *k == key => Some(&mut entry.value),
                _ => None,
            }),
            _ => None,
        }
    }

    /// Looks up an element in a sequence by zero-based index.
    pub fn get_idx(&self, index: usize) -> Option<&Yaml<'a>> {
        match self {
            Self::Sequence(items) => items.get(index),
            _ => None,
        }
    }

    /// Returns the number of elements in a sequence or entries in a mapping,
    /// or `1` for scalars.
    pub fn len(&self) -> usize {
        match self {
            Self::Scalar(_) => 1,
            Self::Sequence(items) => items.len(),
            Self::Mapping(entries) => entries.len(),
        }
    }

    /// Returns `true` if the node is an empty sequence, empty mapping, or empty scalar.
    pub fn is_empty(&self) -> bool {
        match self {
            Self::Scalar(s) => s.is_empty(),
            Self::Sequence(items) => items.is_empty(),
            Self::Mapping(entries) => entries.is_empty(),
        }
    }

    // ==============================================================================
    // Typed Scalar Conversion Helpers
    // ==============================================================================

    /// Parses the scalar value as a boolean (`true`/`false`, `yes`/`no`, `on`/`off`, `1`/`0`).
    pub fn to_bool(&self) -> Result<bool> {
        let raw = self.as_scalar().ok_or_else(|| {
            YamlError::new(
                ErrorKind::Custom("expected scalar for boolean conversion".into()),
                Position::start(),
            )
        })?;

        match raw.trim().to_ascii_lowercase().as_str() {
            "true" | "yes" | "on" | "1" => Ok(true),
            "false" | "no" | "off" | "0" => Ok(false),
            other => Err(YamlError::new(
                ErrorKind::Custom(format!("cannot parse '{other}' as boolean")),
                Position::start(),
            )),
        }
    }

    /// Parses the scalar value as an `i64` integer.
    pub fn to_i64(&self) -> Result<i64> {
        let raw = self.as_scalar().ok_or_else(|| {
            YamlError::new(
                ErrorKind::Custom("expected scalar for integer conversion".into()),
                Position::start(),
            )
        })?;

        raw.trim().parse::<i64>().map_err(|e| {
            YamlError::new(
                ErrorKind::Custom(format!("cannot parse '{raw}' as integer: {e}")),
                Position::start(),
            )
        })
    }

    /// Parses the scalar value as an `f64` floating point number.
    pub fn to_f64(&self) -> Result<f64> {
        let raw = self.as_scalar().ok_or_else(|| {
            YamlError::new(
                ErrorKind::Custom("expected scalar for float conversion".into()),
                Position::start(),
            )
        })?;

        raw.trim().parse::<f64>().map_err(|e| {
            YamlError::new(
                ErrorKind::Custom(format!("cannot parse '{raw}' as float: {e}")),
                Position::start(),
            )
        })
    }
}

// ==============================================================================
// Indexing Operator Implementations
// ==============================================================================

impl<'a> Index<&str> for Yaml<'a> {
    type Output = Yaml<'a>;

    #[inline]
    fn index(&self, key: &str) -> &Self::Output {
        self.get(key)
            .unwrap_or_else(|| panic!("key '{key}' not found in YAML mapping"))
    }
}

impl<'a> Index<usize> for Yaml<'a> {
    type Output = Yaml<'a>;

    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        self.get_idx(index)
            .unwrap_or_else(|| panic!("index {index} out of bounds for YAML sequence"))
    }
}

// ==============================================================================
// Display Implementation
// ==============================================================================

impl<'a> fmt::Display for Yaml<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        format_yaml(self, f, 0)
    }
}

fn format_yaml(val: &Yaml<'_>, f: &mut fmt::Formatter<'_>, indent_level: usize) -> fmt::Result {
    let indent = "  ".repeat(indent_level);
    match val {
        Yaml::Scalar(s) => write!(f, "{s}"),
        Yaml::Sequence(items) => {
            if items.is_empty() {
                write!(f, "[]")
            } else {
                for (i, item) in items.iter().enumerate() {
                    if i > 0 {
                        writeln!(f)?;
                    }
                    write!(f, "{indent}- ")?;
                    match item {
                        Yaml::Scalar(s) => write!(f, "{s}")?,
                        Yaml::Sequence(_) | Yaml::Mapping(_) => {
                            writeln!(f)?;
                            format_yaml(item, f, indent_level + 1)?;
                        }
                    }
                }
                Ok(())
            }
        }
        Yaml::Mapping(entries) => {
            if entries.is_empty() {
                write!(f, "{{}}")
            } else {
                for (i, entry) in entries.iter().enumerate() {
                    if i > 0 {
                        writeln!(f)?;
                    }
                    write!(f, "{indent}{}: ", entry.key)?;
                    match &entry.value {
                        Yaml::Scalar(s) => write!(f, "{s}")?,
                        Yaml::Sequence(_) | Yaml::Mapping(_) => {
                            writeln!(f)?;
                            format_yaml(&entry.value, f, indent_level + 1)?;
                        }
                    }
                }
                Ok(())
            }
        }
    }
}

// ==============================================================================
// Compile-Fail Verification Tests
// ==============================================================================

/// Compile-fail test verifying that `Yaml<'a>` cannot outlive the borrowed `&'a str` buffer.
///
/// This doctest validates borrow checker lifetime safety: returning a `Yaml` node
/// that borrows from a locally scoped `String` that gets dropped must fail compilation
/// with lifetime borrow error `E0515`.
///
/// ```compile_fail
/// use yami::{parse, Yaml};
///
/// fn parse_dropped_string() -> Yaml<'static> {
///     let local_string = String::from("key: value");
///     let parsed = parse(&local_string).unwrap();
///     parsed // compile_fail: `local_string` is dropped at end of function while still borrowed
/// }
/// ```
#[allow(dead_code)]
fn _compile_fail_lifetime_enforcement() {}

// ==============================================================================
// Unit Tests
// ==============================================================================

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

    #[test]
    fn test_scalar_helpers() {
        let scalar = Yaml::Scalar("42");
        assert!(scalar.is_scalar());
        assert!(!scalar.is_sequence());
        assert!(!scalar.is_mapping());
        assert_eq!(scalar.as_scalar(), Some("42"));
        assert_eq!(scalar.as_str(), Some("42"));
        assert_eq!(scalar.to_i64().expect("should parse 42 as i64"), 42);
        assert!((scalar.to_f64().expect("should parse 42 as f64") - 42.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_boolean_conversions() {
        assert!(Yaml::Scalar("true").to_bool().expect("true should parse"));
        assert!(Yaml::Scalar("yes").to_bool().expect("yes should parse"));
        assert!(Yaml::Scalar("ON").to_bool().expect("ON should parse"));
        assert!(Yaml::Scalar("1").to_bool().expect("1 should parse"));

        assert!(!Yaml::Scalar("false").to_bool().expect("false should parse"));
        assert!(!Yaml::Scalar("no").to_bool().expect("no should parse"));
        assert!(!Yaml::Scalar("off").to_bool().expect("off should parse"));
        assert!(!Yaml::Scalar("0").to_bool().expect("0 should parse"));

        assert!(Yaml::Scalar("invalid").to_bool().is_err());
    }

    #[test]
    fn test_sequence_helpers() {
        let seq = Yaml::Sequence(vec![
            Yaml::Scalar("first"),
            Yaml::Scalar("second"),
            Yaml::Scalar("third"),
        ]);
        assert!(seq.is_sequence());
        assert_eq!(seq.len(), 3);
        assert_eq!(seq.get_idx(1), Some(&Yaml::Scalar("second")));
        assert_eq!(seq[0], Yaml::Scalar("first"));
        assert_eq!(seq[2], Yaml::Scalar("third"));
    }

    #[test]
    fn test_mapping_helpers() {
        let mapping = Yaml::Mapping(vec![
            Entry::new(Yaml::Scalar("name"), Yaml::Scalar("Alice")),
            Entry::new(Yaml::Scalar("age"), Yaml::Scalar("30")),
        ]);

        assert!(mapping.is_mapping());
        assert_eq!(mapping.len(), 2);
        assert_eq!(mapping.get("name"), Some(&Yaml::Scalar("Alice")));
        assert_eq!(mapping["age"], Yaml::Scalar("30"));
        assert_eq!(
            mapping["age"]
                .to_i64()
                .expect("age should parse as integer"),
            30
        );
        assert_eq!(mapping.get("nonexistent"), None);
    }

    #[test]
    fn test_display_formatting() {
        let mapping = Yaml::Mapping(vec![
            Entry::new(Yaml::Scalar("host"), Yaml::Scalar("localhost")),
            Entry::new(Yaml::Scalar("port"), Yaml::Scalar("8080")),
        ]);
        let formatted = mapping.to_string();
        assert!(formatted.contains("host: localhost"));
        assert!(formatted.contains("port: 8080"));
    }
}