Skip to content

Commit 2309ded

Browse files
authored
Merge pull request #29 from dollisgame/toml-edit
Switch to toml_edit
2 parents fe743d0 + 56a40f1 commit 2309ded

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Replacement for crate (macro_rules keyword) in proc-macros
1414
readme = "./README.md"
1515

1616
[dependencies]
17-
toml = "0.5.2"
17+
toml_edit = "0.18"
1818
once_cell = "1.13.0"
1919

2020
[dev-dependencies]

src/lib.rs

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,22 @@ at your option.
5858

5959
use std::{
6060
collections::btree_map::{self, BTreeMap},
61-
env, fmt,
62-
fs::{self, File},
63-
io::{self, Read},
61+
env, fmt, fs, io,
6462
path::{Path, PathBuf},
6563
sync::Mutex,
6664
time::SystemTime,
6765
};
6866

6967
use once_cell::sync::Lazy;
70-
use toml::{self, value::Table};
68+
use toml_edit::{Document, Item, Table, TomlError};
7169

7270
/// Error type used by this crate.
7371
#[derive(Debug)]
7472
pub enum Error {
7573
NotFound(PathBuf),
7674
CargoManifestDirNotSet,
7775
CouldNotRead { path: PathBuf, source: io::Error },
78-
InvalidToml { source: toml::de::Error },
76+
InvalidToml { source: TomlError },
7977
CrateNotFound { crate_name: String, path: PathBuf },
8078
}
8179

@@ -215,24 +213,19 @@ fn sanitize_crate_name<S: AsRef<str>>(name: S) -> String {
215213
}
216214

217215
/// Open the given `Cargo.toml` and parse it into a hashmap.
218-
fn open_cargo_toml(path: &Path) -> Result<Table, Error> {
219-
let mut content = String::new();
220-
File::open(path)
221-
.map_err(|e| Error::CouldNotRead {
222-
source: e,
223-
path: path.into(),
224-
})?
225-
.read_to_string(&mut content)
226-
.map_err(|e| Error::CouldNotRead {
227-
source: e,
228-
path: path.into(),
229-
})?;
230-
toml::from_str(&content).map_err(|e| Error::InvalidToml { source: e })
216+
fn open_cargo_toml(path: &Path) -> Result<Document, Error> {
217+
let content = fs::read_to_string(path).map_err(|e| Error::CouldNotRead {
218+
source: e,
219+
path: path.into(),
220+
})?;
221+
content
222+
.parse::<Document>()
223+
.map_err(|e| Error::InvalidToml { source: e })
231224
}
232225

233226
/// Extract all crate names from the given `Cargo.toml` by checking the `dependencies` and
234227
/// `dev-dependencies`.
235-
fn extract_crate_names(cargo_toml: &Table) -> Result<CrateNames, Error> {
228+
fn extract_crate_names(cargo_toml: &Document) -> Result<CrateNames, Error> {
236229
let package_name = extract_package_name(cargo_toml);
237230
let root_pkg = package_name.map(|name| {
238231
let cr = match env::var_os("CARGO_TARGET_TMPDIR") {
@@ -248,8 +241,8 @@ fn extract_crate_names(cargo_toml: &Table) -> Result<CrateNames, Error> {
248241
let dep_tables = dep_tables(cargo_toml).chain(target_dep_tables(cargo_toml));
249242
let dep_pkgs = dep_tables.flatten().map(|(dep_name, dep_value)| {
250243
let pkg_name = dep_value
251-
.as_table()
252-
.and_then(|t| t.get("package")?.as_str())
244+
.get("package")
245+
.and_then(|i| i.as_str())
253246
.unwrap_or(dep_name);
254247
let cr = FoundCrate::Name(sanitize_crate_name(dep_name));
255248

@@ -259,18 +252,19 @@ fn extract_crate_names(cargo_toml: &Table) -> Result<CrateNames, Error> {
259252
Ok(root_pkg.into_iter().chain(dep_pkgs).collect())
260253
}
261254

262-
fn extract_package_name(cargo_toml: &Table) -> Option<&str> {
263-
cargo_toml.get("package")?.as_table()?.get("name")?.as_str()
255+
fn extract_package_name(cargo_toml: &Document) -> Option<&str> {
256+
cargo_toml.get("package")?.get("name")?.as_str()
264257
}
265258

266-
fn target_dep_tables(cargo_toml: &Table) -> impl Iterator<Item = &Table> {
259+
fn target_dep_tables(cargo_toml: &Document) -> impl Iterator<Item = &Table> {
267260
cargo_toml
268261
.get("target")
269262
.into_iter()
270-
.filter_map(toml::Value::as_table)
263+
.filter_map(Item::as_table)
271264
.flat_map(|t| {
272-
t.values()
273-
.filter_map(toml::Value::as_table)
265+
t.iter()
266+
.map(|(_, value)| value)
267+
.filter_map(Item::as_table)
274268
.flat_map(dep_tables)
275269
})
276270
}
@@ -280,7 +274,7 @@ fn dep_tables(table: &Table) -> impl Iterator<Item = &Table> {
280274
.get("dependencies")
281275
.into_iter()
282276
.chain(table.get("dev-dependencies"))
283-
.filter_map(toml::Value::as_table)
277+
.filter_map(Item::as_table)
284278
}
285279

286280
#[cfg(test)]
@@ -295,7 +289,7 @@ mod tests {
295289
) => {
296290
#[test]
297291
fn $name() {
298-
let cargo_toml = toml::from_str($cargo_toml).expect("Parses `Cargo.toml`");
292+
let cargo_toml = $cargo_toml.parse::<Document>().expect("Parses `Cargo.toml`");
299293

300294
match extract_crate_names(&cargo_toml).map(|mut map| map.remove("my_crate")) {
301295
$( $result )* => (),

0 commit comments

Comments
 (0)