refactor: Centralize SourceId creation for manifests

This commit is contained in:
Ed Page 2025-02-11 14:00:10 -06:00
parent b64750d83d
commit 5a351405dc
2 changed files with 12 additions and 5 deletions

View File

@ -204,6 +204,17 @@ impl SourceId {
SourceId::new(SourceKind::Path, url, None)
}
/// Creates a `SourceId` from a filesystem path.
///
/// `path`: an absolute path.
pub fn for_manifest_path(manifest_path: &Path) -> CargoResult<SourceId> {
if crate::util::toml::is_embedded(manifest_path) {
Self::for_path(manifest_path)
} else {
Self::for_path(manifest_path.parent().unwrap())
}
}
/// Creates a `SourceId` from a Git reference.
pub fn for_git(url: &Url, reference: GitReference) -> CargoResult<SourceId> {
SourceId::new(SourceKind::Git(reference), url.clone(), None)

View File

@ -1817,11 +1817,7 @@ impl<'gctx> Packages<'gctx> {
match self.packages.entry(manifest_path.to_path_buf()) {
Entry::Occupied(e) => Ok(e.into_mut()),
Entry::Vacant(v) => {
let source_id = if crate::util::toml::is_embedded(manifest_path) {
SourceId::for_path(manifest_path)?
} else {
SourceId::for_path(manifest_path.parent().unwrap())?
};
let source_id = SourceId::for_manifest_path(manifest_path)?;
let manifest = read_manifest(manifest_path, source_id, self.gctx)?;
Ok(v.insert(match manifest {
EitherManifest::Real(manifest) => {