Skip to content

Commit

Permalink
Use toml_edit instead of toml (bevyengine#7327)
Browse files Browse the repository at this point in the history
# Objective
Fixes bevyengine#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.
  • Loading branch information
james7132 authored and ItsDoot committed Feb 1, 2023
1 parent 28d8c41 commit b32ff45
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 25 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_macro_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"]

[dependencies]
toml = "0.5.8"
toml_edit = "0.17"
syn = "1.0"
quote = "1.0"
25 changes: 8 additions & 17 deletions crates/bevy_macro_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use proc_macro::TokenStream;
use quote::{quote, quote_spanned};
use std::{env, path::PathBuf};
use syn::spanned::Spanned;
use toml::{map::Map, Value};
use toml_edit::{Document, Item};

pub struct BevyManifest {
manifest: Map<String, Value>,
manifest: Document,
}

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

impl BevyManifest {
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() {
None
} else {
dep.as_table()
.unwrap()
.get("package")
.map(|name| name.as_str().unwrap())
dep.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) {
return Some(Self::parse_str(dep_package(dep).unwrap_or(name)));
} else if let Some(dep) = deps.get(BEVY) {
Expand All @@ -66,14 +63,8 @@ impl BevyManifest {
Some(path)
};

let deps = self
.manifest
.get("dependencies")
.map(|deps| deps.as_table().unwrap());
let deps_dev = self
.manifest
.get("dev-dependencies")
.map(|deps| deps.as_table().unwrap());
let deps = self.manifest.get("dependencies");
let deps_dev = self.manifest.get("dev-dependencies");

deps.and_then(find_in_deps)
.or_else(|| deps_dev.and_then(find_in_deps))
Expand Down
2 changes: 1 addition & 1 deletion tools/build-example-pages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false
license = "MIT OR Apache-2.0"

[dependencies]
toml = "0.5"
toml_edit = "0.17"
tera = "1.15"
serde = { version = "1.0", features = [ "derive" ] }
bitflags = "1.3"
12 changes: 6 additions & 6 deletions tools/build-example-pages/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{cmp::Ordering, collections::HashMap, fs::File};
use bitflags::bitflags;
use serde::Serialize;
use tera::{Context, Tera};
use toml::Value;
use toml_edit::Document;

bitflags! {
struct Command: u32 {
Expand Down Expand Up @@ -89,7 +89,7 @@ impl PartialOrd for Example {

fn parse_examples(panic_on_missing: bool) -> Vec<Example> {
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
.get("package")
.unwrap()
Expand All @@ -99,11 +99,11 @@ fn parse_examples(panic_on_missing: bool) -> Vec<Example> {
.clone();

manifest["example"]
.as_array()
.as_array_of_tables()
.unwrap()
.iter()
.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() {
panic!("Missing metadata for example {technical_name}");
}
Expand Down Expand Up @@ -132,15 +132,15 @@ fn parse_examples(panic_on_missing: bool) -> Vec<Example> {

fn parse_categories() -> HashMap<String, String> {
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
.get("package")
.unwrap()
.get("metadata")
.as_ref()
.unwrap()["category"]
.clone()
.as_array()
.as_array_of_tables()
.unwrap()
.iter()
.map(|v| {
Expand Down

0 comments on commit b32ff45

Please sign in to comment.