ron2 0.3.0

RON parser with full AST access, perfect round-trip fidelity, and formatting tools
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
//! Value module.

use alloc::{borrow::Cow, boxed::Box, string::String, vec::Vec};
use core::{
    cmp::Eq,
    fmt::{self, Display, Formatter},
    hash::Hash,
    str::FromStr,
};

mod map;
mod number;

pub use map::Map;
pub use number::{F32, F64, Number};

use crate::{
    Error, ErrorKind,
    ast::{ItemTrivia, RonFormatter, SerializeRon, into_value, parse_document},
};

/// Ordered list of struct fields (name-value pairs).
///
/// Uses a `Vec` to preserve insertion order and support all trait derivations.
/// Field lookup is O(n) but structs typically have few fields.
pub type StructFields = Vec<(String, Value)>;

/// A RON value that can represent any valid RON data.
///
/// This enum distinguishes between all RON syntactic forms:
/// - `Seq`: sequences `[a, b, c]`
/// - `Tuple`: tuples `(a, b, c)`
/// - `Map`: maps with arbitrary keys `{ key: value }`
/// - `Struct`: anonymous structs with named fields `(x: 1, y: 2)`
/// - `Named`: named types (structs/enums) like `Point(x: 1)` or `Option::Some(1)`
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Value {
    // Primitives
    Bool(bool),
    Char(char),
    Number(Number),
    String(String),
    Bytes(Vec<u8>),
    Unit,
    /// `None` or `Some(value)` - special-cased for convenience
    Option(Option<Box<Value>>),

    // Collections (anonymous)
    /// Sequence: `[a, b, c]`
    Seq(Vec<Value>),
    /// Tuple: `(a, b, c)` - positional elements
    Tuple(Vec<Value>),
    /// Map: `{ key: value }` - arbitrary Value keys
    Map(Map),
    /// Anonymous struct: `(x: 1, y: 2)` - named fields, no type name
    Struct(StructFields),

    // Named types (struct or enum)
    /// Named type: `Point`, `Point(1, 2)`, `Point(x: 1)`, or `Type::Variant(...)`
    ///
    /// The `name` field stores the full path as-is (e.g., `"Type::Variant"`).
    Named {
        /// The type/variant name, e.g., `"Point"` or `"Option::Some"`
        name: String,
        /// The content of the named type
        content: NamedContent,
    },
}

impl FromStr for Value {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let doc = parse_document(s)?;

        // Use consuming conversion to avoid unnecessary clones
        match into_value(doc) {
            Some(Ok(value)) => Ok(value),
            Some(Err(e)) => {
                // Conversion error - e already contains span information
                Err(e)
            }
            None => {
                // Empty document - return EOF error (consistent with FromRon::from_ron)
                Err(Error::at_start(ErrorKind::Eof))
            }
        }
    }
}

impl Display for Value {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        use crate::ast::to_ron_string;

        // Serialize directly using SerializeRon
        let formatted = to_ron_string(self);

        f.write_str(&formatted)
    }
}

/// Content of a named type (struct or enum variant).
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum NamedContent {
    /// Unit: `Point` or `MyEnum::Variant`
    Unit,
    /// Tuple: `Point(1, 2)` or `Some(x)`
    Tuple(Vec<Value>),
    /// Struct: `Point(x: 1, y: 2)`
    Struct(StructFields),
}

impl From<bool> for Value {
    fn from(value: bool) -> Self {
        Self::Bool(value)
    }
}

impl From<char> for Value {
    fn from(value: char) -> Self {
        Self::Char(value)
    }
}

impl<K: Into<Value>, V: Into<Value>> FromIterator<(K, V)> for Value {
    fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
        Self::Map(iter.into_iter().collect())
    }
}

impl From<Map> for Value {
    fn from(value: Map) -> Self {
        Self::Map(value)
    }
}

impl<T: Into<Number>> From<T> for Value {
    fn from(value: T) -> Self {
        Self::Number(value.into())
    }
}

impl<T: Into<Value>> From<Option<T>> for Value {
    fn from(value: Option<T>) -> Self {
        Self::Option(value.map(Into::into).map(Box::new))
    }
}

impl<'a> From<&'a str> for Value {
    fn from(value: &'a str) -> Self {
        String::from(value).into()
    }
}

impl<'a> From<Cow<'a, str>> for Value {
    fn from(value: Cow<'a, str>) -> Self {
        String::from(value).into()
    }
}

impl From<String> for Value {
    fn from(value: String) -> Self {
        Self::String(value)
    }
}

/// Special case to allow `Value::from(b"byte string")`
impl<const N: usize> From<&'static [u8; N]> for Value {
    fn from(value: &'static [u8; N]) -> Self {
        Self::Bytes(Vec::from(*value))
    }
}

impl<T: Into<Value>> FromIterator<T> for Value {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        Self::Seq(iter.into_iter().map(Into::into).collect())
    }
}

impl<'a, T: Clone + Into<Value>> From<&'a [T]> for Value {
    fn from(value: &'a [T]) -> Self {
        value.iter().map(Clone::clone).map(Into::into).collect()
    }
}

impl<T: Into<Value>> From<Vec<T>> for Value {
    fn from(value: Vec<T>) -> Self {
        value.into_iter().collect()
    }
}

impl From<()> for Value {
    fn from(_value: ()) -> Self {
        Value::Unit
    }
}

// ============================================================================
// SerializeRon implementation for Value
// ============================================================================

impl SerializeRon for Value {
    fn serialize(&self, fmt: &mut RonFormatter<'_>) {
        match self {
            // Primitives
            Value::Unit => fmt.write_str("()"),
            Value::Bool(b) => fmt.write_str(if *b { "true" } else { "false" }),
            Value::Char(c) => fmt.format_char_value(*c),
            Value::String(s) => fmt.format_string_value(s),
            Value::Bytes(b) => fmt.format_bytes_value(b),
            Value::Number(n) => format_number_to(n, fmt),

            // Option
            Value::Option(opt) => {
                let value = opt
                    .as_ref()
                    .map(|inner| (inner.as_ref(), ItemTrivia::empty()));
                fmt.format_option_with(value);
            }

            // Seq
            Value::Seq(items) => {
                let items = items.iter().map(|v| (ItemTrivia::empty(), v));
                fmt.format_seq_with(None, None, items);
            }

            // Tuple
            Value::Tuple(elements) => {
                let items = elements.iter().map(|v| (ItemTrivia::empty(), v));
                fmt.format_tuple_with(None, None, items);
            }

            // Map
            Value::Map(map) => {
                let entries = map.iter().map(|(k, v)| (ItemTrivia::empty(), k, v));
                fmt.format_map_with(None, None, entries);
            }

            // Anonymous struct
            Value::Struct(fields) => {
                let field_items = fields
                    .iter()
                    .map(|(name, value)| (ItemTrivia::empty(), name.as_str(), value));
                fmt.format_anon_struct_with(None, None, field_items);
            }

            // Named type
            Value::Named { name, content } => {
                fmt.write_str(name);
                match content {
                    NamedContent::Unit => {
                        // Just the name, no body
                    }
                    NamedContent::Tuple(elements) => {
                        let items = elements.iter().map(|v| (ItemTrivia::empty(), v));
                        fmt.format_tuple_with(None, None, items);
                    }
                    NamedContent::Struct(fields) => {
                        let field_items = fields
                            .iter()
                            .map(|(n, v)| (ItemTrivia::empty(), n.as_str(), v));
                        fmt.format_struct_fields_with(None, None, field_items);
                    }
                }
            }
        }
    }
}

/// Format a number directly to the formatter.
fn format_number_to(n: &Number, fmt: &mut RonFormatter<'_>) {
    use alloc::string::ToString;

    match n {
        Number::I8(v) => fmt.write_str(itoa::Buffer::new().format(*v)),
        Number::I16(v) => fmt.write_str(itoa::Buffer::new().format(*v)),
        Number::I32(v) => fmt.write_str(itoa::Buffer::new().format(*v)),
        Number::I64(v) => fmt.write_str(itoa::Buffer::new().format(*v)),
        #[cfg(feature = "integer128")]
        Number::I128(v) => fmt.write_str(itoa::Buffer::new().format(*v)),
        Number::U8(v) => fmt.write_str(itoa::Buffer::new().format(*v)),
        Number::U16(v) => fmt.write_str(itoa::Buffer::new().format(*v)),
        Number::U32(v) => fmt.write_str(itoa::Buffer::new().format(*v)),
        Number::U64(v) => fmt.write_str(itoa::Buffer::new().format(*v)),
        #[cfg(feature = "integer128")]
        Number::U128(v) => fmt.write_str(itoa::Buffer::new().format(*v)),
        Number::F32(f) => {
            let v = f.get();
            if v.is_nan() {
                fmt.write_str("NaN");
            } else if v.is_infinite() {
                fmt.write_str(if v.is_sign_positive() { "inf" } else { "-inf" });
            } else {
                // Format to string first to check for decimal point
                let s = v.to_string();
                if s.contains('.') || s.contains('e') || s.contains('E') {
                    fmt.write_str(&s);
                } else {
                    fmt.write_str(&s);
                    fmt.write_str(".0");
                }
            }
        }
        Number::F64(f) => {
            let v = f.get();
            if v.is_nan() {
                fmt.write_str("NaN");
            } else if v.is_infinite() {
                fmt.write_str(if v.is_sign_positive() { "inf" } else { "-inf" });
            } else {
                let s = v.to_string();
                if s.contains('.') || s.contains('e') || s.contains('E') {
                    fmt.write_str(&s);
                } else {
                    fmt.write_str(&s);
                    fmt.write_str(".0");
                }
            }
        }
        // Handle non-exhaustive variant (future-proofing)
        _ => fmt.write_str("0"),
    }
}

#[cfg(test)]
mod tests {
    use alloc::{vec, vec::Vec};

    use super::*;

    #[test]
    fn boolean() {
        assert_eq!(Value::from(true), Value::Bool(true));
        assert_eq!(Value::from(false), Value::Bool(false));
    }

    #[test]
    fn float() {
        assert_eq!(
            Value::from(42_f32),
            Value::Number(Number::F32(42_f32.into()))
        );
        assert_eq!(
            Value::from(42_f64),
            Value::Number(Number::F64(42_f64.into()))
        );
    }

    #[test]
    fn int() {
        assert_eq!(Value::from(0_i8), Value::Number(Number::I8(0)));
        assert_eq!(Value::from(0_i16), Value::Number(Number::I16(0)));
        assert_eq!(Value::from(0_i32), Value::Number(Number::I32(0)));
        assert_eq!(Value::from(0_i64), Value::Number(Number::I64(0)));
        #[cfg(feature = "integer128")]
        assert_eq!(Value::from(0_i128), Value::Number(Number::I128(0)));
        assert_eq!(Value::from(0_u8), Value::Number(Number::U8(0)));
        assert_eq!(Value::from(0_u16), Value::Number(Number::U16(0)));
        assert_eq!(Value::from(0_u32), Value::Number(Number::U32(0)));
        assert_eq!(Value::from(0_u64), Value::Number(Number::U64(0)));
        #[cfg(feature = "integer128")]
        assert_eq!(Value::from(0_u128), Value::Number(Number::U128(0)));
    }

    #[test]
    fn char_value() {
        assert_eq!(Value::from('a'), Value::Char('a'));
    }

    #[test]
    fn string() {
        assert_eq!(Value::from("slice"), Value::String(String::from("slice")));
        assert_eq!(
            Value::from(String::from("string")),
            Value::String(String::from("string"))
        );
        assert_eq!(
            Value::from(Cow::Borrowed("cow")),
            Value::String(String::from("cow"))
        );
    }

    #[test]
    fn bytes() {
        assert_eq!(Value::from(b"bytes"), Value::Bytes(Vec::from(*b"bytes")));
    }

    #[test]
    fn map() {
        assert_eq!(Value::from(Map::new()), Value::Map(Map::new()));
        assert_eq!(
            Value::from_iter([("a", 42)]),
            Value::Map({
                let mut map = Map::new();
                map.insert(Value::from("a"), Value::from(42));
                map
            })
        );
    }

    #[test]
    fn option() {
        assert_eq!(Value::from(Option::<bool>::None), Value::Option(None));
        assert_eq!(
            Value::from(Some(false)),
            Value::Option(Some(Box::new(Value::Bool(false))))
        );
        assert_eq!(
            Value::from(Some(Option::<bool>::None)),
            Value::Option(Some(Box::new(Value::Option(None))))
        );
    }

    #[test]
    fn seq() {
        assert_eq!(
            Value::from([-1_i8, 2, -3].as_slice()),
            Value::Seq(vec![
                Value::from(-1_i8),
                Value::from(2_i8),
                Value::from(-3_i8)
            ])
        );
        assert_eq!(
            Value::from(vec![-1_i8, 2, -3]),
            Value::Seq(vec![
                Value::from(-1_i8),
                Value::from(2_i8),
                Value::from(-3_i8)
            ])
        );
        assert_eq!(
            Value::from_iter([-1_i8, 2, -3]),
            Value::Seq(vec![
                Value::from(-1_i8),
                Value::from(2_i8),
                Value::from(-3_i8)
            ])
        );
    }

    #[test]
    fn unit() {
        assert_eq!(Value::from(()), Value::Unit);
    }
}