tomlproc 0.1.2

A self-contained TOML 1.1.0 parser and serializer with no external dependencies
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Documents the specification calls valid.

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

use tomlproc::{DatetimeKind, Table, Value, parse};

#[track_caller]
fn doc(input: &str) -> Table {
    parse(input).unwrap_or_else(|e| panic!("expected `{input}` to parse, got: {e}"))
}

#[track_caller]
fn value(input: &str) -> Value {
    doc(&format!("x = {input}"))
        .remove("x")
        .expect("the key was parsed")
}

#[track_caller]
fn string(input: &str) -> String {
    value(input).as_str().expect("a string").to_owned()
}

// ----- keys ---------------------------------------------------------------

#[test]
fn bare_keys() {
    let table = doc("key = 1\nbare_key = 2\nbare-key = 3\n1234 = 4\n");
    assert_eq!(
        table.keys().collect::<Vec<_>>(),
        ["key", "bare_key", "bare-key", "1234"]
    );
}

#[test]
fn quoted_keys() {
    let table = doc(r#"
"127.0.0.1" = "value"
"character encoding" = "value"
"ʎǝʞ" = "value"
'quoted "value"' = "value"
"" = "empty, but valid"
"#);
    assert_eq!(table.len(), 5);
    assert!(table.contains_key("127.0.0.1"));
    assert!(table.contains_key("quoted \"value\""));
    // The empty key is valid, if discouraged; both quote styles name it.
    assert_eq!(table[""].as_str(), Some("empty, but valid"));
}

#[test]
fn dotted_keys() {
    let table = doc(r#"
name = "Orange"
physical.color = "orange"
physical.shape = "round"
site."google.com" = true
"#);
    assert_eq!(table["physical"]["color"].as_str(), Some("orange"));
    assert_eq!(table["site"]["google.com"].as_bool(), Some(true));
}

#[test]
fn whitespace_around_dots_is_ignored() {
    let table = doc("fruit . flavor = \"banana\"\n[ a . b ]\nc = 1\n");
    assert_eq!(table["fruit"]["flavor"].as_str(), Some("banana"));
    assert_eq!(table["a"]["b"]["c"].as_integer(), Some(1));
}

#[test]
fn dotted_keys_may_be_extended_by_a_later_sub_table() {
    // The spec's own example: `apple` is created by a dotted key, so
    // `[fruit.apple]` would be invalid, but adding a *new* sub-table is fine.
    let table = doc(r#"
[fruit]
apple.color = "red"
apple.taste.sweet = true

[fruit.apple.texture]
smooth = true
"#);
    assert_eq!(
        table["fruit"]["apple"]["taste"]["sweet"].as_bool(),
        Some(true)
    );
    assert_eq!(
        table["fruit"]["apple"]["texture"]["smooth"].as_bool(),
        Some(true)
    );
}

// ----- strings ------------------------------------------------------------

#[test]
fn basic_strings() {
    assert_eq!(
        string(r#""I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF.""#),
        "I'm a string. \"You can quote me\". Name\tJosé\nLocation\tSF."
    );
    assert_eq!(string(r#""\b\t\n\f\r\"\\""#), "\u{8}\t\n\u{c}\r\"\\");
    assert_eq!(string(r#""\U0001F600""#), "\u{1F600}");
    assert_eq!(string(r#""""#), "");
}

#[test]
fn literal_strings() {
    assert_eq!(
        string(r#"'C:\Users\nodejs\templates'"#),
        r"C:\Users\nodejs\templates"
    );
    assert_eq!(
        string(r#"'Tom "Dubs" Preston-Werner'"#),
        "Tom \"Dubs\" Preston-Werner"
    );
    assert_eq!(string("''"), "");
}

#[test]
fn multiline_basic_strings() {
    // The newline right after the opening delimiter is trimmed.
    assert_eq!(
        string("\"\"\"\nRoses are red\nViolets are blue\"\"\""),
        "Roses are red\nViolets are blue"
    );

    // A backslash at the end of a line eats the newline and the indentation.
    let folded = string(
        "\"\"\"\\\n     The quick brown \\\n     fox jumps over \\\n     the lazy dog.\\\n     \"\"\"",
    );
    assert_eq!(folded, "The quick brown fox jumps over the lazy dog.");

    // One or two quotes are content; three end the string.
    assert_eq!(
        string(r#""""Here are two quotation marks: "". Simple.""""#),
        "Here are two quotation marks: \"\". Simple."
    );
    assert_eq!(
        string(r#""""Here are three quotation marks: ""\".""""#),
        "Here are three quotation marks: \"\"\"."
    );
    assert_eq!(string("\"\"\"\"\"\""), "");
    assert_eq!(string("\"\"\"a\"\"\"\""), "a\"");
    assert_eq!(string("\"\"\"a\"\"\"\"\""), "a\"\"");
}

#[test]
fn multiline_literal_strings() {
    assert_eq!(
        string("'''\nThe first newline is\ntrimmed.'''"),
        "The first newline is\ntrimmed."
    );
    assert_eq!(
        string(r#"'''I [dw]on't need \d{2} apples'''"#),
        r"I [dw]on't need \d{2} apples"
    );
    assert_eq!(string("''''that's it'''"), "'that's it");
    assert_eq!(string("''''''"), "");
}

#[test]
fn crlf_is_normalized_inside_multiline_strings() {
    assert_eq!(string("\"\"\"\r\na\r\nb\"\"\""), "a\nb");
    assert_eq!(string("'''\r\na\r\nb'''"), "a\nb");
}

#[test]
fn tabs_are_allowed_in_strings() {
    assert_eq!(string("\"a\tb\""), "a\tb");
    assert_eq!(string("'a\tb'"), "a\tb");
}

// ----- numbers ------------------------------------------------------------

#[test]
fn integers() {
    for (input, expected) in [
        ("+99", 99),
        ("42", 42),
        ("0", 0),
        ("-17", -17),
        ("1_000", 1000),
        ("5_349_221", 5_349_221),
        ("0xDEADBEEF", 0xDEAD_BEEF),
        ("0xdead_beef", 0xDEAD_BEEF),
        ("0o01234567", 0o0123_4567),
        ("0o755", 0o755),
        ("0b11010110", 0b1101_0110),
        ("9223372036854775807", i64::MAX),
        ("-9223372036854775808", i64::MIN),
    ] {
        assert_eq!(value(input).as_integer(), Some(expected), "{input}");
    }
}

#[test]
fn floats() {
    for (input, expected) in [
        ("+1.0", 1.0),
        ("3.7415", 3.7415),
        ("-0.01", -0.01),
        ("5e+22", 5e22),
        ("1e06", 1e6),
        ("-2E-2", -2E-2),
        ("6.626e-34", 6.626e-34),
        ("224_617.445_991_228", 224_617.445_991_228),
        ("0.0", 0.0),
        ("-0.0", -0.0),
    ] {
        assert_eq!(value(input).as_float(), Some(expected), "{input}");
    }

    assert_eq!(value("inf").as_float(), Some(f64::INFINITY));
    assert_eq!(value("+inf").as_float(), Some(f64::INFINITY));
    assert_eq!(value("-inf").as_float(), Some(f64::NEG_INFINITY));
    for input in ["nan", "+nan", "-nan"] {
        assert!(
            value(input).as_float().expect("a float").is_nan(),
            "{input}"
        );
    }
}

#[test]
fn integers_and_floats_are_distinct_types() {
    assert!(value("1").is_integer());
    assert!(value("1.0").is_float());
    assert!(value("1e0").is_float());
    assert_eq!(value("1").as_float(), None);
}

#[test]
fn booleans() {
    assert_eq!(value("true").as_bool(), Some(true));
    assert_eq!(value("false").as_bool(), Some(false));
}

// ----- date-times ---------------------------------------------------------

#[test]
fn datetimes() {
    for (input, kind) in [
        ("1979-05-27T07:32:00Z", DatetimeKind::OffsetDatetime),
        ("1979-05-27T00:32:00-07:00", DatetimeKind::OffsetDatetime),
        ("1979-05-27 07:32:00Z", DatetimeKind::OffsetDatetime),
        (
            "1979-05-27T07:32:00.999999-07:00",
            DatetimeKind::OffsetDatetime,
        ),
        ("1979-05-27T07:32:00", DatetimeKind::LocalDatetime),
        ("1979-05-27T00:32:00.999999", DatetimeKind::LocalDatetime),
        ("1979-05-27", DatetimeKind::LocalDate),
        ("07:32:00", DatetimeKind::LocalTime),
        ("00:32:00.999999", DatetimeKind::LocalTime),
    ] {
        let value = value(input);
        assert_eq!(
            value.as_datetime().expect("a datetime").kind(),
            kind,
            "{input}"
        );
    }
}

#[test]
fn a_bare_date_in_an_array_is_not_followed_into_the_next_element() {
    let value = value("[1979-05-27, 1979-05-28]");
    let array = value.as_array().expect("an array");
    assert_eq!(array.len(), 2);
    assert_eq!(
        array[1]
            .as_datetime()
            .expect("a datetime")
            .date
            .expect("a date")
            .day,
        28
    );
}

// ----- arrays -------------------------------------------------------------

#[test]
fn arrays() {
    assert_eq!(value("[]").as_array().expect("an array").len(), 0);
    assert_eq!(value("[ 1, 2, 3 ]").as_array().expect("an array").len(), 3);
    assert_eq!(
        value(r#"[ "all", 'strings', """are the same""", '''type''' ]"#)
            .as_array()
            .expect("an array")
            .len(),
        4
    );
    // TOML 1.0 arrays may mix types.
    assert_eq!(
        value(r#"[ 0.1, 1, "a", true, [1] ]"#)
            .as_array()
            .expect("an array")
            .len(),
        5
    );
}

#[test]
fn arrays_may_span_lines_with_comments_and_a_trailing_comma() {
    let value = value("[\n  1, # the first\n  2, # the second\n\n  # a lone comment\n  3,\n]");
    assert_eq!(value.as_array().expect("an array").len(), 3);
}

// ----- tables -------------------------------------------------------------

#[test]
fn tables() {
    let table = doc(r#"
[table-1]
key1 = "some string"
key2 = 123

[table-2]
key1 = "another string"
"#);
    assert_eq!(table["table-1"]["key2"].as_integer(), Some(123));
    assert_eq!(table["table-2"]["key1"].as_str(), Some("another string"));
}

#[test]
fn nested_tables_may_arrive_out_of_order() {
    let table = doc("[x.y.z.w]\na = 1\n\n[x]\nb = 2\n");
    assert_eq!(table["x"]["y"]["z"]["w"]["a"].as_integer(), Some(1));
    assert_eq!(table["x"]["b"].as_integer(), Some(2));
}

#[test]
fn empty_tables_are_kept() {
    let table = doc("[a]\n[b]\n");
    assert!(table["a"].as_table().expect("a table").is_empty());
    assert_eq!(table.len(), 2);
}

#[test]
fn a_table_name_may_be_quoted() {
    let table = doc(r#"["a.b"]
c = 1
"#);
    assert_eq!(table["a.b"]["c"].as_integer(), Some(1));
}

// ----- inline tables ------------------------------------------------------

#[test]
fn inline_tables() {
    let table = doc(r#"
name = { first = "Tom", last = "Preston-Werner" }
point = { x = 1, y = 2 }
animal = { type.name = "pug" }
empty = {}
"#);
    assert_eq!(table["name"]["first"].as_str(), Some("Tom"));
    assert_eq!(table["animal"]["type"]["name"].as_str(), Some("pug"));
    assert!(table["empty"].as_table().expect("a table").is_empty());
}

#[test]
fn inline_tables_may_use_several_dotted_keys_under_one_parent() {
    let table = doc(r#"a = { b.c = 1, b.d = 2 }"#);
    assert_eq!(table["a"]["b"]["c"].as_integer(), Some(1));
    assert_eq!(table["a"]["b"]["d"].as_integer(), Some(2));
}

// ----- arrays of tables ---------------------------------------------------

#[test]
fn arrays_of_tables() {
    let table = doc(r#"
[[products]]
name = "Hammer"
sku = 738594937

[[products]]  # an empty one

[[products]]
name = "Nail"
sku = 284758393
"#);
    let products = table["products"].as_array().expect("an array");
    assert_eq!(products.len(), 3);
    assert_eq!(products[0]["name"].as_str(), Some("Hammer"));
    assert!(products[1].as_table().expect("a table").is_empty());
    assert_eq!(products[2]["sku"].as_integer(), Some(284_758_393));
}

#[test]
fn nested_arrays_of_tables() {
    let table = doc(r#"
[[fruits]]
name = "apple"

[fruits.physical]
color = "red"
shape = "round"

[[fruits.varieties]]
name = "red delicious"

[[fruits.varieties]]
name = "granny smith"

[[fruits]]
name = "banana"

[[fruits.varieties]]
name = "plantain"
"#);
    let fruits = table["fruits"].as_array().expect("an array");
    assert_eq!(fruits.len(), 2);
    assert_eq!(fruits[0]["physical"]["color"].as_str(), Some("red"));
    assert_eq!(
        fruits[0]["varieties"].as_array().expect("an array").len(),
        2
    );
    assert_eq!(fruits[1]["varieties"][0]["name"].as_str(), Some("plantain"));
}

// ----- document structure -------------------------------------------------

#[test]
fn comments_and_blank_lines() {
    let table =
        doc("# a full-line comment\n\n\nkey = \"value\" # a trailing comment\n\n# another\n");
    assert_eq!(table.len(), 1);
}

#[test]
fn an_empty_document_is_valid() {
    assert!(doc("").is_empty());
    assert!(doc("\n\n# nothing here\n").is_empty());
}

#[test]
fn a_leading_byte_order_mark_is_ignored() {
    assert_eq!(doc("\u{feff}a = 1").len(), 1);
}

#[test]
fn crlf_line_endings() {
    let table = doc("a = 1\r\n[t]\r\nb = 2\r\n");
    assert_eq!(table["t"]["b"].as_integer(), Some(2));
}

#[test]
fn a_document_need_not_end_with_a_newline() {
    assert_eq!(doc("a = 1").len(), 1);
}

#[test]
fn values_may_nest_to_the_documented_limit() {
    let input = format!("a = {}1{}", "[".repeat(128), "]".repeat(128));
    assert!(doc(&input).contains_key("a"));
}

// ----- TOML 1.1 ------------------------------------------------------------

#[test]
fn inline_tables_may_span_lines_with_a_trailing_comma() {
    let table = doc(
        "tbl = {\n    key      = \"a string\",\n    # a comment\n    moar-tbl = {\n        key = 1,\n    },\n}\n",
    );
    assert_eq!(table["tbl"]["key"].as_str(), Some("a string"));
    assert_eq!(table["tbl"]["moar-tbl"]["key"].as_integer(), Some(1));

    assert_eq!(doc("a = { b = 1, }")["a"]["b"].as_integer(), Some(1));
    assert!(doc("a = {\n}")["a"].as_table().expect("a table").is_empty());
    assert!(
        doc("a = {\n\n# nothing\n\n}")["a"]
            .as_table()
            .expect("a table")
            .is_empty()
    );
}

#[test]
fn the_new_string_escapes() {
    // \xHH reaches the first 256 codepoints; \e is the escape character.
    assert_eq!(
        string(r#""null byte: \x00; letter a: \x61""#),
        "null byte: \u{0}; letter a: a"
    );
    assert_eq!(string(r#""\e[""#), "\u{1b}[");
    assert_eq!(string(r#""\xff""#), "\u{ff}");
    // ...in multi-line strings too.
    assert_eq!(string("\"\"\"\\x41\\e\"\"\""), "A\u{1b}");
    // ...and in keys, which are basic strings.
    assert!(doc(r#""\x41" = 1"#).contains_key("A"));
}

#[test]
fn seconds_are_optional_in_times() {
    for (input, expected) in [
        ("2010-02-03 14:15", "2010-02-03T14:15:00"),
        ("2010-02-03T14:15Z", "2010-02-03T14:15:00Z"),
        ("2010-02-03T14:15-07:00", "2010-02-03T14:15:00-07:00"),
        ("14:15", "14:15:00"),
    ] {
        let value = value(input);
        let datetime = value.as_datetime().expect("a datetime");
        assert_eq!(datetime.to_string(), expected, "{input}");
    }
}

#[test]
fn a_dotted_key_may_extend_a_table_a_header_only_created() {
    // `[a.b.d]` creates `a.b` on its way to `a.b.d`, but does not *define* it,
    // so a dotted key may still write into it.
    let table = doc("[a.b.d]\nx = 1\n\n[a]\nb.c = 3\n");
    assert_eq!(table["a"]["b"]["c"].as_integer(), Some(3));
    assert_eq!(table["a"]["b"]["d"]["x"].as_integer(), Some(1));

    // ...and a header may still define sub-tables underneath it afterwards.
    let table = doc("[a.b.d]\n[a]\nb.c = 3\n\n[a.b.e]\ny = 2\n");
    assert_eq!(table["a"]["b"]["e"]["y"].as_integer(), Some(2));
}