Repository: toml-rs/toml
Pull request #870 (2025-04-23 19:20:47)

fix: Remove hack for serializing Values
This was to avoid writing key-value pairs for a table after children tables are written.
However, using `toml_edit`, this is taken care of for us.

This causesx a table ordering that doesn't happen otherwise

coveralls: ## Pull Request Test Coverage Report for [Build 14626350367](https://coveralls.io/builds/73355914)


### Details

* **1** of **1**   **(100.0%)**  changed or added relevant line in **1** file are covered.
* **1** unchanged line in **1** file lost coverage.
* Overall coverage decreased (**-0.07%**) to **67.689%**

---


|  Files with Coverage Reduction | New Missed Lines | % |
| :-----|--------------|--: |
| [crates/toml/src/value.rs](https://coveralls.io/builds/73355914/source?filename=crates%2Ftoml%2Fsrc%2Fvalue.rs#L192) | 1 | 54.47% |
<!-- | **Total:** | **1** |  | -->

|  Totals | [![Coverage Status](https://coveralls.io/builds/73355914/badge)](https://coveralls.io/builds/73355914) |
| :-- | --: |
| Change from base [Build 14626342720](https://coveralls.io/builds/73355905): |  -0.07% |
| Covered Lines: | 3863 |
| Relevant Lines: | 5707 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/crates/toml/src/value.rs b/crates/toml/src/value.rs
index e85523b1..77db874b 100644
--- a/crates/toml/src/value.rs
+++ b/crates/toml/src/value.rs
@@ -400,8 +400,6 @@ impl ser::Serialize for Value {
     where
         S: ser::Serializer,
     {
-        use serde::ser::SerializeMap;
-
         match *self {
             Value::String(ref s) => serializer.serialize_str(s),
             Value::Integer(i) => serializer.serialize_i64(i),
@@ -409,35 +407,7 @@ impl ser::Serialize for Value {
             Value::Boolean(b) => serializer.serialize_bool(b),
             Value::Datetime(ref s) => s.serialize(serializer),
             Value::Array(ref a) => a.serialize(serializer),
-            Value::Table(ref t) => {
-                let mut map = serializer.serialize_map(Some(t.len()))?;
-                // Be sure to visit non-tables first (and also non
-                // array-of-tables) as all keys must be emitted first.
-                for (k, v) in t {
-                    if !v.is_table() && !v.is_array()
-                        || (v
-                            .as_array()
-                            .map(|a| !a.iter().any(|v| v.is_table()))
-                            .unwrap_or(false))
-                    {
-                        map.serialize_entry(k, v)?;
-                    }
-                }
-                for (k, v) in t {
-                    if v.as_array()
-                        .map(|a| a.iter().any(|v| v.is_table()))
-                        .unwrap_or(false)
-                    {
-                        map.serialize_entry(k, v)?;
-                    }
-                }
-                for (k, v) in t {
-                    if v.is_table() {
-                        map.serialize_entry(k, v)?;
-                    }
-                }
-                map.end()
-            }
+            Value::Table(ref t) => t.serialize(serializer),
         }
     }
 }


Test patch:
diff --git a/crates/toml/tests/serde/pretty.rs b/crates/toml/tests/serde/pretty.rs
index 22dce5f9..9ac78501 100644
--- a/crates/toml/tests/serde/pretty.rs
+++ b/crates/toml/tests/serde/pretty.rs
@@ -146,15 +146,15 @@ this is the fourth line
 
 #[test]
 fn pretty_table_array() {
-    let toml = r#"[[array]]
+    let toml = r#"[abc]
+doc = "this is a table"
+
+[[array]]
 key = "foo"
 
 [[array]]
 key = "bar"
 
-[abc]
-doc = "this is a table"
-
 [example]
 single = "this is a single line string"
 "#;
@@ -167,15 +167,15 @@ single = "this is a single line string"
     assert_data_eq!(
         &result,
         str![[r#"
+[abc]
+doc = "this is a table"
+
 [[array]]
 key = "foo"
 
 [[array]]
 key = "bar"
 
-[abc]
-doc = "this is a table"
-
 [example]
 single = "this is a single line string"
 
@@ -185,15 +185,15 @@ single = "this is a single line string"
 
 #[test]
 fn no_pretty_table_array() {
-    let toml = r#"[[array]]
+    let toml = r#"[abc]
+doc = "this is a table"
+
+[[array]]
 key = "foo"
 
 [[array]]
 key = "bar"
 
-[abc]
-doc = "this is a table"
-
 [example]
 single = "this is a single line string"
 "#;
@@ -204,15 +204,15 @@ single = "this is a single line string"
     assert_data_eq!(
         &result,
         str![[r#"
+[abc]
+doc = "this is a table"
+
 [[array]]
 key = "foo"
 
 [[array]]
 key = "bar"
 
-[abc]
-doc = "this is a table"
-
 [example]
 single = "this is a single line string"