Repository: ron-rs/ron
Pull request #41 (2017-08-05 12:45:54)

Be more permissive wrt whitespace


Patch:
diff --git a/src/de/mod.rs b/src/de/mod.rs
index a139915a..f3c3099c 100644
--- a/src/de/mod.rs
+++ b/src/de/mod.rs
@@ -178,9 +178,11 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
     fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
         where V: Visitor<'de>
     {
-        if self.bytes.consume("Some(") {
+        if self.bytes.consume("Some") && { self.bytes.skip_ws(); self.bytes.consume("(") } {
             let v = visitor.visit_some(&mut *self)?;
 
+            self.bytes.skip_ws();
+
             if self.bytes.consume(")") {
                 Ok(v)
             } else {
@@ -228,6 +230,8 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
     {
         self.bytes.consume(name);
 
+        self.bytes.skip_ws();
+
         if self.bytes.consume("(") {
             let value = visitor.visit_newtype_struct(&mut *self)?;
             self.bytes.comma();
@@ -325,6 +329,8 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
     {
         self.bytes.consume(name);
 
+        self.bytes.skip_ws();
+
         if self.bytes.consume("(") {
             let value = visitor.visit_map(CommaSeparated::new(b')', &mut self))?;
             self.bytes.comma();
@@ -430,6 +436,8 @@ impl<'de, 'a> de::MapAccess<'de> for CommaSeparated<'a, 'de> {
     fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
         where V: DeserializeSeed<'de>
     {
+        self.de.bytes.skip_ws();
+
         if self.de.bytes.consume(":") {
             self.de.bytes.skip_ws();
 
@@ -477,6 +485,8 @@ impl<'de, 'a> de::VariantAccess<'de> for Enum<'a, 'de> {
     fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
         where T: DeserializeSeed<'de>
     {
+        self.de.bytes.skip_ws();
+        
         if self.de.bytes.consume("(") {
             let val = seed.deserialize(&mut *self.de)?;
 
@@ -495,6 +505,8 @@ impl<'de, 'a> de::VariantAccess<'de> for Enum<'a, 'de> {
     fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value>
         where V: Visitor<'de>
     {
+        self.de.bytes.skip_ws();
+
         self.de.deserialize_tuple(len, visitor)
     }
 
@@ -505,6 +517,8 @@ impl<'de, 'a> de::VariantAccess<'de> for Enum<'a, 'de> {
     ) -> Result<V::Value>
         where V: Visitor<'de>
     {
+        self.de.bytes.skip_ws();
+
         self.de.deserialize_struct("", fields, visitor)
     }
 }
diff --git a/src/parse.rs b/src/parse.rs
index d71bfc89..6f29e045 100644
--- a/src/parse.rs
+++ b/src/parse.rs
@@ -19,11 +19,15 @@ pub struct Bytes<'a> {
 
 impl<'a> Bytes<'a> {
     pub fn new(bytes: &'a [u8]) -> Self {
-        Bytes {
+        let mut b = Bytes {
             bytes,
             column: 1,
             line: 1,
-        }
+        };
+
+        b.skip_ws();
+
+        b
     }
 
     pub fn advance(&mut self, bytes: usize) -> Result<()> {
@@ -88,6 +92,8 @@ impl<'a> Bytes<'a> {
     }
 
     pub fn comma(&mut self) -> bool {
+        self.skip_ws();
+
         if self.consume(",") {
             self.skip_ws();
 


Test patch:
diff --git a/src/de/tests.rs b/src/de/tests.rs
index 2226de7d..dd729646 100644
--- a/src/de/tests.rs
+++ b/src/de/tests.rs
@@ -141,3 +141,9 @@ fn test_err_wrong_value() {
     assert_eq!(from_str::<MyStruct>("MyStruct(\n    x: 3.5, \n    y:)"),
                err(ExpectedFloat, 3, 7));
 }
+
+#[test]
+fn test_perm_ws() {
+    assert_eq!(from_str::<MyStruct>("\nMyStruct  \t ( \n x   : 3.5 , \t y\n: 4.5 \n ) \t\n"),
+                Ok(MyStruct { x: 3.5, y: 4.5 }));
+}