mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:34:57 +00:00
[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:
parent
f4be05a83b
commit
e0c98874e2
1 changed files with 31 additions and 18 deletions
|
@ -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_system_path(&self) -> Option<&SystemPath> {
|
||||
self.as_path().as_system_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,
|
||||
}
|
||||
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<'_> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue