Skip to content

Commit

Permalink
refactor: simplify duplicate checking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Feb 22, 2024
1 parent 8017e20 commit 569a02d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
30 changes: 0 additions & 30 deletions src/cli/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,23 +217,9 @@ pub async fn add_pypi_specs_to_project(
// TODO: Get best version
// Add the dependency to the project
if specs_platforms.is_empty() {
let dependencies = project.pypi_dependencies(None);
if let Some(package_name) = dependencies.keys().find(|pkg_name| &name == pkg_name) {
return Err(miette::miette!(
"{} is already added as dependency.",
console::style(package_name.as_source_str()).bold(),
));
}
project.manifest.add_pypi_dependency(name, spec, None)?;
} else {
for platform in specs_platforms.iter() {
let dependencies = project.pypi_dependencies(Some(*platform));
if let Some(package_name) = dependencies.keys().find(|pkg_name| &name == pkg_name) {
return Err(miette::miette!(
"{} is already added as dependency.",
console::style(package_name.as_source_str()).bold(),
));
}
project
.manifest
.add_pypi_dependency(name, spec, Some(*platform))?;
Expand Down Expand Up @@ -329,25 +315,9 @@ pub async fn add_conda_specs_to_project(

// Add the dependency to the project
if specs_platforms.is_empty() {
let dependencies = project.dependencies(Some(spec_type), None);
if let Some(dependency) = dependencies.names().find(|d| spec.name.as_ref() == Some(d)) {
return Err(miette::miette!(
"{} is already added as dependency.",
console::style(dependency.as_normalized()).bold(),
));
}
project.manifest.add_dependency(&spec, spec_type, None)?;
} else {
for platform in specs_platforms.iter() {
let dependencies = project.dependencies(Some(spec_type), Some(*platform));
if let Some(dependency) =
dependencies.names().find(|d| spec.name.as_ref() == Some(d))
{
return Err(miette::miette!(
"{} is already added as dependency.",
console::style(dependency.as_normalized()).bold(),
));
}
project
.manifest
.add_dependency(&spec, spec_type, Some(*platform))?;
Expand Down
16 changes: 16 additions & 0 deletions src/project/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,14 @@ impl Manifest {
miette::bail!("pixi does not support wildcard dependencies")
};

// Check for duplicates.
if dependency_table.contains_key(name.as_source()) {
return Err(miette::miette!(
"{} is already added.",
console::style(name.as_normalized()).bold(),
));
}

// Store (or replace) in the document
dependency_table.insert(name.as_source(), Item::Value(spec.to_string().into()));

Expand Down Expand Up @@ -387,6 +395,14 @@ impl Manifest {
// Add the pypi dependency to the table
dependency_table.insert(name.as_str(), (*requirement).clone().into());

// Check for duplicates.
if dependency_table.contains_key(name.as_str()) {
return Err(miette::miette!(
"{} is already added.",
console::style(name.as_source_str()).bold(),
));
}

// Add the dependency to the manifest as well
self.default_feature_mut()
.targets
Expand Down

0 comments on commit 569a02d

Please sign in to comment.