Skip to content

Commit 5f18033

Browse files
committed
Use toml_edit instead of toml (#7327)
# Objective Fixes #5675. Replace `toml` with `toml_edit` ## Solution Replace `toml` with `toml_edit`. This conveniently also removes the `serde` dependency from `bevy_macro_utils`, which may speed up cold compilation by removing the serde bottleneck from most of the macro crates in the engine.
1 parent 7a176ae commit 5f18033

File tree

4 files changed

+16
-25
lines changed

4 files changed

+16
-25
lines changed

crates/bevy_macro_utils/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ license = "MIT OR Apache-2.0"
99
keywords = ["bevy"]
1010

1111
[dependencies]
12-
toml = "0.5.8"
12+
toml_edit = "0.17"
1313
syn = "1.0"
1414
quote = "1.0"

crates/bevy_macro_utils/src/lib.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use proc_macro::TokenStream;
1212
use quote::{quote, quote_spanned};
1313
use std::{env, path::PathBuf};
1414
use syn::spanned::Spanned;
15-
use toml::{map::Map, Value};
15+
use toml_edit::{Document, Item};
1616

1717
pub struct BevyManifest {
18-
manifest: Map<String, Value>,
18+
manifest: Document,
1919
}
2020

2121
impl Default for BevyManifest {
@@ -26,7 +26,7 @@ impl Default for BevyManifest {
2626
.map(|mut path| {
2727
path.push("Cargo.toml");
2828
let manifest = std::fs::read_to_string(path).unwrap();
29-
toml::from_str(&manifest).unwrap()
29+
manifest.parse::<Document>().unwrap()
3030
})
3131
.unwrap(),
3232
}
@@ -37,18 +37,15 @@ const BEVY_INTERNAL: &str = "bevy_internal";
3737

3838
impl BevyManifest {
3939
pub fn maybe_get_path(&self, name: &str) -> Option<syn::Path> {
40-
fn dep_package(dep: &Value) -> Option<&str> {
40+
fn dep_package(dep: &Item) -> Option<&str> {
4141
if dep.as_str().is_some() {
4242
None
4343
} else {
44-
dep.as_table()
45-
.unwrap()
46-
.get("package")
47-
.map(|name| name.as_str().unwrap())
44+
dep.get("package").map(|name| name.as_str().unwrap())
4845
}
4946
}
5047

51-
let find_in_deps = |deps: &Map<String, Value>| -> Option<syn::Path> {
48+
let find_in_deps = |deps: &Item| -> Option<syn::Path> {
5249
let package = if let Some(dep) = deps.get(name) {
5350
return Some(Self::parse_str(dep_package(dep).unwrap_or(name)));
5451
} else if let Some(dep) = deps.get(BEVY) {
@@ -66,14 +63,8 @@ impl BevyManifest {
6663
Some(path)
6764
};
6865

69-
let deps = self
70-
.manifest
71-
.get("dependencies")
72-
.map(|deps| deps.as_table().unwrap());
73-
let deps_dev = self
74-
.manifest
75-
.get("dev-dependencies")
76-
.map(|deps| deps.as_table().unwrap());
66+
let deps = self.manifest.get("dependencies");
67+
let deps_dev = self.manifest.get("dev-dependencies");
7768

7869
deps.and_then(find_in_deps)
7970
.or_else(|| deps_dev.and_then(find_in_deps))

tools/build-example-pages/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish = false
77
license = "MIT OR Apache-2.0"
88

99
[dependencies]
10-
toml = "0.5"
10+
toml_edit = "0.17"
1111
tera = "1.15"
1212
serde = { version = "1.0", features = [ "derive" ] }
1313
bitflags = "1.3"

tools/build-example-pages/src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{cmp::Ordering, collections::HashMap, fs::File};
33
use bitflags::bitflags;
44
use serde::Serialize;
55
use tera::{Context, Tera};
6-
use toml::Value;
6+
use toml_edit::Document;
77

88
bitflags! {
99
struct Command: u32 {
@@ -89,7 +89,7 @@ impl PartialOrd for Example {
8989

9090
fn parse_examples(panic_on_missing: bool) -> Vec<Example> {
9191
let manifest_file = std::fs::read_to_string("Cargo.toml").unwrap();
92-
let manifest: HashMap<String, Value> = toml::from_str(&manifest_file).unwrap();
92+
let manifest = manifest_file.parse::<Document>().unwrap();
9393
let metadatas = manifest
9494
.get("package")
9595
.unwrap()
@@ -99,11 +99,11 @@ fn parse_examples(panic_on_missing: bool) -> Vec<Example> {
9999
.clone();
100100

101101
manifest["example"]
102-
.as_array()
102+
.as_array_of_tables()
103103
.unwrap()
104104
.iter()
105105
.flat_map(|val| {
106-
let technical_name = val["name"].as_str().unwrap().to_string();
106+
let technical_name = val.get("name").unwrap().as_str().unwrap().to_string();
107107
if panic_on_missing && metadatas.get(&technical_name).is_none() {
108108
panic!("Missing metadata for example {technical_name}");
109109
}
@@ -132,15 +132,15 @@ fn parse_examples(panic_on_missing: bool) -> Vec<Example> {
132132

133133
fn parse_categories() -> HashMap<String, String> {
134134
let manifest_file = std::fs::read_to_string("Cargo.toml").unwrap();
135-
let manifest: HashMap<String, Value> = toml::from_str(&manifest_file).unwrap();
135+
let manifest = manifest_file.parse::<Document>().unwrap();
136136
manifest
137137
.get("package")
138138
.unwrap()
139139
.get("metadata")
140140
.as_ref()
141141
.unwrap()["category"]
142142
.clone()
143-
.as_array()
143+
.as_array_of_tables()
144144
.unwrap()
145145
.iter()
146146
.map(|v| {

0 commit comments

Comments
 (0)