babbel_yaml 0.1.1

Fast, modular YAML 1.2 parser and emitter with anchors, aliases, and tags
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
//! Embedded Lightweight Node Representation
//!
//! Defines minimal, fixed-size node types for YAML parsing on embedded systems.
//! Optimized for memory-constrained environments, supporting basic scalar types and
//! conversion from full-featured nodes. Collection support is limited or requires arena context.
//!
//! Copyright (c) 2026 YAML Library Developers

use crate::nodes::node::{Node, Numeric};
use core::convert::TryFrom;
impl TryFrom<&Node> for LightNode {
    type Error = &'static str;
    fn try_from(node: &Node) -> Result<Self, Self::Error> {
        match node {
            Node::Boolean(b) => Ok(LightNode::Boolean(*b)),
            Node::Number(n) => Ok(LightNode::Number(LightNumeric::try_from(n)?)),
            Node::Str(s, q, b) => {
                let fs = FixedString::from_str(s)?;
                Ok(LightNode::Str(fs, q.clone(), b.clone()))
            }
            Node::None => Ok(LightNode::None),
            // For collections, conversion requires arena context, so skip here
            _ => Err("Unsupported node type for lightweight conversion"),
        }
    }
}

impl TryFrom<&Numeric> for LightNumeric {
    type Error = &'static str;
    fn try_from(num: &Numeric) -> Result<Self, Self::Error> {
        match num {
            Numeric::Integer(i) => Ok(LightNumeric::Integer(*i as i32)),
            Numeric::Int32(i) => Ok(LightNumeric::Integer(*i)),
            Numeric::Int16(i) => Ok(LightNumeric::Short(*i)),
            Numeric::Int8(i) => Ok(LightNumeric::Short(*i as i16)),
            Numeric::Float(f) => Ok(LightNumeric::Float(*f as f32)),
            Numeric::Byte(b) => Ok(LightNumeric::Byte(*b)),
            Numeric::UInt8(u) => Ok(LightNumeric::Byte(*u)),
            Numeric::UInt16(u) => Ok(LightNumeric::Byte((*u).min(255) as u8)),
            Numeric::UInt32(u) => Ok(LightNumeric::Integer((*u).min(i32::MAX as u32) as i32)),
            Numeric::UInteger(u) => Ok(LightNumeric::Integer((*u).min(i32::MAX as u64) as i32)),
            // Add more mappings as needed
        }
    }
}

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

use crate::embedded::config::*;
use crate::nodes::node::{BlockStyle, QuoteType};

/// Lightweight numeric representation for embedded systems
#[derive(Clone, Debug, PartialEq)]
pub enum LightNumeric {
    Integer(i32),
    Float(f32),
    Byte(u8),
    Short(i16),
}

impl From<i32> for LightNumeric {
    fn from(value: i32) -> Self {
        LightNumeric::Integer(value)
    }
}

impl From<f32> for LightNumeric {
    fn from(value: f32) -> Self {
        LightNumeric::Float(value)
    }
}

impl From<u8> for LightNumeric {
    fn from(value: u8) -> Self {
        LightNumeric::Byte(value)
    }
}

impl From<i16> for LightNumeric {
    fn from(value: i16) -> Self {
        LightNumeric::Short(value)
    }
}

/// Fixed-size string for embedded systems
#[derive(Clone, Debug, PartialEq)]
pub struct FixedString {
    data: [u8; 256], // Fixed size buffer
    len: usize,
}

impl FixedString {
    pub fn new() -> Self {
        Self {
            data: [0; 256],
            len: 0,
        }
    }

    pub fn from_str(s: &str) -> Result<Self, &'static str> {
        let bytes = s.as_bytes();
        if bytes.len() > 256 {
            return Err("String too long for FixedString");
        }
        let mut data = [0; 256];
        data[..bytes.len()].copy_from_slice(bytes);
        Ok(Self {
            data,
            len: bytes.len(),
        })
    }

    pub fn as_str(&self) -> &str {
        core::str::from_utf8(&self.data[..self.len]).unwrap_or("")
    }

    pub fn len(&self) -> usize {
        self.len
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }
}

impl Default for FixedString {
    fn default() -> Self {
        Self::new()
    }
}

/// Lightweight node for embedded systems with bounded size
#[derive(Clone, Debug, PartialEq)]
pub enum LightNode {
    /// Boolean value
    Boolean(bool),
    /// Numeric value with reduced precision
    Number(LightNumeric),
    /// String with fixed maximum size
    Str(FixedString, QuoteType, BlockStyle),
    /// Reference to array in arena (index-based)
    ArrayRef(u16),
    /// Reference to mapping in arena (index-based)
    MappingRef(u16),
    /// Null value
    None,
}

impl LightNode {
    /// Creates a boolean node
    pub fn boolean(value: bool) -> Self {
        LightNode::Boolean(value)
    }

    /// Creates an integer node
    pub fn integer(value: i32) -> Self {
        LightNode::Number(LightNumeric::Integer(value))
    }

    /// Creates a float node
    pub fn float(value: f32) -> Self {
        LightNode::Number(LightNumeric::Float(value))
    }

    /// Creates a string node from &str
    pub fn string(s: &str) -> Result<Self, &'static str> {
        Ok(LightNode::Str(
            FixedString::from_str(s)?,
            QuoteType::Unquoted,
            BlockStyle::None,
        ))
    }

    /// Creates a null node
    pub fn null() -> Self {
        LightNode::None
    }
}

/// Arena allocator for nodes in embedded systems
/// Uses fixed-size arrays instead of dynamic allocation
#[derive(Debug)]
pub struct NodeArena {
    #[cfg(feature = "alloc")]
    arrays: Vec<Vec<LightNode>>,
    #[cfg(feature = "alloc")]
    mappings: Vec<Vec<(LightNode, LightNode)>>,
    #[cfg(not(feature = "alloc"))]
    arrays: [Option<[LightNode; 32]>; 16],
    #[cfg(not(feature = "alloc"))]
    mappings: [Option<[(LightNode, LightNode); 32]>; 16],
    array_count: usize,
    mapping_count: usize,
}

impl NodeArena {
    #[cfg(feature = "alloc")]
    pub fn new() -> Self {
        Self {
            arrays: Vec::new(),
            mappings: Vec::new(),
            array_count: 0,
            mapping_count: 0,
        }
    }

    #[cfg(not(feature = "alloc"))]
    pub fn new() -> Self {
        const NONE_ARRAY: Option<[LightNode; 32]> = None;
        const NONE_MAPPING: Option<[(LightNode, LightNode); 32]> = None;
        Self {
            arrays: [NONE_ARRAY; 16],
            mappings: [NONE_MAPPING; 16],
            array_count: 0,
            mapping_count: 0,
        }
    }

    #[cfg(feature = "alloc")]
    pub fn add_array(&mut self, items: Vec<LightNode>) -> Result<u16, &'static str> {
        if items.len() > MAX_SEQUENCE_ITEMS {
            return Err("Array too large");
        }
        let idx = self.array_count;
        if idx >= u16::MAX as usize {
            return Err("Too many arrays");
        }
        self.arrays.push(items);
        self.array_count += 1;
        Ok(idx as u16)
    }

    #[cfg(feature = "alloc")]
    pub fn add_mapping(&mut self, pairs: Vec<(LightNode, LightNode)>) -> Result<u16, &'static str> {
        if pairs.len() > MAX_MAPPING_PAIRS {
            return Err("Mapping too large");
        }
        let idx = self.mapping_count;
        if idx >= u16::MAX as usize {
            return Err("Too many mappings");
        }
        self.mappings.push(pairs);
        self.mapping_count += 1;
        Ok(idx as u16)
    }

    #[cfg(feature = "alloc")]
    pub fn get_array(&self, idx: u16) -> Option<&Vec<LightNode>> {
        self.arrays.get(idx as usize)
    }

    #[cfg(feature = "alloc")]
    pub fn get_mapping(&self, idx: u16) -> Option<&Vec<(LightNode, LightNode)>> {
        self.mappings.get(idx as usize)
    }
}

#[cfg(feature = "alloc")]
impl Default for NodeArena {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_light_numeric_conversions() {
        assert_eq!(LightNumeric::from(42i32), LightNumeric::Integer(42));
        assert_eq!(LightNumeric::from(3.14f32), LightNumeric::Float(3.14));
        assert_eq!(LightNumeric::from(255u8), LightNumeric::Byte(255));
        assert_eq!(LightNumeric::from(1000i16), LightNumeric::Short(1000));
    }

    #[test]
    fn test_light_numeric_try_from_numeric() {
        let n = Numeric::Integer(123);
        let ln = LightNumeric::try_from(&n).unwrap();
        assert_eq!(ln, LightNumeric::Integer(123));
        let n = Numeric::Float(1.5);
        let ln = LightNumeric::try_from(&n).unwrap();
        assert_eq!(ln, LightNumeric::Float(1.5));
        let n = Numeric::Byte(7);
        let ln = LightNumeric::try_from(&n).unwrap();
        assert_eq!(ln, LightNumeric::Byte(7));
        let n = Numeric::Int16(42);
        let ln = LightNumeric::try_from(&n).unwrap();
        assert_eq!(ln, LightNumeric::Short(42));
    }

    #[test]
    fn test_fixed_string_creation() {
        let s = FixedString::from_str("Hello").unwrap();
        assert_eq!(s.as_str(), "Hello");
        assert_eq!(s.len(), 5);
        assert!(!s.is_empty());
    }

    #[test]
    fn test_fixed_string_too_long() {
        let long_str = "a".repeat(257);
        assert!(FixedString::from_str(&long_str).is_err());
    }

    #[test]
    fn test_fixed_string_empty() {
        let s = FixedString::new();
        assert!(s.is_empty());
        assert_eq!(s.len(), 0);
        assert_eq!(s.as_str(), "");
    }

    #[test]
    fn test_fixed_string_from_str_edge_cases() {
        let empty = FixedString::from_str("").unwrap();
        assert!(empty.is_empty());
        let max = FixedString::from_str(&"a".repeat(256)).unwrap();
        assert_eq!(max.len(), 256);
        assert_eq!(max.as_str(), &"a".repeat(256));
    }

    #[test]
    fn test_light_node_creation() {
        let bool_node = LightNode::boolean(true);
        assert_eq!(bool_node, LightNode::Boolean(true));

        let int_node = LightNode::integer(42);
        assert_eq!(int_node, LightNode::Number(LightNumeric::Integer(42)));

        let float_node = LightNode::float(3.14);
        assert_eq!(float_node, LightNode::Number(LightNumeric::Float(3.14)));

        let str_node = LightNode::string("test").unwrap();
        match str_node {
            LightNode::Str(s, _, _) => assert_eq!(s.as_str(), "test"),
            _ => panic!("Expected string node"),
        }

        let null_node = LightNode::null();
        assert_eq!(null_node, LightNode::None);
    }

    #[test]
    fn test_light_node_try_from_node() {
        let node = Node::from(true);
        let ln = LightNode::try_from(&node).unwrap();
        assert_eq!(ln, LightNode::Boolean(true));
        let node = Node::from(123);
        let ln = LightNode::try_from(&node).unwrap();
        assert_eq!(ln, LightNode::Number(LightNumeric::Integer(123)));
        let node = Node::from("abc");
        let ln = LightNode::try_from(&node).unwrap();
        match ln {
            LightNode::Str(fs, _, _) => assert_eq!(fs.as_str(), "abc"),
            _ => panic!("Expected string node"),
        }
        let node = Node::None;
        let ln = LightNode::try_from(&node).unwrap();
        assert_eq!(ln, LightNode::None);
        // Test unsupported node types
        let array_node = Node::Array(vec![Node::from(1)]);
        assert!(LightNode::try_from(&array_node).is_err());
        let mapping_node = Node::Mapping(vec![(Node::from("k"), Node::from(1))]);
        assert!(LightNode::try_from(&mapping_node).is_err());
    }

    #[test]
    fn test_light_node_string_too_long() {
        let long_str = "a".repeat(257);
        assert!(LightNode::string(&long_str).is_err());
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_node_arena_arrays() {
        let mut arena = NodeArena::new();
        let items = alloc::vec![
            LightNode::integer(1),
            LightNode::integer(2),
            LightNode::integer(3),
        ];
        let idx = arena.add_array(items.clone()).unwrap();
        let retrieved = arena.get_array(idx).unwrap();
        assert_eq!(retrieved.len(), 3);
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_node_arena_mappings() {
        let mut arena = NodeArena::new();
        let pairs = alloc::vec![
            (LightNode::string("key1").unwrap(), LightNode::integer(1)),
            (LightNode::string("key2").unwrap(), LightNode::integer(2)),
        ];
        let idx = arena.add_mapping(pairs.clone()).unwrap();
        let retrieved = arena.get_mapping(idx).unwrap();
        assert_eq!(retrieved.len(), 2);
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_node_arena_array_too_large() {
        let mut arena = NodeArena::new();
        let items = alloc::vec![LightNode::integer(0); MAX_SEQUENCE_ITEMS + 1];
        assert!(arena.add_array(items).is_err());
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_node_arena_mapping_too_large() {
        let mut arena = NodeArena::new();
        let pairs =
            alloc::vec![(LightNode::integer(0), LightNode::integer(1)); MAX_MAPPING_PAIRS + 1];
        assert!(arena.add_mapping(pairs).is_err());
    }
}