Skip to content

Commit

Permalink
Search dep in other tables
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
  • Loading branch information
Rustin170506 committed Oct 26, 2023
1 parent 7f3fc2b commit 9c83284
Showing 1 changed file with 72 additions and 12 deletions.
84 changes: 72 additions & 12 deletions src/cargo/util/toml_mut/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,22 +367,77 @@ impl LocalManifest {
pub fn remove_from_table(&mut self, table_path: &[String], name: &str) -> CargoResult<()> {
let parent_table = self.get_table_mut(table_path)?;

let dep = parent_table
.get_mut(name)
.filter(|t| !t.is_none())
.ok_or_else(|| non_existent_dependency_err(name, table_path.join(".")))?;

// remove the dependency
*dep = toml_edit::Item::None;
match parent_table.get_mut(name).filter(|t| !t.is_none()) {
Some(dep) => {
// remove the dependency
*dep = toml_edit::Item::None;

// remove table if empty
if parent_table.as_table_like().unwrap().is_empty() {
*parent_table = toml_edit::Item::None;
}
}
None => {
// Search in other tables.
let table_paths = self.get_all_deps_table_paths();
let found_table_path = table_paths
.iter()
.find(|table_path| {
let table = self.get_table(table_path).unwrap();
table.get(name).filter(|t| !t.is_none()).is_some()
})
.map(|table_path| table_path.join("."));

// remove table if empty
if parent_table.as_table_like().unwrap().is_empty() {
*parent_table = toml_edit::Item::None;
return Err(non_existent_dependency_err(
name,
table_path.join("."),
found_table_path,
));
}
}

Ok(())
}

fn get_all_deps_table_paths(&self) -> Vec<Vec<String>> {
let mut result = Vec::new();

for table in DepTable::KINDS {
let dependency_type = table.kind.kind_table();
// Dependencies can be in the three standard sections...
if self
.data
.get(dependency_type)
.map(|t| t.is_table_like())
.unwrap_or(false)
{
result.push(vec![dependency_type.to_owned()])
}

// And in `target.<target>.(build-/dev-)dependencies`.
result.extend(
self.data
.as_table()
.get("target")
.and_then(toml_edit::Item::as_table_like)
.into_iter()
.flat_map(toml_edit::TableLike::iter)
.filter_map(|(target_name, target_table)| {
let dependency_table = target_table.get(dependency_type)?;
dependency_table.as_table_like().map(|_| {
vec![
"target".to_owned(),
target_name.to_owned(),
dependency_type.to_owned(),
]
})
}),
);
}

result
}

/// Remove references to `dep_key` if its no longer present.
pub fn gc_dep(&mut self, dep_key: &str) {
let explicit_dep_activation = self.is_explicit_dep_activation(dep_key);
Expand Down Expand Up @@ -537,9 +592,14 @@ fn non_existent_table_err(table: impl std::fmt::Display) -> anyhow::Error {

fn non_existent_dependency_err(
name: impl std::fmt::Display,
table: impl std::fmt::Display,
search_table: impl std::fmt::Display,
found_table: Option<impl std::fmt::Display>,
) -> anyhow::Error {
anyhow::format_err!("the dependency `{name}` could not be found in `{table}`.")
let mut msg = format!("the dependency `{name}` could not be found in `{search_table}`.");
if let Some(found_table) = found_table {
msg.push_str(&format!("But it was found in `{found_table}`.",));
}
anyhow::format_err!(msg)
}

fn remove_array_index(array: &mut toml_edit::Array, index: usize) {
Expand Down

0 comments on commit 9c83284

Please sign in to comment.