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
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
//! Error quality tests - verifying span accuracy and message helpfulness.
//!
//! These tests ensure that errors provide precise location information
//! and helpful messages that enable rustc-like diagnostics.

#![cfg(feature = "derive")]

use ron2::{Result, Value, error::ErrorKind};

// =============================================================================
// Span Accuracy Tests - Parsing Errors
// =============================================================================

/// Helper to verify span points to expected text in source.
#[allow(dead_code)]
fn verify_span_text(source: &str, result: Result<Value>, expected_text: &str) {
    let err = result.expect_err("expected an error");
    let span = err.span();
    let actual_text = &source[span.start_offset..span.end_offset];
    assert_eq!(
        actual_text, expected_text,
        "Span should point to '{}' but points to '{}'\nError: {}\nSpan: {:?}",
        expected_text, actual_text, err, span
    );
}

/// Helper to verify span starts at expected line and column.
#[allow(dead_code)]
fn verify_span_position(
    result: Result<Value>,
    expected_line: usize,
    expected_col: usize,
    context: &str,
) {
    let err = result.expect_err("expected an error");
    assert_eq!(
        err.span().start.line,
        expected_line,
        "{}: expected line {}, got line {}\nError: {}",
        context,
        expected_line,
        err.span().start.line,
        err
    );
    assert_eq!(
        err.span().start.col,
        expected_col,
        "{}: expected col {}, got col {}\nError: {}",
        context,
        expected_col,
        err.span().start.col,
        err
    );
}

#[test]
fn span_points_to_invalid_token() {
    // Invalid character in number - should point to the 'G'
    let source = "0xGGG";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    // The error should be at the invalid digit
    assert_eq!(err.span().start.line, 1);
    assert!(
        err.span().start.col >= 3,
        "Error should point to 'G' at col 3 or later, got col {}",
        err.span().start.col
    );
}

#[test]
fn span_points_to_unclosed_bracket_content() {
    let source = "[1, 2, 3";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    // Error should be at end of file
    assert_eq!(err.span().start.line, 1);
    assert_eq!(err.span().start.col, 9, "Error should be at EOF position");
}

#[test]
fn span_multiline_points_to_correct_line() {
    use ron2::FromRon;

    // Bare identifiers are valid RON at the parsing level (they become unit-like values).
    // The error occurs during deserialization when we try to convert to Vec<i32>.
    let source = r#"[
    1,
    2,
    invalid_ident
]"#;

    // Parsing succeeds - bare identifiers are valid RON
    let result: Result<Value> = source.parse();
    assert!(result.is_ok(), "Parsing should succeed: {:?}", result);

    // Deserialization to Vec<i32> should fail with correct span
    let result = Vec::<i32>::from_ron(source);
    let err = result.expect_err("deserialization to Vec<i32> should fail");

    // Error should be on line 4 where invalid_ident is
    assert_eq!(
        err.span().start.line,
        4,
        "Error should be on line 4, got line {}",
        err.span().start.line
    );
}

#[test]
fn span_points_to_unclosed_string() {
    let source = "\"hello world";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    // Should point to the string start or end
    assert_eq!(err.span().start.line, 1);
}

#[test]
fn span_points_to_invalid_escape_sequence() {
    let source = r#""hello\qworld""#;
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    // Error should be in the string where \q appears
    assert_eq!(err.span().start.line, 1);
    // Column should point to the escape sequence
    assert!(
        err.span().start.col >= 7,
        "Error should point to escape sequence area, got col {}",
        err.span().start.col
    );
}

#[test]
fn span_points_to_map_missing_colon() {
    let source = r#"{ "key" "value" }"#;
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    assert_eq!(err.span().start.line, 1);
    // Should point to "value" where colon was expected before
    assert!(
        err.span().start.col >= 9,
        "Error should point near 'value', got col {}",
        err.span().start.col
    );
}

#[test]
fn span_points_to_trailing_junk() {
    let source = "42 junk";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    assert_eq!(err.span().start.line, 1);
    assert_eq!(
        err.span().start.col,
        4,
        "Error should point to 'junk' at col 4"
    );
}

#[test]
fn span_byte_offsets_enable_slicing() {
    let source = "[1, 2, }";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    // Verify byte offsets are valid for slicing
    assert!(
        err.span().start_offset <= source.len(),
        "start_offset {} should be <= source len {}",
        err.span().start_offset,
        source.len()
    );
    assert!(
        err.span().end_offset <= source.len(),
        "end_offset {} should be <= source len {}",
        err.span().end_offset,
        source.len()
    );
    assert!(
        err.span().start_offset <= err.span().end_offset,
        "start_offset should be <= end_offset"
    );
}

// =============================================================================
// Span Accuracy Tests - Multiline
// =============================================================================

#[test]
fn span_deeply_nested_error() {
    use std::collections::HashMap;

    use ron2::FromRon;

    // Bare identifiers are valid RON at the parsing level.
    // The error occurs during deserialization when we try to convert to the target type.
    let source = r#"{
    "level1": {
        "level2": {
            "level3": [
                1,
                2,
                invalid
            ]
        }
    }
}"#;

    // Parsing succeeds - bare identifiers are valid RON
    let result: Result<Value> = source.parse();
    assert!(result.is_ok(), "Parsing should succeed: {:?}", result);

    // Deserialization should fail with correct span
    type NestedMap = HashMap<String, HashMap<String, HashMap<String, Vec<i32>>>>;
    let result = NestedMap::from_ron(source);
    let err = result.expect_err("deserialization should fail");

    // Error should be on line 7 where "invalid" appears
    assert_eq!(
        err.span().start.line,
        7,
        "Error should be on line 7, got {}",
        err.span().start.line
    );
}

// =============================================================================
// Error Message Quality Tests
// =============================================================================

#[test]
fn error_message_includes_context_for_comma() {
    let source = "[1 2]";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();
    let msg = err.to_string();

    // Error should mention what was expected
    assert!(
        msg.contains("comma") || msg.contains(",") || msg.contains("Expected"),
        "Error should mention comma or expectation: {}",
        msg
    );
}

#[test]
fn error_message_shows_position() {
    let source = "[1, 2, }";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();
    let msg = err.to_string();

    // Should show line:col format
    assert!(
        msg.contains(':'),
        "Error should contain position separator: {}",
        msg
    );
}

#[test]
fn error_message_eof_is_clear() {
    let source = "[1, 2,";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();
    let msg = err.to_string();

    // Should mention EOF or end
    assert!(
        msg.to_lowercase().contains("eof")
            || msg.to_lowercase().contains("end")
            || msg.contains("Unexpected"),
        "Error should mention EOF: {}",
        msg
    );
}

#[test]
fn type_bounds_checked_at_deserialization_not_parsing() {
    // Type bounds like u8 are checked during deserialization, not parsing
    // 256 is fine for i64 (default integer storage) but too big for u8
    let source = "256";
    let result: Result<Value> = source.parse();
    assert!(result.is_ok(), "256 should parse as a generic number");

    // i64::MAX is valid
    let source = "9223372036854775807";
    let result: Result<Value> = source.parse();
    assert!(result.is_ok(), "i64::MAX should parse successfully");
}

#[test]
fn error_distinguishes_bracket_types() {
    // Mismatched brackets
    let source1 = "[1, 2}";
    let result1: Result<Value> = source1.parse();
    let err1 = result1.unwrap_err();

    let source2 = "{1: 2]";
    let result2: Result<Value> = source2.parse();
    let err2 = result2.unwrap_err();

    // Errors should be different or at least specific
    let msg1 = err1.to_string();
    let msg2 = err2.to_string();

    // Both should have position info
    assert!(msg1.contains(':'), "Error 1 should have position: {}", msg1);
    assert!(msg2.contains(':'), "Error 2 should have position: {}", msg2);
}

// =============================================================================
// Error Code Specificity Tests
// =============================================================================

#[test]
fn error_code_for_unclosed_string() {
    let source = "\"hello";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    assert!(
        matches!(err.kind(), ErrorKind::ExpectedStringEnd | ErrorKind::Eof),
        "Expected ExpectedStringEnd or Eof error, got {:?}",
        err.kind()
    );
}

#[test]
fn error_code_for_invalid_escape() {
    let source = r#""\q""#;
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    assert!(
        matches!(err.kind(), ErrorKind::InvalidEscape(_)),
        "Expected InvalidEscape error, got {:?}",
        err.kind()
    );
}

#[test]
fn error_code_for_trailing_characters() {
    let source = "42 extra";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    assert!(
        matches!(err.kind(), ErrorKind::TrailingCharacters),
        "Expected TrailingCharacters error, got {:?}",
        err.kind()
    );
}

// =============================================================================
// Edge Cases
// =============================================================================

#[test]
fn empty_input_has_sensible_error() {
    let source = "";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    // Should report EOF at position 1:1
    assert_eq!(err.span().start.line, 1);
    assert_eq!(err.span().start.col, 1);
    assert!(matches!(err.kind(), ErrorKind::Eof));
}

#[test]
fn whitespace_only_has_sensible_error() {
    let source = "   \n\n   ";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    // Should report EOF
    assert!(matches!(err.kind(), ErrorKind::Eof));
}

#[test]
fn unicode_positions_are_byte_based() {
    // Unicode: "日本語" = 3 characters but 9 bytes
    // Columns are byte-based for performance (O(log n) lookup via LineIndex)
    let source = "\"日本語\" junk";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    // The error should be at "junk"
    // String bytes: " (1) + 日 (3) + 本 (3) + 語 (3) + " (1) + space (1) = 12 bytes
    // So "junk" starts at byte offset 12, column 13 (1-indexed)
    assert_eq!(err.span().start.line, 1);
    assert_eq!(
        err.span().start.col,
        13,
        "Column {} should be byte-based (expected 13)",
        err.span().start.col
    );
}

#[test]
fn very_long_line_has_correct_position() {
    let long_prefix = "x".repeat(1000);
    let source = format!("[{}]", long_prefix);
    // This should parse fine
    let result: Result<Value> = source.parse();
    assert!(result.is_ok());

    // Now with error at end
    let source_err = format!("[{}", long_prefix);
    let result_err: Result<Value> = source_err.parse();
    let err = result_err.unwrap_err();

    assert_eq!(err.span().start.line, 1);
    assert!(
        err.span().start.col > 1000,
        "Column should be > 1000, got {}",
        err.span().start.col
    );
}

// =============================================================================
// Span Text Extraction Tests
// =============================================================================

#[test]
fn span_slice_method_works() {
    let source = "{ \"key\" value }";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    // Use the span to extract text
    let sliced = err.span().slice(source);

    // The sliced text should be non-empty and from the source
    assert!(
        source.contains(sliced) || sliced.is_empty(),
        "Sliced '{}' should be in source",
        sliced
    );
}

// =============================================================================
// Comment handling in errors
// =============================================================================

#[test]
fn error_after_comment_has_correct_position() {
    let source = r#"// This is a comment
[1, 2, }"#;
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    // Error should be on line 2
    assert_eq!(
        err.span().start.line,
        2,
        "Error should be on line 2 after comment"
    );
}

#[test]
fn error_inside_block_comment() {
    let source = "/* unclosed comment";
    let result: Result<Value> = source.parse();
    let err = result.unwrap_err();

    // Should report unclosed block comment
    assert!(
        matches!(err.kind(), ErrorKind::UnclosedBlockComment),
        "Expected UnclosedBlockComment, got {:?}",
        err.kind()
    );
}