From 2393e270ed3d548fd8f0aa9711048c1a5098616e Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 16 Dec 2022 21:28:19 -0500 Subject: [PATCH] Change a few more methods to take AsRef --- src/cache.rs | 12 ++++++------ src/fs.rs | 8 +++++--- src/resolver.rs | 2 +- src/settings/pyproject.rs | 18 ++++++++++++------ 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index 5e463ec232..fdd658fee4 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -42,10 +42,10 @@ fn content_dir() -> &'static str { "content" } -fn cache_key(path: &Path, settings: &Settings, autofix: fixer::Mode) -> u64 { +fn cache_key>(path: P, settings: &Settings, autofix: fixer::Mode) -> u64 { let mut hasher = DefaultHasher::new(); CARGO_PKG_VERSION.hash(&mut hasher); - path.absolutize().unwrap().hash(&mut hasher); + path.as_ref().absolutize().unwrap().hash(&mut hasher); settings.hash(&mut hasher); autofix.hash(&mut hasher); hasher.finish() @@ -91,8 +91,8 @@ fn read_sync(key: u64) -> Result, std::io::Error> { } /// Get a value from the cache. -pub fn get( - path: &Path, +pub fn get>( + path: P, metadata: &Metadata, settings: &Settings, autofix: fixer::Mode, @@ -120,8 +120,8 @@ pub fn get( } /// Set a value in the cache. -pub fn set( - path: &Path, +pub fn set>( + path: P, metadata: &Metadata, settings: &Settings, autofix: fixer::Mode, diff --git a/src/fs.rs b/src/fs.rs index e67e52c156..f58e0da5ef 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -40,7 +40,8 @@ pub(crate) fn ignores_from_path<'a>( /// Convert any path to an absolute path (based on the current working /// directory). -pub fn normalize_path(path: &Path) -> PathBuf { +pub fn normalize_path>(path: P) -> PathBuf { + let path = path.as_ref(); if let Ok(path) = path.absolutize() { return path.to_path_buf(); } @@ -48,8 +49,9 @@ pub fn normalize_path(path: &Path) -> PathBuf { } /// Convert any path to an absolute path (based on the specified project root). -pub fn normalize_path_to(path: &Path, project_root: &Path) -> PathBuf { - if let Ok(path) = path.absolutize_from(project_root) { +pub fn normalize_path_to, R: AsRef>(path: P, project_root: R) -> PathBuf { + let path = path.as_ref(); + if let Ok(path) = path.absolutize_from(project_root.as_ref()) { return path.to_path_buf(); } path.to_path_buf() diff --git a/src/resolver.rs b/src/resolver.rs index cf68001622..3d8f890867 100644 --- a/src/resolver.rs +++ b/src/resolver.rs @@ -189,7 +189,7 @@ pub fn python_files_in_path( overrides: &Overrides, ) -> Result<(Vec>, Resolver)> { // Normalize every path (e.g., convert from relative to absolute). - let paths: Vec = paths.iter().map(|path| fs::normalize_path(path)).collect(); + let paths: Vec = paths.iter().map(fs::normalize_path).collect(); // Search for `pyproject.toml` files in all parent directories. let mut resolver = Resolver::default(); diff --git a/src/settings/pyproject.rs b/src/settings/pyproject.rs index 8ae6690979..66704f4f6c 100644 --- a/src/settings/pyproject.rs +++ b/src/settings/pyproject.rs @@ -40,8 +40,8 @@ pub fn has_ruff_section>(path: P) -> Result { } /// Find the path to the `pyproject.toml` file, if such a file exists. -pub fn find_pyproject_toml(path: &Path) -> Result> { - for directory in path.ancestors() { +pub fn find_pyproject_toml>(path: P) -> Result> { + for directory in path.as_ref().ancestors() { let pyproject = directory.join("pyproject.toml"); if pyproject.is_file() && has_ruff_section(&pyproject)? { return Ok(Some(pyproject)); @@ -63,9 +63,15 @@ pub fn find_user_pyproject_toml() -> Option { } /// Load `Options` from a `pyproject.toml`. -pub fn load_options(pyproject: &Path) -> Result { - Ok(parse_pyproject_toml(pyproject) - .map_err(|err| anyhow!("Failed to parse `{}`: {}", pyproject.to_string_lossy(), err))? +pub fn load_options>(pyproject: P) -> Result { + Ok(parse_pyproject_toml(&pyproject) + .map_err(|err| { + anyhow!( + "Failed to parse `{}`: {}", + pyproject.as_ref().to_string_lossy(), + err + ) + })? .tool .and_then(|tool| tool.ruff) .unwrap_or_default()) @@ -373,7 +379,7 @@ other-attribute = 1 fn find_and_parse_pyproject_toml() -> Result<()> { let cwd = current_dir()?; let pyproject = - find_pyproject_toml(&cwd.join("resources/test/fixtures/__init__.py"))?.unwrap(); + find_pyproject_toml(cwd.join("resources/test/fixtures/__init__.py"))?.unwrap(); assert_eq!( pyproject, cwd.join("resources/test/fixtures/pyproject.toml")