Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of inline tables #58

Merged
merged 7 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
disallowed-methods = [
{ path = "toml_edit::Item::as_table", reason = "Use .as_table_like instead!" },
{ path = "toml_edit::Item::is_table", reason = "Use .is_table_like instead!" },
]
21 changes: 11 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ use std::{
time::SystemTime,
};

use toml_edit::{DocumentMut, Item, Table, TomlError};
use toml_edit::{DocumentMut, Item, TableLike, TomlError};

/// Error type used by this crate.
pub enum Error {
Expand Down Expand Up @@ -308,6 +308,7 @@ fn extract_workspace_dependencies(
) -> Result<BTreeMap<String, String>, Error> {
Ok(workspace_dep_tables(&workspace_toml)
.into_iter()
.map(|t| t.iter())
.flatten()
.map(move |(dep_name, dep_value)| {
let pkg_name = dep_value.get("package").and_then(|i| i.as_str()).unwrap_or(dep_name);
Expand All @@ -318,10 +319,10 @@ fn extract_workspace_dependencies(
}

/// Return an iterator over all `[workspace.dependencies]`
fn workspace_dep_tables(cargo_toml: &DocumentMut) -> Option<&Table> {
fn workspace_dep_tables(cargo_toml: &DocumentMut) -> Option<&dyn TableLike> {
cargo_toml
.get("workspace")
.and_then(|w| w.as_table()?.get("dependencies")?.as_table())
.and_then(|w| w.as_table_like()?.get("dependencies")?.as_table_like())
}

/// Make sure that the given crate name is a valid rust identifier.
Expand Down Expand Up @@ -354,8 +355,8 @@ fn extract_crate_names(
(name.to_string(), cr)
});

let dep_tables = dep_tables(cargo_toml).chain(target_dep_tables(cargo_toml));
let dep_pkgs = dep_tables.flatten().filter_map(move |(dep_name, dep_value)| {
let dep_tables = dep_tables(cargo_toml.as_table()).chain(target_dep_tables(cargo_toml));
let dep_pkgs = dep_tables.map(|t| t.iter()).flatten().filter_map(move |(dep_name, dep_value)| {
let pkg_name = dep_value.get("package").and_then(|i| i.as_str()).unwrap_or(dep_name);

// We already handle this via `root_pkg` above.
Expand Down Expand Up @@ -383,18 +384,18 @@ fn extract_package_name(cargo_toml: &DocumentMut) -> Option<&str> {
cargo_toml.get("package")?.get("name")?.as_str()
}

fn target_dep_tables(cargo_toml: &DocumentMut) -> impl Iterator<Item = &Table> {
cargo_toml.get("target").into_iter().filter_map(Item::as_table).flat_map(|t| {
t.iter().map(|(_, value)| value).filter_map(Item::as_table).flat_map(dep_tables)
fn target_dep_tables(cargo_toml: &DocumentMut) -> impl Iterator<Item = &dyn TableLike> {
cargo_toml.get("target").into_iter().filter_map(Item::as_table_like).flat_map(|t| {
t.iter().map(|(_, value)| value).filter_map(Item::as_table_like).flat_map(dep_tables)
})
}

fn dep_tables(table: &Table) -> impl Iterator<Item = &Table> {
fn dep_tables(table: &dyn TableLike) -> impl Iterator<Item = &dyn TableLike> {
table
.get("dependencies")
.into_iter()
.chain(table.get("dev-dependencies"))
.filter_map(Item::as_table)
.filter_map(Item::as_table_like)
}

#[cfg(test)]
Expand Down
Loading