Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #373 from mgsloan/allow-empty-table-keys
Browse files Browse the repository at this point in the history
Allow empty table keys
  • Loading branch information
ehuss committed Apr 26, 2021
2 parents 1928707 + f74715c commit a6420b6
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/de.rs
Expand Up @@ -1993,7 +1993,6 @@ impl<'a> Deserializer<'a> {
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),
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/ser.rs
Expand Up @@ -509,10 +509,11 @@ impl<'a> Serializer<'a> {
}

fn escape_key(&mut self, key: &str) -> Result<(), Error> {
let ok = key.chars().all(|c| match c {
'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' => true,
_ => false,
});
let ok = key.len() > 0
&& key.chars().all(|c| match c {
'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' => true,
_ => false,
});
if ok {
write!(self.dst, "{}", key).map_err(ser::Error::custom)?;
} else {
Expand Down
4 changes: 0 additions & 4 deletions src/tokens.rs
Expand Up @@ -56,7 +56,6 @@ pub enum Error {
UnterminatedString(usize),
NewlineInTableKey(usize),
MultilineStringKey(usize),
EmptyTableKey(usize),
Wanted {
at: usize,
expected: &'static str,
Expand Down Expand Up @@ -194,9 +193,6 @@ impl<'a> Tokenizer<'a> {
if multiline {
return Err(Error::MultilineStringKey(offset));
}
if val == "" {
return Err(Error::EmptyTableKey(offset));
}
match src.find('\n') {
None => Ok((span, val)),
Some(i) => Err(Error::NewlineInTableKey(offset + i)),
Expand Down
2 changes: 1 addition & 1 deletion test-suite/tests/invalid-misc.rs
Expand Up @@ -14,7 +14,7 @@ fn bad() {
bad!("a = 01", "invalid number at line 1 column 6");
bad!("a = 1__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!(
Expand Down
8 changes: 4 additions & 4 deletions test-suite/tests/parser.rs
Expand Up @@ -342,12 +342,14 @@ fn bad_keys() {
"key|=3",
"unexpected character found: `|` at line 1 column 4"
);
bad!("\"\"=3", "empty table key found at line 1 column 1");
bad!(
"=3",
"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!(
"\"\r\"|=3",
Expand Down Expand Up @@ -381,12 +383,10 @@ fn bad_table_names() {
"[.]",
"expected a table key, found a period at line 1 column 2"
);
bad!("[\"\".\"\"]", "empty table key found at line 1 column 2");
bad!(
"[a.]",
"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!("[\"\n\"]", "newline in string found at line 1 column 3");
bad!(
Expand Down
5 changes: 5 additions & 0 deletions test-suite/tests/valid.rs
Expand Up @@ -231,6 +231,11 @@ test!(
include_str!("valid/key-with-pound.toml"),
include_str!("valid/key-with-pound.json")
);
test!(
key_empty,
include_str!("valid/key-empty.toml"),
include_str!("valid/key-empty.json")
);
test!(
long_float,
include_str!("valid/long-float.toml"),
Expand Down
3 changes: 3 additions & 0 deletions test-suite/tests/valid/key-empty.json
@@ -0,0 +1,3 @@
{
"": {"type": "integer", "value": "1"}
}
1 change: 1 addition & 0 deletions test-suite/tests/valid/key-empty.toml
@@ -0,0 +1 @@
"" = 1

0 comments on commit a6420b6

Please sign in to comment.