Merge pull request #373 from mgsloan/allow-empty-table-keys

Allow empty table keys
This commit is contained in:
Eric Huss 2021-04-26 13:32:09 -07:00 committed by GitHub
commit d31787fa3b
8 changed files with 19 additions and 14 deletions

View file

@ -1993,7 +1993,6 @@ impl<'a> Deserializer<'a> {
expected, expected,
found, found,
} => self.error(at, ErrorKind::Wanted { expected, found }), } => self.error(at, ErrorKind::Wanted { expected, found }),
TokenError::EmptyTableKey(at) => self.error(at, ErrorKind::EmptyTableKey),
TokenError::MultilineStringKey(at) => self.error(at, ErrorKind::MultilineStringKey), TokenError::MultilineStringKey(at) => self.error(at, ErrorKind::MultilineStringKey),
} }
} }

View file

@ -509,10 +509,11 @@ impl<'a> Serializer<'a> {
} }
fn escape_key(&mut self, key: &str) -> Result<(), Error> { fn escape_key(&mut self, key: &str) -> Result<(), Error> {
let ok = key.chars().all(|c| match c { let ok = key.len() > 0
'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' => true, && key.chars().all(|c| match c {
_ => false, 'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' => true,
}); _ => false,
});
if ok { if ok {
write!(self.dst, "{}", key).map_err(ser::Error::custom)?; write!(self.dst, "{}", key).map_err(ser::Error::custom)?;
} else { } else {

View file

@ -56,7 +56,6 @@ pub enum Error {
UnterminatedString(usize), UnterminatedString(usize),
NewlineInTableKey(usize), NewlineInTableKey(usize),
MultilineStringKey(usize), MultilineStringKey(usize),
EmptyTableKey(usize),
Wanted { Wanted {
at: usize, at: usize,
expected: &'static str, expected: &'static str,
@ -194,9 +193,6 @@ impl<'a> Tokenizer<'a> {
if multiline { if multiline {
return Err(Error::MultilineStringKey(offset)); return Err(Error::MultilineStringKey(offset));
} }
if val == "" {
return Err(Error::EmptyTableKey(offset));
}
match src.find('\n') { match src.find('\n') {
None => Ok((span, val)), None => Ok((span, val)),
Some(i) => Err(Error::NewlineInTableKey(offset + i)), Some(i) => Err(Error::NewlineInTableKey(offset + i)),

View file

@ -14,7 +14,7 @@ fn bad() {
bad!("a = 01", "invalid number at line 1 column 6"); bad!("a = 01", "invalid number at line 1 column 6");
bad!("a = 1__1", "invalid number at line 1 column 5"); bad!("a = 1__1", "invalid number at line 1 column 5");
bad!("a = 1_", "invalid number at line 1 column 5"); bad!("a = 1_", "invalid number at line 1 column 5");
bad!("''", "empty table key found at line 1 column 1"); bad!("''", "expected an equals, found eof at line 1 column 3");
bad!("a = 9e99999", "invalid number at line 1 column 5"); bad!("a = 9e99999", "invalid number at line 1 column 5");
bad!( bad!(

View file

@ -342,12 +342,14 @@ fn bad_keys() {
"key|=3", "key|=3",
"unexpected character found: `|` at line 1 column 4" "unexpected character found: `|` at line 1 column 4"
); );
bad!("\"\"=3", "empty table key found at line 1 column 1");
bad!( bad!(
"=3", "=3",
"expected a table key, found an equals at line 1 column 1" "expected a table key, found an equals at line 1 column 1"
); );
bad!("\"\"|=3", "empty table key found at line 1 column 1"); bad!(
"\"\"|=3",
"unexpected character found: `|` at line 1 column 3"
);
bad!("\"\n\"|=3", "newline in string found at line 1 column 2"); bad!("\"\n\"|=3", "newline in string found at line 1 column 2");
bad!( bad!(
"\"\r\"|=3", "\"\r\"|=3",
@ -381,12 +383,10 @@ fn bad_table_names() {
"[.]", "[.]",
"expected a table key, found a period at line 1 column 2" "expected a table key, found a period at line 1 column 2"
); );
bad!("[\"\".\"\"]", "empty table key found at line 1 column 2");
bad!( bad!(
"[a.]", "[a.]",
"expected a table key, found a right bracket at line 1 column 4" "expected a table key, found a right bracket at line 1 column 4"
); );
bad!("[\"\"]", "empty table key found at line 1 column 2");
bad!("[!]", "unexpected character found: `!` at line 1 column 2"); bad!("[!]", "unexpected character found: `!` at line 1 column 2");
bad!("[\"\n\"]", "newline in string found at line 1 column 3"); bad!("[\"\n\"]", "newline in string found at line 1 column 3");
bad!( bad!(

View file

@ -231,6 +231,11 @@ test!(
include_str!("valid/key-with-pound.toml"), include_str!("valid/key-with-pound.toml"),
include_str!("valid/key-with-pound.json") include_str!("valid/key-with-pound.json")
); );
test!(
key_empty,
include_str!("valid/key-empty.toml"),
include_str!("valid/key-empty.json")
);
test!( test!(
long_float, long_float,
include_str!("valid/long-float.toml"), include_str!("valid/long-float.toml"),

View file

@ -0,0 +1,3 @@
{
"": {"type": "integer", "value": "1"}
}

View file

@ -0,0 +1 @@
"" = 1