2019-07-30 12:14:17 -05:00
|
|
|
extern crate serde;
|
|
|
|
extern crate toml;
|
|
|
|
|
|
|
|
use serde::{de, Deserialize};
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
macro_rules! bad {
|
|
|
|
($toml:expr, $ty:ty, $msg:expr) => {
|
|
|
|
match toml::from_str::<$ty>($toml) {
|
|
|
|
Ok(s) => panic!("parsed to: {:#?}", s),
|
|
|
|
Err(e) => assert_eq!(e.to_string(), $msg),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, PartialEq)]
|
|
|
|
struct Parent<T> {
|
|
|
|
p_a: T,
|
|
|
|
p_b: Vec<Child<T>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, PartialEq)]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
struct Child<T> {
|
|
|
|
c_a: T,
|
|
|
|
c_b: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum CasedString {
|
|
|
|
Lowercase(String),
|
|
|
|
Uppercase(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> de::Deserialize<'de> for CasedString {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: de::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
struct CasedStringVisitor;
|
|
|
|
|
|
|
|
impl<'de> de::Visitor<'de> for CasedStringVisitor {
|
|
|
|
type Value = CasedString;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: de::Error,
|
|
|
|
{
|
|
|
|
if s.is_empty() {
|
|
|
|
Err(de::Error::invalid_length(0, &"a non-empty string"))
|
|
|
|
} else if s.chars().all(|x| x.is_ascii_lowercase()) {
|
|
|
|
Ok(CasedString::Lowercase(s.to_string()))
|
|
|
|
} else if s.chars().all(|x| x.is_ascii_uppercase()) {
|
|
|
|
Ok(CasedString::Uppercase(s.to_string()))
|
|
|
|
} else {
|
|
|
|
Err(de::Error::invalid_value(
|
|
|
|
de::Unexpected::Str(s),
|
|
|
|
&"all lowercase or all uppercase",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize_any(CasedStringVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_errors() {
|
|
|
|
toml::from_str::<Parent<CasedString>>(
|
|
|
|
"
|
|
|
|
p_a = 'a'
|
|
|
|
p_b = [{c_a = 'a', c_b = 'c'}]
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Custom error at p_b value.
|
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = ''
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
",
|
|
|
|
Parent<CasedString>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"invalid length 0, expected a non-empty string for key `p_a` at line 2 column 19"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Missing field in table.
|
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = 'a'
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
",
|
|
|
|
Parent<CasedString>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"missing field `p_b` at line 1 column 1"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Invalid type in p_b.
|
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = 'a'
|
|
|
|
p_b = 1
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
",
|
|
|
|
Parent<CasedString>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"invalid type: integer `1`, expected a sequence for key `p_b` at line 3 column 19"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sub-table in Vec is missing a field.
|
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = 'a'
|
|
|
|
p_b = [
|
|
|
|
{c_a = 'a'}
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
]
|
|
|
|
",
|
|
|
|
Parent<CasedString>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"missing field `c_b` for key `p_b` at line 4 column 17"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sub-table in Vec has a field with a bad value.
|
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = 'a'
|
|
|
|
p_b = [
|
|
|
|
{c_a = 'a', c_b = '*'}
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
]
|
|
|
|
",
|
|
|
|
Parent<CasedString>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"invalid value: string \"*\", expected all lowercase or all uppercase for key `p_b` at line 4 column 35"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sub-table in Vec is missing a field.
|
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = 'a'
|
|
|
|
p_b = [
|
|
|
|
{c_a = 'a', c_b = 'b'},
|
|
|
|
{c_a = 'aa'}
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
]
|
|
|
|
",
|
|
|
|
Parent<CasedString>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"missing field `c_b` for key `p_b` at line 5 column 17"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sub-table in the middle of a Vec is missing a field.
|
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = 'a'
|
|
|
|
p_b = [
|
|
|
|
{c_a = 'a', c_b = 'b'},
|
|
|
|
{c_a = 'aa'},
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
{c_a = 'aaa', c_b = 'bbb'},
|
|
|
|
]
|
|
|
|
",
|
|
|
|
Parent<CasedString>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"missing field `c_b` for key `p_b` at line 5 column 17"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sub-table in the middle of a Vec has a field with a bad value.
|
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = 'a'
|
|
|
|
p_b = [
|
|
|
|
{c_a = 'a', c_b = 'b'},
|
|
|
|
{c_a = 'aa', c_b = 1},
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
{c_a = 'aaa', c_b = 'bbb'},
|
|
|
|
]
|
|
|
|
",
|
|
|
|
Parent<CasedString>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"invalid type: integer `1`, expected a string for key `p_b` at line 5 column 36"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sub-table in the middle of a Vec has an extra field.
|
2019-07-30 12:20:18 -05:00
|
|
|
// FIXME: This location could be better.
|
2019-07-30 12:14:17 -05:00
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = 'a'
|
|
|
|
p_b = [
|
|
|
|
{c_a = 'a', c_b = 'b'},
|
|
|
|
{c_a = 'aa', c_b = 'bb', c_d = 'd'},
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
{c_a = 'aaa', c_b = 'bbb'},
|
2019-07-30 12:20:18 -05:00
|
|
|
{c_a = 'aaaa', c_b = 'bbbb'},
|
2019-07-30 12:14:17 -05:00
|
|
|
]
|
|
|
|
",
|
|
|
|
Parent<CasedString>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"unknown field `c_d`, expected `c_a` or `c_b` for key `p_b` at line 5 column 17"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sub-table in the middle of a Vec is missing a field.
|
2019-07-30 12:20:18 -05:00
|
|
|
// FIXME: This location is pretty off.
|
2019-07-30 12:14:17 -05:00
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = 'a'
|
|
|
|
[[p_b]]
|
|
|
|
c_a = 'a'
|
|
|
|
c_b = 'b'
|
|
|
|
[[p_b]]
|
|
|
|
c_a = 'aa'
|
2019-07-30 12:20:18 -05:00
|
|
|
# c_b = 'bb' # <- missing field
|
2019-07-30 12:14:17 -05:00
|
|
|
[[p_b]]
|
|
|
|
c_a = 'aaa'
|
|
|
|
c_b = 'bbb'
|
2019-07-30 12:20:18 -05:00
|
|
|
[[p_b]]
|
|
|
|
# ^
|
|
|
|
c_a = 'aaaa'
|
|
|
|
c_b = 'bbbb'
|
2019-07-30 12:14:17 -05:00
|
|
|
",
|
|
|
|
Parent<CasedString>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"missing field `c_b` for key `p_b` at line 12 column 13"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sub-table in the middle of a Vec has a field with a bad value.
|
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = 'a'
|
|
|
|
[[p_b]]
|
|
|
|
c_a = 'a'
|
|
|
|
c_b = 'b'
|
|
|
|
[[p_b]]
|
|
|
|
c_a = 'aa'
|
|
|
|
c_b = '*'
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
[[p_b]]
|
|
|
|
c_a = 'aaa'
|
|
|
|
c_b = 'bbb'
|
|
|
|
",
|
|
|
|
Parent<CasedString>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"invalid value: string \"*\", expected all lowercase or all uppercase for key `p_b.c_b` at line 8 column 19"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Sub-table in the middle of a Vec has an extra field.
|
2019-07-30 12:20:18 -05:00
|
|
|
// FIXME: This location is pretty off.
|
2019-07-30 12:14:17 -05:00
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = 'a'
|
|
|
|
[[p_b]]
|
|
|
|
c_a = 'a'
|
|
|
|
c_b = 'b'
|
|
|
|
[[p_b]]
|
|
|
|
c_a = 'aa'
|
|
|
|
c_d = 'dd' # unknown field
|
|
|
|
[[p_b]]
|
|
|
|
c_a = 'aaa'
|
|
|
|
c_b = 'bbb'
|
2019-07-30 12:20:18 -05:00
|
|
|
[[p_b]]
|
|
|
|
# ^
|
|
|
|
c_a = 'aaaa'
|
|
|
|
c_b = 'bbbb'
|
2019-07-30 12:14:17 -05:00
|
|
|
",
|
|
|
|
Parent<CasedString>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"unknown field `c_d`, expected `c_a` or `c_b` for key `p_b` at line 12 column 13"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn serde_derive_deserialize_errors() {
|
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = ''
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
",
|
|
|
|
Parent<String>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"missing field `p_b` at line 1 column 1"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = ''
|
|
|
|
p_b = [
|
|
|
|
{c_a = ''}
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
]
|
|
|
|
",
|
|
|
|
Parent<String>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"missing field `c_b` for key `p_b` at line 4 column 17"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = ''
|
|
|
|
p_b = [
|
|
|
|
{c_a = '', c_b = 1}
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
]
|
|
|
|
",
|
|
|
|
Parent<String>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"invalid type: integer `1`, expected a string for key `p_b` at line 4 column 34"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
2019-07-30 12:20:18 -05:00
|
|
|
// FIXME: This location could be better.
|
2019-07-30 12:14:17 -05:00
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = ''
|
|
|
|
p_b = [
|
|
|
|
{c_a = '', c_b = '', c_d = ''},
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
]
|
|
|
|
",
|
|
|
|
Parent<String>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"unknown field `c_d`, expected `c_a` or `c_b` for key `p_b` at line 4 column 17"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
bad!(
|
|
|
|
"
|
|
|
|
p_a = 'a'
|
|
|
|
p_b = [
|
|
|
|
{c_a = '', c_b = 1, c_d = ''},
|
2019-07-30 12:20:18 -05:00
|
|
|
# ^
|
2019-07-30 12:14:17 -05:00
|
|
|
]
|
|
|
|
",
|
|
|
|
Parent<String>,
|
2019-07-30 12:20:18 -05:00
|
|
|
"invalid type: integer `1`, expected a string for key `p_b` at line 4 column 34"
|
2019-07-30 12:14:17 -05:00
|
|
|
);
|
|
|
|
}
|
2019-08-13 15:48:54 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_handles_crlf() {
|
|
|
|
bad!(
|
|
|
|
"\r\n\
|
2019-08-28 09:58:15 -05:00
|
|
|
[t1]\r\n\
|
|
|
|
[t2]\r\n\
|
|
|
|
a = 1\r\n\
|
|
|
|
a = 2\r\n\
|
|
|
|
",
|
2019-08-13 15:48:54 -05:00
|
|
|
toml::Value,
|
|
|
|
"duplicate key: `a` for key `t2` at line 3 column 1"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Should be the same as above.
|
|
|
|
bad!(
|
|
|
|
"\n\
|
2019-08-28 09:58:15 -05:00
|
|
|
[t1]\n\
|
|
|
|
[t2]\n\
|
|
|
|
a = 1\n\
|
|
|
|
a = 2\n\
|
|
|
|
",
|
2019-08-13 15:48:54 -05:00
|
|
|
toml::Value,
|
|
|
|
"duplicate key: `a` for key `t2` at line 3 column 1"
|
|
|
|
);
|
|
|
|
}
|