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.
This commit is contained in:
James Liu 2023-01-22 19:41:48 +00:00
parent 7a176ae0a8
commit 5f18033dd3
4 changed files with 16 additions and 25 deletions

View File

@ -9,6 +9,6 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"] keywords = ["bevy"]
[dependencies] [dependencies]
toml = "0.5.8" toml_edit = "0.17"
syn = "1.0" syn = "1.0"
quote = "1.0" quote = "1.0"

View File

@ -12,10 +12,10 @@ use proc_macro::TokenStream;
use quote::{quote, quote_spanned}; use quote::{quote, quote_spanned};
use std::{env, path::PathBuf}; use std::{env, path::PathBuf};
use syn::spanned::Spanned; use syn::spanned::Spanned;
use toml::{map::Map, Value}; use toml_edit::{Document, Item};
pub struct BevyManifest { pub struct BevyManifest {
manifest: Map<String, Value>, manifest: Document,
} }
impl Default for BevyManifest { impl Default for BevyManifest {
@ -26,7 +26,7 @@ impl Default for BevyManifest {
.map(|mut path| { .map(|mut path| {
path.push("Cargo.toml"); path.push("Cargo.toml");
let manifest = std::fs::read_to_string(path).unwrap(); let manifest = std::fs::read_to_string(path).unwrap();
toml::from_str(&manifest).unwrap() manifest.parse::<Document>().unwrap()
}) })
.unwrap(), .unwrap(),
} }
@ -37,18 +37,15 @@ const BEVY_INTERNAL: &str = "bevy_internal";
impl BevyManifest { impl BevyManifest {
pub fn maybe_get_path(&self, name: &str) -> Option<syn::Path> { pub fn maybe_get_path(&self, name: &str) -> Option<syn::Path> {
fn dep_package(dep: &Value) -> Option<&str> { fn dep_package(dep: &Item) -> Option<&str> {
if dep.as_str().is_some() { if dep.as_str().is_some() {
None None
} else { } else {
dep.as_table() dep.get("package").map(|name| name.as_str().unwrap())
.unwrap()
.get("package")
.map(|name| name.as_str().unwrap())
} }
} }
let find_in_deps = |deps: &Map<String, Value>| -> Option<syn::Path> { let find_in_deps = |deps: &Item| -> Option<syn::Path> {
let package = if let Some(dep) = deps.get(name) { let package = if let Some(dep) = deps.get(name) {
return Some(Self::parse_str(dep_package(dep).unwrap_or(name))); return Some(Self::parse_str(dep_package(dep).unwrap_or(name)));
} else if let Some(dep) = deps.get(BEVY) { } else if let Some(dep) = deps.get(BEVY) {
@ -66,14 +63,8 @@ impl BevyManifest {
Some(path) Some(path)
}; };
let deps = self let deps = self.manifest.get("dependencies");
.manifest let deps_dev = self.manifest.get("dev-dependencies");
.get("dependencies")
.map(|deps| deps.as_table().unwrap());
let deps_dev = self
.manifest
.get("dev-dependencies")
.map(|deps| deps.as_table().unwrap());
deps.and_then(find_in_deps) deps.and_then(find_in_deps)
.or_else(|| deps_dev.and_then(find_in_deps)) .or_else(|| deps_dev.and_then(find_in_deps))

View File

@ -7,7 +7,7 @@ publish = false
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
[dependencies] [dependencies]
toml = "0.5" toml_edit = "0.17"
tera = "1.15" tera = "1.15"
serde = { version = "1.0", features = [ "derive" ] } serde = { version = "1.0", features = [ "derive" ] }
bitflags = "1.3" bitflags = "1.3"

View File

@ -3,7 +3,7 @@ use std::{cmp::Ordering, collections::HashMap, fs::File};
use bitflags::bitflags; use bitflags::bitflags;
use serde::Serialize; use serde::Serialize;
use tera::{Context, Tera}; use tera::{Context, Tera};
use toml::Value; use toml_edit::Document;
bitflags! { bitflags! {
struct Command: u32 { struct Command: u32 {
@ -89,7 +89,7 @@ impl PartialOrd for Example {
fn parse_examples(panic_on_missing: bool) -> Vec<Example> { fn parse_examples(panic_on_missing: bool) -> Vec<Example> {
let manifest_file = std::fs::read_to_string("Cargo.toml").unwrap(); let manifest_file = std::fs::read_to_string("Cargo.toml").unwrap();
let manifest: HashMap<String, Value> = toml::from_str(&manifest_file).unwrap(); let manifest = manifest_file.parse::<Document>().unwrap();
let metadatas = manifest let metadatas = manifest
.get("package") .get("package")
.unwrap() .unwrap()
@ -99,11 +99,11 @@ fn parse_examples(panic_on_missing: bool) -> Vec<Example> {
.clone(); .clone();
manifest["example"] manifest["example"]
.as_array() .as_array_of_tables()
.unwrap() .unwrap()
.iter() .iter()
.flat_map(|val| { .flat_map(|val| {
let technical_name = val["name"].as_str().unwrap().to_string(); let technical_name = val.get("name").unwrap().as_str().unwrap().to_string();
if panic_on_missing && metadatas.get(&technical_name).is_none() { if panic_on_missing && metadatas.get(&technical_name).is_none() {
panic!("Missing metadata for example {technical_name}"); panic!("Missing metadata for example {technical_name}");
} }
@ -132,7 +132,7 @@ fn parse_examples(panic_on_missing: bool) -> Vec<Example> {
fn parse_categories() -> HashMap<String, String> { fn parse_categories() -> HashMap<String, String> {
let manifest_file = std::fs::read_to_string("Cargo.toml").unwrap(); let manifest_file = std::fs::read_to_string("Cargo.toml").unwrap();
let manifest: HashMap<String, Value> = toml::from_str(&manifest_file).unwrap(); let manifest = manifest_file.parse::<Document>().unwrap();
manifest manifest
.get("package") .get("package")
.unwrap() .unwrap()
@ -140,7 +140,7 @@ fn parse_categories() -> HashMap<String, String> {
.as_ref() .as_ref()
.unwrap()["category"] .unwrap()["category"]
.clone() .clone()
.as_array() .as_array_of_tables()
.unwrap() .unwrap()
.iter() .iter()
.map(|v| { .map(|v| {