From 375d2c87b2bc5e5a2c4affaf7569606ecc46f3e0 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 23 Jun 2024 15:52:26 +0100 Subject: [PATCH] [red-knot] Simplify conversions from `std::path::Path` to `VendoredPath(Buf)` (#11988) --- crates/red_knot_module_resolver/src/typeshed.rs | 8 ++------ crates/ruff_db/src/vendored/path.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/red_knot_module_resolver/src/typeshed.rs b/crates/red_knot_module_resolver/src/typeshed.rs index 725a337330..fa49261d5f 100644 --- a/crates/red_knot_module_resolver/src/typeshed.rs +++ b/crates/red_knot_module_resolver/src/typeshed.rs @@ -49,12 +49,8 @@ mod tests { panic!("Expected {absolute_path:?} to be a child of {vendored_typeshed_dir:?}") }); - let posix_style_path = relative_path - .as_os_str() - .to_str() - .unwrap_or_else(|| panic!("Expected {relative_path:?} to be a valid UTF-8 path")); - - let vendored_path = VendoredPath::new(posix_style_path); + let vendored_path = <&VendoredPath>::try_from(relative_path) + .unwrap_or_else(|_| panic!("Expected {relative_path:?} to be valid UTF-8")); assert!( vendored_typeshed_stubs.exists(vendored_path), diff --git a/crates/ruff_db/src/vendored/path.rs b/crates/ruff_db/src/vendored/path.rs index 550d4c11a2..194d3e8ff8 100644 --- a/crates/ruff_db/src/vendored/path.rs +++ b/crates/ruff_db/src/vendored/path.rs @@ -93,3 +93,19 @@ impl Deref for VendoredPathBuf { self.as_path() } } + +impl<'a> TryFrom<&'a path::Path> for &'a VendoredPath { + type Error = camino::FromPathError; + + fn try_from(value: &'a path::Path) -> Result { + Ok(VendoredPath::new(<&camino::Utf8Path>::try_from(value)?)) + } +} + +impl TryFrom for VendoredPathBuf { + type Error = camino::FromPathBufError; + + fn try_from(value: path::PathBuf) -> Result { + Ok(VendoredPathBuf(camino::Utf8PathBuf::try_from(value)?)) + } +}