-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtoml.rs
115 lines (97 loc) · 2.5 KB
/
toml.rs
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
#![cfg(feature = "toml")]
extern crate alloc;
use alloc::collections::BTreeMap;
use nanoserde::Toml;
use nanoserde::TomlParser;
#[test]
fn de_toml() {
let toml_str = r#"
[[array]]
# a
name = "a" #aasdf
[[array]]
name = "b#asdf"
data = 123
[[array]]
name = "c"
[[other_array]]
hmm = "a"
[[other_array]]
hmm = "b"
"#;
let toml = TomlParser::parse(toml_str).unwrap();
assert_eq!(toml["array"].arr()[0]["name"].str(), "a");
assert_eq!(toml["array"].arr()[1]["name"].str(), "b#asdf");
assert_eq!(toml["array"].arr()[1]["data"].num(), 123.);
assert_eq!(toml["other_array"].arr()[0]["hmm"].str(), "a");
assert_eq!(toml["other_array"].arr()[1]["hmm"].str(), "b");
}
#[test]
fn comment() {
let data = r#"
# This is a full-line comment
key = "value" # This is a comment at the end of a line
another = " # This is not a comment"
"#;
let toml = TomlParser::parse(data).unwrap();
assert_eq!(toml["key"].str(), "value");
assert_eq!(toml["another"].str(), " # This is not a comment");
}
#[test]
fn key_without_value() {
let data = r#"
key = # INVALID
"#;
assert!(TomlParser::parse(data).is_err());
}
#[test]
fn assert_specific_toml_types() {
let data = r#"
num = 3.14
str = "quoth the raven"
simple_arr = [1, 2, 3, 4]
boolean = false
date = 1979-05-27
"#;
assert_eq!(TomlParser::parse(data).unwrap()["num"].num(), 3.14);
assert_eq!(
TomlParser::parse(data).unwrap()["str"].str(),
"quoth the raven"
);
assert_eq!(TomlParser::parse(data).unwrap()["boolean"].boolean(), false);
assert_eq!(
TomlParser::parse(data).unwrap()["date"].date(),
"1979-05-27".to_string()
);
assert_eq!(
TomlParser::parse(data).unwrap()["simple_arr"].simple_arr(),
&vec![
Toml::Num(1.0),
Toml::Num(2.0),
Toml::Num(3.0),
Toml::Num(4.0)
]
);
}
#[test]
fn toml_key_chars() {
let toml_str = r#"
[foo.bar.baz]
123abc456def = "myval"
-inf = 0
2024-04-30 = 100
½ = 0.5
"#;
assert_eq!(
TomlParser::parse(toml_str).unwrap(),
BTreeMap::from([
(
"foo.bar.baz.123abc456def".to_string(),
Toml::Str("myval".to_string())
),
("foo.bar.baz.-inf".to_string(), Toml::Num(0.0)),
("foo.bar.baz.2024-04-30".to_string(), Toml::Num(100.0)),
("foo.bar.baz.½".to_string(), Toml::Num(0.5))
])
);
}