[red-knot] Simplify conversions from `std::path::Path` to `VendoredPath(Buf)` (#11988)

This commit is contained in:
Alex Waygood 2024-06-23 15:52:26 +01:00 committed by GitHub
parent f846fc9e07
commit 375d2c87b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 6 deletions

View File

@ -49,12 +49,8 @@ mod tests {
panic!("Expected {absolute_path:?} to be a child of {vendored_typeshed_dir:?}") panic!("Expected {absolute_path:?} to be a child of {vendored_typeshed_dir:?}")
}); });
let posix_style_path = relative_path let vendored_path = <&VendoredPath>::try_from(relative_path)
.as_os_str() .unwrap_or_else(|_| panic!("Expected {relative_path:?} to be valid UTF-8"));
.to_str()
.unwrap_or_else(|| panic!("Expected {relative_path:?} to be a valid UTF-8 path"));
let vendored_path = VendoredPath::new(posix_style_path);
assert!( assert!(
vendored_typeshed_stubs.exists(vendored_path), vendored_typeshed_stubs.exists(vendored_path),

View File

@ -93,3 +93,19 @@ impl Deref for VendoredPathBuf {
self.as_path() self.as_path()
} }
} }
impl<'a> TryFrom<&'a path::Path> for &'a VendoredPath {
type Error = camino::FromPathError;
fn try_from(value: &'a path::Path) -> Result<Self, Self::Error> {
Ok(VendoredPath::new(<&camino::Utf8Path>::try_from(value)?))
}
}
impl TryFrom<path::PathBuf> for VendoredPathBuf {
type Error = camino::FromPathBufError;
fn try_from(value: path::PathBuf) -> Result<Self, Self::Error> {
Ok(VendoredPathBuf(camino::Utf8PathBuf::try_from(value)?))
}
}