Add PubGrubPackage::name_no_root (#4542)

Small code style improvement.
This commit is contained in:
konsti 2024-06-26 14:29:26 +02:00 committed by GitHub
parent 89bef7bf09
commit d7f195fdc9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 72 additions and 76 deletions

View file

@ -132,6 +132,18 @@ impl PubGrubPackage {
} }
} }
/// Returns the name of this PubGrub package, if it is not the root package or a Python version
/// constraint.
pub(crate) fn name_no_root(&self) -> Option<&PackageName> {
match &**self {
PubGrubPackageInner::Root(_) | PubGrubPackageInner::Python(_) => None,
PubGrubPackageInner::Package { name, .. }
| PubGrubPackageInner::Extra { name, .. }
| PubGrubPackageInner::Dev { name, .. }
| PubGrubPackageInner::Marker { name, .. } => Some(name),
}
}
/// Returns the marker expression associated with this PubGrub package, if /// Returns the marker expression associated with this PubGrub package, if
/// it has one. /// it has one.
pub(crate) fn marker(&self) -> Option<&MarkerTree> { pub(crate) fn marker(&self) -> Option<&MarkerTree> {

View file

@ -31,53 +31,49 @@ impl PubGrubPriorities {
urls: &ForkUrls, urls: &ForkUrls,
) { ) {
let next = self.0.len(); let next = self.0.len();
match &**package { // The root package and Python constraints have no explicit priority, the root package is
PubGrubPackageInner::Root(_) => {} // always first and the Python version (range) is fixed.
PubGrubPackageInner::Python(_) => {} let Some(name) = package.name_no_root() else {
return;
};
PubGrubPackageInner::Marker { name, .. } match self.0.entry(name.clone()) {
| PubGrubPackageInner::Extra { name, .. } std::collections::hash_map::Entry::Occupied(mut entry) => {
| PubGrubPackageInner::Dev { name, .. } // Preserve the original index.
| PubGrubPackageInner::Package { name, .. } => { let index = match entry.get() {
match self.0.entry(name.clone()) { PubGrubPriority::Unspecified(Reverse(index)) => *index,
std::collections::hash_map::Entry::Occupied(mut entry) => { PubGrubPriority::Singleton(Reverse(index)) => *index,
// Preserve the original index. PubGrubPriority::DirectUrl(Reverse(index)) => *index,
let index = match entry.get() { PubGrubPriority::Root => next,
PubGrubPriority::Unspecified(Reverse(index)) => *index, };
PubGrubPriority::Singleton(Reverse(index)) => *index,
PubGrubPriority::DirectUrl(Reverse(index)) => *index,
PubGrubPriority::Root => next,
};
// Compute the priority. // Compute the priority.
let priority = if urls.get(name).is_some() { let priority = if urls.get(name).is_some() {
PubGrubPriority::DirectUrl(Reverse(index)) PubGrubPriority::DirectUrl(Reverse(index))
} else if version.as_singleton().is_some() { } else if version.as_singleton().is_some() {
PubGrubPriority::Singleton(Reverse(index)) PubGrubPriority::Singleton(Reverse(index))
} else { } else {
PubGrubPriority::Unspecified(Reverse(index)) PubGrubPriority::Unspecified(Reverse(index))
}; };
// Take the maximum of the new and existing priorities. // Take the maximum of the new and existing priorities.
if priority > *entry.get() { if priority > *entry.get() {
entry.insert(priority); entry.insert(priority);
}
}
std::collections::hash_map::Entry::Vacant(entry) => {
// Compute the priority.
let priority = if urls.get(name).is_some() {
PubGrubPriority::DirectUrl(Reverse(next))
} else if version.as_singleton().is_some() {
PubGrubPriority::Singleton(Reverse(next))
} else {
PubGrubPriority::Unspecified(Reverse(next))
};
// Insert the priority.
entry.insert(priority);
}
} }
} }
std::collections::hash_map::Entry::Vacant(entry) => {
// Compute the priority.
let priority = if urls.get(name).is_some() {
PubGrubPriority::DirectUrl(Reverse(next))
} else if version.as_singleton().is_some() {
PubGrubPriority::Singleton(Reverse(next))
} else {
PubGrubPriority::Unspecified(Reverse(next))
};
// Insert the priority.
entry.insert(priority);
}
} }
} }

View file

@ -667,43 +667,31 @@ impl<InstalledPackages: InstalledPackagesProvider> ResolverState<InstalledPackag
url: Option<&VerbatimParsedUrl>, url: Option<&VerbatimParsedUrl>,
request_sink: &Sender<Request>, request_sink: &Sender<Request>,
) -> Result<(), ResolveError> { ) -> Result<(), ResolveError> {
match (&**package, url) { // Only request real package
(PubGrubPackageInner::Root(_), _) => {} let Some(name) = package.name_no_root() else {
(PubGrubPackageInner::Python(_), _) => {} return Ok(());
( };
PubGrubPackageInner::Marker { name, .. }
| PubGrubPackageInner::Extra { name, .. }
| PubGrubPackageInner::Dev { name, .. }
| PubGrubPackageInner::Package { name, .. },
None,
) => {
// Verify that the package is allowed under the hash-checking policy.
if !self.hasher.allows_package(name) {
return Err(ResolveError::UnhashedPackage(name.clone()));
}
// Emit a request to fetch the metadata for this package. if let Some(url) = url {
if self.index.packages().register(name.clone()) { // Verify that the package is allowed under the hash-checking policy.
request_sink.blocking_send(Request::Package(name.clone()))?; if !self.hasher.allows_url(&url.verbatim) {
} return Err(ResolveError::UnhashedPackage(name.clone()));
} }
(
PubGrubPackageInner::Marker { name, .. }
| PubGrubPackageInner::Extra { name, .. }
| PubGrubPackageInner::Dev { name, .. }
| PubGrubPackageInner::Package { name, .. },
Some(url),
) => {
// Verify that the package is allowed under the hash-checking policy.
if !self.hasher.allows_url(&url.verbatim) {
return Err(ResolveError::UnhashedPackage(name.clone()));
}
// Emit a request to fetch the metadata for this distribution. // Emit a request to fetch the metadata for this distribution.
let dist = Dist::from_url(name.clone(), url.clone())?; let dist = Dist::from_url(name.clone(), url.clone())?;
if self.index.distributions().register(dist.version_id()) { if self.index.distributions().register(dist.version_id()) {
request_sink.blocking_send(Request::Dist(dist))?; request_sink.blocking_send(Request::Dist(dist))?;
} }
} else {
// Verify that the package is allowed under the hash-checking policy.
if !self.hasher.allows_package(name) {
return Err(ResolveError::UnhashedPackage(name.clone()));
}
// Emit a request to fetch the metadata for this package.
if self.index.packages().register(name.clone()) {
request_sink.blocking_send(Request::Package(name.clone()))?;
} }
} }
Ok(()) Ok(())