Repository: toml-rs/toml (language: rust)

Issue:
Suggest quoted strings to users in errors
This is a regression compared to toml-rs as found by cargo's test suite

```
---- config::config_get_list stdout ----                                                                                                           
thread 'config::config_get_list' panicked at '                                                                                                     
                                                                                                                                                   
error: output did not match:                                                                                                                       
1        -error in environment variable `CARGO_BAD_ENV`: could not parse TOML list: invalid TOML value, did you mean to use a quoted string? at lin
e 1 column 8                                                                                                                                       
    1    +error in environment variable `CARGO_BAD_ENV`: could not parse TOML list: TOML parse error at line 1, column 8                           
    2    +  |                                                            
    3    +1 | value=[zzz]                                                
    4    +  |        ^                                                   
    5    +Unexpected `z`                                                 
    6    +Expected `a newline` or `#`                                    
```


Discussion / hints:

Fix patch:
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index 1b9c0e0c..525c5d37 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -9,7 +9,7 @@ mod document;
 mod errors;
 mod inline_table;
 mod key;
-mod numbers;
+pub(crate) mod numbers;
 pub(crate) mod strings;
 mod table;
 mod trivia;
diff --git a/src/parser/numbers.rs b/src/parser/numbers.rs
index cace325f..56045a72 100644
--- a/src/parser/numbers.rs
+++ b/src/parser/numbers.rs
@@ -15,6 +15,12 @@ parse!(boolean() -> bool, {
         (byte(FALSE[0]), range(&FALSE[1..]),),
     )).map(|p| p.0 == b't')
 });
+parse!(true_() -> bool, {
+    range(crate::parser::numbers::TRUE).map(|_| true)
+});
+parse!(false_() -> bool, {
+    range(crate::parser::numbers::FALSE).map(|_| false)
+});
 
 // ;; Integer
 
@@ -179,11 +185,11 @@ parse!(special_float() -> f64, {
 // inf = %x69.6e.66  ; inf
 pub(crate) const INF: &[u8] = b"inf";
 parse!(inf() -> f64, {
-    range(INF).map(|_| f64::INFINITY)
+    bytes(INF).map(|_| f64::INFINITY)
 });
 
 // nan = %x6e.61.6e  ; nan
 pub(crate) const NAN: &[u8] = b"nan";
 parse!(nan() -> f64, {
-    range(NAN).map(|_| f64::NAN)
+    bytes(NAN).map(|_| f64::NAN)
 });
diff --git a/src/parser/value.rs b/src/parser/value.rs
index dd3bc7d2..6fcbc75d 100644
--- a/src/parser/value.rs
+++ b/src/parser/value.rs
@@ -1,7 +1,7 @@
 use crate::parser::array::array;
 use crate::parser::datetime::date_time;
 use crate::parser::inline_table::inline_table;
-use crate::parser::numbers::{boolean, float, integer};
+use crate::parser::numbers::{float, integer};
 use crate::parser::strings::string;
 use crate::parser::trivia::from_utf8_unchecked;
 use crate::repr::{Formatted, Repr};
@@ -23,17 +23,36 @@ parse!(value() -> v::Value, {
             }),
             crate::parser::array::ARRAY_OPEN => array().map(v::Value::Array),
             crate::parser::inline_table::INLINE_TABLE_OPEN => inline_table().map(v::Value::InlineTable),
-            // Uncommon enough not to be worth optimizing at this time
-            _ => choice((
-                boolean()
-                    .map(v::Value::from),
-                date_time()
-                    .map(v::Value::from),
-                float()
-                    .map(v::Value::from),
-                integer()
-                    .map(v::Value::from),
-            )),
+            // Date/number starts
+            b'+' | b'-' | b'0'..=b'9' |
+            // Report as if they were numbers because its most likely a typo
+            b'.' | b'_' => {
+                // Uncommon enough not to be worth optimizing at this time
+                choice((
+                    date_time()
+                        .map(v::Value::from),
+                    float()
+                        .map(v::Value::from),
+                    integer()
+                        .map(v::Value::from),
+                ))
+            },
+            b't' => {
+                crate::parser::numbers::true_().map(v::Value::from).expected("quoted string")
+            },
+            b'f' => {
+                crate::parser::numbers::false_().map(v::Value::from).expected("quoted string")
+            },
+            b'i' => {
+                crate::parser::numbers::inf().map(v::Value::from).expected("quoted string")
+            },
+            b'n' => {
+                crate::parser::numbers::nan().map(v::Value::from).expected("quoted string")
+            },
+            _ => {
+                // Pick something random to fail, we'll override `expected` anyways
+                crate::parser::numbers::nan().map(v::Value::from).expected("quoted string")
+            },
         )
     })).and_then(|(raw, value)| apply_raw(value, raw))
 });


Test patch:
diff --git a/tests/fixtures/invalid/key-two-equals.stderr b/tests/fixtures/invalid/key-two-equals.stderr
index 2505bc8a..a5c06479 100644
--- a/tests/fixtures/invalid/key-two-equals.stderr
+++ b/tests/fixtures/invalid/key-two-equals.stderr
@@ -3,13 +3,4 @@ TOML parse error at line 1, column 6
 1 | key= = 1
   |      ^
 Unexpected `=`
-Expected `-`, `+`, `inf`, `nan`, `0x`, `0o` or `0b`
-expected 4 more elements
-expected 2 more elements
-While parsing a Time
-While parsing a hexadecimal Integer
-While parsing a octal Integer
-While parsing a binary Integer
-While parsing an Integer
-While parsing a Date-Time
-While parsing a Float
+Expected `quoted string`
diff --git a/tests/fixtures/invalid/string-no-quotes-constant-like.stderr b/tests/fixtures/invalid/string-no-quotes-constant-like.stderr
index 6c298e9a..01b1a6ac 100644
--- a/tests/fixtures/invalid/string-no-quotes-constant-like.stderr
+++ b/tests/fixtures/invalid/string-no-quotes-constant-like.stderr
@@ -1,6 +1,6 @@
-TOML parse error at line 1, column 8
+TOML parse error at line 1, column 7
   |
 1 | value=trust
-  |        ^
-Unexpected `r`
-Expected `rue`
+  |       ^
+Unexpected `t`
+Expected `quoted string`
diff --git a/tests/fixtures/invalid/string-no-quotes-in-array.stderr b/tests/fixtures/invalid/string-no-quotes-in-array.stderr
index e04961ea..07f80118 100644
--- a/tests/fixtures/invalid/string-no-quotes-in-array.stderr
+++ b/tests/fixtures/invalid/string-no-quotes-in-array.stderr
@@ -1,15 +1,6 @@
-TOML parse error at line 1, column 9
+TOML parse error at line 1, column 10
   |
-1 | value = ZZZ
-  |         ^
+1 | value = [ZZZ]
+  |          ^
 Unexpected `Z`
-Expected `-`, `+`, `inf`, `nan`, `0x`, `0o` or `0b`
-expected 4 more elements
-expected 2 more elements
-While parsing a Time
-While parsing a hexadecimal Integer
-While parsing a octal Integer
-While parsing a binary Integer
-While parsing an Integer
-While parsing a Date-Time
-While parsing a Float
+Expected `a newline` or `#`
diff --git a/tests/fixtures/invalid/string-no-quotes-in-array.toml b/tests/fixtures/invalid/string-no-quotes-in-array.toml
index b203d585..f70e8e80 100644
--- a/tests/fixtures/invalid/string-no-quotes-in-array.toml
+++ b/tests/fixtures/invalid/string-no-quotes-in-array.toml
@@ -1,1 +1,1 @@
-value = ZZZ
+value = [ZZZ]
diff --git a/tests/fixtures/invalid/string-no-quotes-in-inline-table.stderr b/tests/fixtures/invalid/string-no-quotes-in-inline-table.stderr
index 07f80118..bab0ccc7 100644
--- a/tests/fixtures/invalid/string-no-quotes-in-inline-table.stderr
+++ b/tests/fixtures/invalid/string-no-quotes-in-inline-table.stderr
@@ -1,6 +1,6 @@
-TOML parse error at line 1, column 10
+TOML parse error at line 1, column 17
   |
-1 | value = [ZZZ]
-  |          ^
+1 | value = { key = ZZZ }
+  |                 ^
 Unexpected `Z`
-Expected `a newline` or `#`
+Expected `quoted string`
diff --git a/tests/fixtures/invalid/string-no-quotes-in-inline-table.toml b/tests/fixtures/invalid/string-no-quotes-in-inline-table.toml
index f70e8e80..e21b119e 100644
--- a/tests/fixtures/invalid/string-no-quotes-in-inline-table.toml
+++ b/tests/fixtures/invalid/string-no-quotes-in-inline-table.toml
@@ -1,1 +1,1 @@
-value = [ZZZ]
+value = { key = ZZZ }
diff --git a/tests/fixtures/invalid/string-no-quotes-in-table.stderr b/tests/fixtures/invalid/string-no-quotes-in-table.stderr
index 7bed0ea3..c518dd97 100644
--- a/tests/fixtures/invalid/string-no-quotes-in-table.stderr
+++ b/tests/fixtures/invalid/string-no-quotes-in-table.stderr
@@ -1,15 +1,6 @@
-TOML parse error at line 1, column 14
+TOML parse error at line 1, column 7
   |
-1 | value={key = ZZZ}
-  |              ^
+1 | value=ZZZ
+  |       ^
 Unexpected `Z`
-Expected `-`, `+`, `inf`, `nan`, `0x`, `0o` or `0b`
-expected 4 more elements
-expected 2 more elements
-While parsing a Time
-While parsing a hexadecimal Integer
-While parsing a octal Integer
-While parsing a binary Integer
-While parsing an Integer
-While parsing a Date-Time
-While parsing a Float
+Expected `quoted string`
diff --git a/tests/fixtures/invalid/string-no-quotes-in-table.toml b/tests/fixtures/invalid/string-no-quotes-in-table.toml
index aba893d9..20117342 100644
--- a/tests/fixtures/invalid/string-no-quotes-in-table.toml
+++ b/tests/fixtures/invalid/string-no-quotes-in-table.toml
@@ -1,1 +1,1 @@
-value={key = ZZZ}
+value=ZZZ