Change a few more methods to take AsRef<Path>

This commit is contained in:
Charlie Marsh 2022-12-16 21:28:19 -05:00
parent f36e6035c8
commit 2393e270ed
4 changed files with 24 additions and 16 deletions

View file

@ -42,10 +42,10 @@ fn content_dir() -> &'static str {
"content" "content"
} }
fn cache_key(path: &Path, settings: &Settings, autofix: fixer::Mode) -> u64 { fn cache_key<P: AsRef<Path>>(path: P, settings: &Settings, autofix: fixer::Mode) -> u64 {
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();
CARGO_PKG_VERSION.hash(&mut hasher); CARGO_PKG_VERSION.hash(&mut hasher);
path.absolutize().unwrap().hash(&mut hasher); path.as_ref().absolutize().unwrap().hash(&mut hasher);
settings.hash(&mut hasher); settings.hash(&mut hasher);
autofix.hash(&mut hasher); autofix.hash(&mut hasher);
hasher.finish() hasher.finish()
@ -91,8 +91,8 @@ fn read_sync(key: u64) -> Result<Vec<u8>, std::io::Error> {
} }
/// Get a value from the cache. /// Get a value from the cache.
pub fn get( pub fn get<P: AsRef<Path>>(
path: &Path, path: P,
metadata: &Metadata, metadata: &Metadata,
settings: &Settings, settings: &Settings,
autofix: fixer::Mode, autofix: fixer::Mode,
@ -120,8 +120,8 @@ pub fn get(
} }
/// Set a value in the cache. /// Set a value in the cache.
pub fn set( pub fn set<P: AsRef<Path>>(
path: &Path, path: P,
metadata: &Metadata, metadata: &Metadata,
settings: &Settings, settings: &Settings,
autofix: fixer::Mode, autofix: fixer::Mode,

View file

@ -40,7 +40,8 @@ pub(crate) fn ignores_from_path<'a>(
/// Convert any path to an absolute path (based on the current working /// Convert any path to an absolute path (based on the current working
/// directory). /// directory).
pub fn normalize_path(path: &Path) -> PathBuf { pub fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
let path = path.as_ref();
if let Ok(path) = path.absolutize() { if let Ok(path) = path.absolutize() {
return path.to_path_buf(); 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). /// Convert any path to an absolute path (based on the specified project root).
pub fn normalize_path_to(path: &Path, project_root: &Path) -> PathBuf { pub fn normalize_path_to<P: AsRef<Path>, R: AsRef<Path>>(path: P, project_root: R) -> PathBuf {
if let Ok(path) = path.absolutize_from(project_root) { let path = path.as_ref();
if let Ok(path) = path.absolutize_from(project_root.as_ref()) {
return path.to_path_buf(); return path.to_path_buf();
} }
path.to_path_buf() path.to_path_buf()

View file

@ -189,7 +189,7 @@ pub fn python_files_in_path(
overrides: &Overrides, overrides: &Overrides,
) -> Result<(Vec<Result<DirEntry, ignore::Error>>, Resolver)> { ) -> Result<(Vec<Result<DirEntry, ignore::Error>>, Resolver)> {
// Normalize every path (e.g., convert from relative to absolute). // Normalize every path (e.g., convert from relative to absolute).
let paths: Vec<PathBuf> = paths.iter().map(|path| fs::normalize_path(path)).collect(); let paths: Vec<PathBuf> = paths.iter().map(fs::normalize_path).collect();
// Search for `pyproject.toml` files in all parent directories. // Search for `pyproject.toml` files in all parent directories.
let mut resolver = Resolver::default(); let mut resolver = Resolver::default();

View file

@ -40,8 +40,8 @@ pub fn has_ruff_section<P: AsRef<Path>>(path: P) -> Result<bool> {
} }
/// Find the path to the `pyproject.toml` file, if such a file exists. /// Find the path to the `pyproject.toml` file, if such a file exists.
pub fn find_pyproject_toml(path: &Path) -> Result<Option<PathBuf>> { pub fn find_pyproject_toml<P: AsRef<Path>>(path: P) -> Result<Option<PathBuf>> {
for directory in path.ancestors() { for directory in path.as_ref().ancestors() {
let pyproject = directory.join("pyproject.toml"); let pyproject = directory.join("pyproject.toml");
if pyproject.is_file() && has_ruff_section(&pyproject)? { if pyproject.is_file() && has_ruff_section(&pyproject)? {
return Ok(Some(pyproject)); return Ok(Some(pyproject));
@ -63,9 +63,15 @@ pub fn find_user_pyproject_toml() -> Option<PathBuf> {
} }
/// Load `Options` from a `pyproject.toml`. /// Load `Options` from a `pyproject.toml`.
pub fn load_options(pyproject: &Path) -> Result<Options> { pub fn load_options<P: AsRef<Path>>(pyproject: P) -> Result<Options> {
Ok(parse_pyproject_toml(pyproject) Ok(parse_pyproject_toml(&pyproject)
.map_err(|err| anyhow!("Failed to parse `{}`: {}", pyproject.to_string_lossy(), err))? .map_err(|err| {
anyhow!(
"Failed to parse `{}`: {}",
pyproject.as_ref().to_string_lossy(),
err
)
})?
.tool .tool
.and_then(|tool| tool.ruff) .and_then(|tool| tool.ruff)
.unwrap_or_default()) .unwrap_or_default())
@ -373,7 +379,7 @@ other-attribute = 1
fn find_and_parse_pyproject_toml() -> Result<()> { fn find_and_parse_pyproject_toml() -> Result<()> {
let cwd = current_dir()?; let cwd = current_dir()?;
let pyproject = 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!( assert_eq!(
pyproject, pyproject,
cwd.join("resources/test/fixtures/pyproject.toml") cwd.join("resources/test/fixtures/pyproject.toml")