Skip to content

Commit d3509d8

Browse files
committed
publish: Extract validate_dependencies() fn
1 parent 8613e5f commit d3509d8

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

src/controllers/krate/publish.rs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
284284
)?;
285285

286286
// Link this new version to all dependencies
287+
validate_dependencies(&metadata.deps)?;
287288
add_dependencies(conn, &metadata.deps, version.id)?;
288289

289290
// Update all keywords for this crate
@@ -429,6 +430,33 @@ fn missing_metadata_error_message(missing: &[&str]) -> String {
429430
)
430431
}
431432

433+
#[instrument(skip_all)]
434+
pub fn validate_dependencies(deps: &[EncodableCrateDependency]) -> AppResult<()> {
435+
deps
436+
.iter()
437+
.map(|dep| {
438+
if let Some(registry) = &dep.registry {
439+
if !registry.is_empty() {
440+
return Err(cargo_err(&format_args!("Dependency `{}` is hosted on another registry. Cross-registry dependencies are not permitted on crates.io.", &*dep.name)));
441+
}
442+
}
443+
444+
if let Ok(version_req) = semver::VersionReq::parse(&dep.version_req.0) {
445+
if version_req == semver::VersionReq::STAR {
446+
return Err(cargo_err(&format_args!("wildcard (`*`) dependency constraints are not allowed \
447+
on crates.io. Crate with this problem: `{}` See https://doc.rust-lang.org/cargo/faq.html#can-\
448+
libraries-use--as-a-version-for-their-dependencies for more \
449+
information", &*dep.name)));
450+
}
451+
}
452+
453+
Ok(())
454+
})
455+
.collect::<Result<Vec<_>, _>>()?;
456+
457+
Ok(())
458+
}
459+
432460
#[instrument(skip_all)]
433461
pub fn add_dependencies(
434462
conn: &mut PgConnection,
@@ -446,26 +474,14 @@ pub fn add_dependencies(
446474
let new_dependencies = deps
447475
.iter()
448476
.map(|dep| {
449-
if let Some(registry) = &dep.registry {
450-
if !registry.is_empty() {
451-
return Err(cargo_err(&format_args!("Dependency `{}` is hosted on another registry. Cross-registry dependencies are not permitted on crates.io.", &*dep.name)));
452-
}
453-
}
454-
455477
// Match only identical names to ensure the index always references the original crate name
456478
let Some(&crate_id) = crate_ids.get(&dep.name.0) else {
457-
return Err(cargo_err(&format_args!("no known crate named `{}`", &*dep.name)));
479+
return Err(cargo_err(&format_args!(
480+
"no known crate named `{}`",
481+
&*dep.name
482+
)));
458483
};
459484

460-
if let Ok(version_req) = semver::VersionReq::parse(&dep.version_req.0) {
461-
if version_req == semver::VersionReq::STAR {
462-
return Err(cargo_err(&format_args!("wildcard (`*`) dependency constraints are not allowed \
463-
on crates.io. Crate with this problem: `{}` See https://doc.rust-lang.org/cargo/faq.html#can-\
464-
libraries-use--as-a-version-for-their-dependencies for more \
465-
information", &*dep.name)));
466-
}
467-
}
468-
469485
Ok((
470486
dependencies::version_id.eq(version_id),
471487
dependencies::crate_id.eq(crate_id),
@@ -475,7 +491,7 @@ pub fn add_dependencies(
475491
dependencies::default_features.eq(dep.default_features),
476492
dependencies::features.eq(&dep.features),
477493
dependencies::target.eq(dep.target.as_deref()),
478-
dependencies::explicit_name.eq(dep.explicit_name_in_toml.as_deref())
494+
dependencies::explicit_name.eq(dep.explicit_name_in_toml.as_deref()),
479495
))
480496
})
481497
.collect::<Result<Vec<_>, _>>()?;

0 commit comments

Comments
 (0)