[ty] Add more path helper functions

This makes it easier to do exhaustive case analysis
on a `SearchPath` depending on whether it is a vendored
or system path.
This commit is contained in:
Andrew Gallant 2025-08-06 11:08:10 -04:00 committed by Andrew Gallant
parent f4be05a83b
commit e0c98874e2

View file

@ -621,29 +621,28 @@ impl SearchPath {
}
#[must_use]
pub(crate) fn as_system_path(&self) -> Option<&SystemPath> {
match &*self.0 {
SearchPathInner::Extra(path)
| SearchPathInner::FirstParty(path)
| SearchPathInner::StandardLibraryCustom(path)
| SearchPathInner::StandardLibraryReal(path)
| SearchPathInner::SitePackages(path)
| SearchPathInner::Editable(path) => Some(path),
SearchPathInner::StandardLibraryVendored(_) => None,
pub(super) fn as_path(&self) -> SystemOrVendoredPathRef<'_> {
match *self.0 {
SearchPathInner::Extra(ref path)
| SearchPathInner::FirstParty(ref path)
| SearchPathInner::StandardLibraryCustom(ref path)
| SearchPathInner::StandardLibraryReal(ref path)
| SearchPathInner::SitePackages(ref path)
| SearchPathInner::Editable(ref path) => SystemOrVendoredPathRef::System(path),
SearchPathInner::StandardLibraryVendored(ref path) => {
SystemOrVendoredPathRef::Vendored(path)
}
}
}
#[must_use]
pub(crate) fn as_vendored_path(&self) -> Option<&VendoredPath> {
match &*self.0 {
SearchPathInner::StandardLibraryVendored(path) => Some(path),
SearchPathInner::Extra(_)
| SearchPathInner::FirstParty(_)
| SearchPathInner::StandardLibraryCustom(_)
| SearchPathInner::StandardLibraryReal(_)
| SearchPathInner::SitePackages(_)
| SearchPathInner::Editable(_) => None,
pub(crate) fn as_system_path(&self) -> Option<&SystemPath> {
self.as_path().as_system_path()
}
#[must_use]
pub(crate) fn as_vendored_path(&self) -> Option<&VendoredPath> {
self.as_path().as_vendored_path()
}
}
@ -740,6 +739,20 @@ impl<'db> SystemOrVendoredPathRef<'db> {
Self::Vendored(vendored) => vendored.parent().map(Self::Vendored),
}
}
fn as_system_path(&self) -> Option<&'db SystemPath> {
match self {
SystemOrVendoredPathRef::System(path) => Some(path),
SystemOrVendoredPathRef::Vendored(_) => None,
}
}
fn as_vendored_path(&self) -> Option<&'db VendoredPath> {
match self {
SystemOrVendoredPathRef::Vendored(path) => Some(path),
SystemOrVendoredPathRef::System(_) => None,
}
}
}
impl std::fmt::Display for SystemOrVendoredPathRef<'_> {