2015-01-15 14:44:05 -06:00
|
|
|
extern crate toml;
|
2017-07-27 17:37:30 -05:00
|
|
|
extern crate serde;
|
2017-01-29 18:53:20 -06:00
|
|
|
extern crate serde_json;
|
2014-06-20 19:01:38 -05:00
|
|
|
|
2017-07-27 17:37:30 -05:00
|
|
|
use toml::{Value as Toml, to_string_pretty};
|
|
|
|
use serde::ser::Serialize;
|
2017-01-29 18:53:20 -06:00
|
|
|
use serde_json::Value as Json;
|
2014-06-20 19:01:38 -05:00
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
fn to_json(toml: toml::Value) -> Json {
|
2014-12-03 01:57:17 -06:00
|
|
|
fn doit(s: &str, json: Json) -> Json {
|
2017-01-29 18:53:20 -06:00
|
|
|
let mut map = serde_json::Map::new();
|
|
|
|
map.insert("type".to_string(), Json::String(s.to_string()));
|
|
|
|
map.insert("value".to_string(), json);
|
2014-12-03 01:57:17 -06:00
|
|
|
Json::Object(map)
|
2014-06-20 19:01:38 -05:00
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
|
2014-06-20 19:01:38 -05:00
|
|
|
match toml {
|
2017-01-29 18:53:20 -06:00
|
|
|
Toml::String(s) => doit("string", Json::String(s)),
|
|
|
|
Toml::Integer(i) => doit("integer", Json::String(i.to_string())),
|
|
|
|
Toml::Float(f) => doit("float", Json::String({
|
2015-01-27 21:29:58 -06:00
|
|
|
let s = format!("{:.15}", f);
|
|
|
|
let s = format!("{}", s.trim_right_matches('0'));
|
2017-03-30 06:40:27 -05:00
|
|
|
if s.ends_with('.') {format!("{}0", s)} else {s}
|
2014-06-20 19:01:38 -05:00
|
|
|
})),
|
2017-01-29 18:53:20 -06:00
|
|
|
Toml::Boolean(b) => doit("bool", Json::String(format!("{}", b))),
|
|
|
|
Toml::Datetime(s) => doit("datetime", Json::String(s.to_string())),
|
|
|
|
Toml::Array(arr) => {
|
2015-01-27 21:29:58 -06:00
|
|
|
let is_table = match arr.first() {
|
2017-01-29 18:53:20 -06:00
|
|
|
Some(&Toml::Table(..)) => true,
|
2014-06-20 19:01:38 -05:00
|
|
|
_ => false,
|
|
|
|
};
|
2014-12-03 01:57:17 -06:00
|
|
|
let json = Json::Array(arr.into_iter().map(to_json).collect());
|
2014-06-20 19:01:38 -05:00
|
|
|
if is_table {json} else {doit("array", json)}
|
|
|
|
}
|
2017-01-29 18:53:20 -06:00
|
|
|
Toml::Table(table) => {
|
|
|
|
let mut map = serde_json::Map::new();
|
|
|
|
for (k, v) in table {
|
|
|
|
map.insert(k, to_json(v));
|
|
|
|
}
|
|
|
|
Json::Object(map)
|
|
|
|
}
|
2014-06-20 19:01:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 17:37:30 -05:00
|
|
|
fn run_pretty(toml: Toml) {
|
|
|
|
// Assert toml == json
|
|
|
|
println!("### pretty round trip parse.");
|
2018-07-11 01:29:47 -05:00
|
|
|
|
2017-07-27 17:37:30 -05:00
|
|
|
// standard pretty
|
|
|
|
let toml_raw = to_string_pretty(&toml).expect("to string");
|
|
|
|
let toml2 = toml_raw.parse().expect("from string");
|
|
|
|
assert_eq!(toml, toml2);
|
|
|
|
|
|
|
|
// pretty with indent 2
|
|
|
|
let mut result = String::with_capacity(128);
|
|
|
|
{
|
|
|
|
let mut serializer = toml::Serializer::pretty(&mut result);
|
|
|
|
serializer.pretty_array_indent(2);
|
|
|
|
toml.serialize(&mut serializer).expect("to string");
|
|
|
|
}
|
|
|
|
assert_eq!(toml, result.parse().expect("from str"));
|
|
|
|
result.clear();
|
|
|
|
{
|
|
|
|
let mut serializer = toml::Serializer::new(&mut result);
|
|
|
|
serializer.pretty_array_trailing_comma(false);
|
|
|
|
toml.serialize(&mut serializer).expect("to string");
|
|
|
|
}
|
|
|
|
assert_eq!(toml, result.parse().expect("from str"));
|
|
|
|
result.clear();
|
|
|
|
{
|
|
|
|
let mut serializer = toml::Serializer::pretty(&mut result);
|
|
|
|
serializer.pretty_string(false);
|
|
|
|
toml.serialize(&mut serializer).expect("to string");
|
|
|
|
assert_eq!(toml, toml2);
|
|
|
|
}
|
|
|
|
assert_eq!(toml, result.parse().expect("from str"));
|
|
|
|
result.clear();
|
|
|
|
{
|
|
|
|
let mut serializer = toml::Serializer::pretty(&mut result);
|
|
|
|
serializer.pretty_array(false);
|
|
|
|
toml.serialize(&mut serializer).expect("to string");
|
|
|
|
assert_eq!(toml, toml2);
|
|
|
|
}
|
|
|
|
assert_eq!(toml, result.parse().expect("from str"));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(toml_raw: &str, json_raw: &str) {
|
|
|
|
println!("parsing:\n{}", toml_raw);
|
|
|
|
let toml: Toml = toml_raw.parse().unwrap();
|
|
|
|
let json: Json = json_raw.parse().unwrap();
|
2014-06-20 19:01:38 -05:00
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
// Assert toml == json
|
2015-02-03 00:54:46 -06:00
|
|
|
let toml_json = to_json(toml.clone());
|
2014-06-20 19:01:38 -05:00
|
|
|
assert!(json == toml_json,
|
|
|
|
"expected\n{}\ngot\n{}\n",
|
2017-01-29 18:53:20 -06:00
|
|
|
serde_json::to_string_pretty(&json).unwrap(),
|
|
|
|
serde_json::to_string_pretty(&toml_json).unwrap());
|
2015-02-03 00:54:46 -06:00
|
|
|
|
2017-01-29 18:53:20 -06:00
|
|
|
// Assert round trip
|
|
|
|
println!("round trip parse: {}", toml);
|
|
|
|
let toml2 = toml.to_string().parse().unwrap();
|
|
|
|
assert_eq!(toml, toml2);
|
2017-07-27 17:37:30 -05:00
|
|
|
run_pretty(toml);
|
2014-06-20 19:01:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! test( ($name:ident, $toml:expr, $json:expr) => (
|
|
|
|
#[test]
|
|
|
|
fn $name() { run($toml, $json); }
|
2014-12-18 15:48:34 -06:00
|
|
|
) );
|
2014-06-20 19:01:38 -05:00
|
|
|
|
|
|
|
test!(array_empty,
|
|
|
|
include_str!("valid/array-empty.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/array-empty.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(array_nospaces,
|
|
|
|
include_str!("valid/array-nospaces.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/array-nospaces.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(arrays_hetergeneous,
|
|
|
|
include_str!("valid/arrays-hetergeneous.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/arrays-hetergeneous.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(arrays,
|
|
|
|
include_str!("valid/arrays.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/arrays.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(arrays_nested,
|
|
|
|
include_str!("valid/arrays-nested.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/arrays-nested.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(empty,
|
|
|
|
include_str!("valid/empty.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/empty.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(bool,
|
|
|
|
include_str!("valid/bool.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/bool.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(datetime,
|
|
|
|
include_str!("valid/datetime.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/datetime.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(example,
|
|
|
|
include_str!("valid/example.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/example.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(float,
|
|
|
|
include_str!("valid/float.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/float.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(implicit_and_explicit_after,
|
|
|
|
include_str!("valid/implicit-and-explicit-after.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/implicit-and-explicit-after.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(implicit_and_explicit_before,
|
|
|
|
include_str!("valid/implicit-and-explicit-before.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/implicit-and-explicit-before.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(implicit_groups,
|
|
|
|
include_str!("valid/implicit-groups.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/implicit-groups.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(integer,
|
|
|
|
include_str!("valid/integer.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/integer.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(key_equals_nospace,
|
|
|
|
include_str!("valid/key-equals-nospace.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/key-equals-nospace.json"));
|
2016-04-02 03:15:59 -05:00
|
|
|
test!(key_space,
|
|
|
|
include_str!("valid/key-space.toml"),
|
|
|
|
include_str!("valid/key-space.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(key_special_chars,
|
|
|
|
include_str!("valid/key-special-chars.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/key-special-chars.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(key_with_pound,
|
|
|
|
include_str!("valid/key-with-pound.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/key-with-pound.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(long_float,
|
|
|
|
include_str!("valid/long-float.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/long-float.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(long_integer,
|
|
|
|
include_str!("valid/long-integer.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/long-integer.json"));
|
2016-04-02 03:15:59 -05:00
|
|
|
test!(multiline_string,
|
|
|
|
include_str!("valid/multiline-string.toml"),
|
|
|
|
include_str!("valid/multiline-string.json"));
|
|
|
|
test!(raw_multiline_string,
|
|
|
|
include_str!("valid/raw-multiline-string.toml"),
|
|
|
|
include_str!("valid/raw-multiline-string.json"));
|
|
|
|
test!(raw_string,
|
|
|
|
include_str!("valid/raw-string.toml"),
|
|
|
|
include_str!("valid/raw-string.json"));
|
2014-07-18 05:19:21 -05:00
|
|
|
test!(string_empty,
|
|
|
|
include_str!("valid/string-empty.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/string-empty.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(string_escapes,
|
|
|
|
include_str!("valid/string-escapes.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/string-escapes.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(string_simple,
|
|
|
|
include_str!("valid/string-simple.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/string-simple.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(string_with_pound,
|
|
|
|
include_str!("valid/string-with-pound.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/string-with-pound.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(table_array_implicit,
|
|
|
|
include_str!("valid/table-array-implicit.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/table-array-implicit.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(table_array_many,
|
|
|
|
include_str!("valid/table-array-many.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/table-array-many.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(table_array_nest,
|
|
|
|
include_str!("valid/table-array-nest.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/table-array-nest.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(table_array_one,
|
|
|
|
include_str!("valid/table-array-one.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/table-array-one.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(table_empty,
|
|
|
|
include_str!("valid/table-empty.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/table-empty.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(table_sub_empty,
|
|
|
|
include_str!("valid/table-sub-empty.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/table-sub-empty.json"));
|
2017-07-26 23:08:22 -05:00
|
|
|
test!(table_multi_empty,
|
|
|
|
include_str!("valid/table-multi-empty.toml"),
|
|
|
|
include_str!("valid/table-multi-empty.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(table_whitespace,
|
|
|
|
include_str!("valid/table-whitespace.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/table-whitespace.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(table_with_pound,
|
|
|
|
include_str!("valid/table-with-pound.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/table-with-pound.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(unicode_escape,
|
|
|
|
include_str!("valid/unicode-escape.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/unicode-escape.json"));
|
2014-06-20 19:01:38 -05:00
|
|
|
test!(unicode_literal,
|
|
|
|
include_str!("valid/unicode-literal.toml"),
|
2014-12-18 15:48:34 -06:00
|
|
|
include_str!("valid/unicode-literal.json"));
|
2015-01-15 14:56:35 -06:00
|
|
|
test!(hard_example,
|
|
|
|
include_str!("valid/hard_example.toml"),
|
|
|
|
include_str!("valid/hard_example.json"));
|
|
|
|
test!(example2,
|
|
|
|
include_str!("valid/example2.toml"),
|
|
|
|
include_str!("valid/example2.json"));
|
|
|
|
test!(example3,
|
|
|
|
include_str!("valid/example-v0.3.0.toml"),
|
|
|
|
include_str!("valid/example-v0.3.0.json"));
|
2015-02-13 20:31:04 -06:00
|
|
|
test!(example4,
|
|
|
|
include_str!("valid/example-v0.4.0.toml"),
|
|
|
|
include_str!("valid/example-v0.4.0.json"));
|
2015-10-07 04:35:49 -05:00
|
|
|
test!(example_bom,
|
|
|
|
include_str!("valid/example-bom.toml"),
|
|
|
|
include_str!("valid/example.json"));
|
2017-06-01 04:29:11 -05:00
|
|
|
|
2017-06-01 14:35:39 -05:00
|
|
|
test!(datetime_truncate,
|
|
|
|
include_str!("valid/datetime-truncate.toml"),
|
|
|
|
include_str!("valid/datetime-truncate.json"));
|
2017-06-01 14:54:17 -05:00
|
|
|
test!(key_quote_newline,
|
|
|
|
include_str!("valid/key-quote-newline.toml"),
|
|
|
|
include_str!("valid/key-quote-newline.json"));
|
2017-06-01 04:29:11 -05:00
|
|
|
test!(table_array_nest_no_keys,
|
|
|
|
include_str!("valid/table-array-nest-no-keys.toml"),
|
|
|
|
include_str!("valid/table-array-nest-no-keys.json"));
|
2018-07-11 01:29:47 -05:00
|
|
|
test!(dotted_keys,
|
|
|
|
include_str!("valid/dotted-keys.toml"),
|
|
|
|
include_str!("valid/dotted-keys.json"));
|