mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 20:39:13 +00:00
Change a few more methods to take AsRef<Path>
This commit is contained in:
parent
f36e6035c8
commit
2393e270ed
4 changed files with 24 additions and 16 deletions
12
src/cache.rs
12
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<P: AsRef<Path>>(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<Vec<u8>, std::io::Error> {
|
|||
}
|
||||
|
||||
/// Get a value from the cache.
|
||||
pub fn get(
|
||||
path: &Path,
|
||||
pub fn get<P: AsRef<Path>>(
|
||||
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<P: AsRef<Path>>(
|
||||
path: P,
|
||||
metadata: &Metadata,
|
||||
settings: &Settings,
|
||||
autofix: fixer::Mode,
|
||||
|
|
|
@ -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<P: AsRef<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<P: AsRef<Path>, R: AsRef<Path>>(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()
|
||||
|
|
|
@ -189,7 +189,7 @@ pub fn python_files_in_path(
|
|||
overrides: &Overrides,
|
||||
) -> Result<(Vec<Result<DirEntry, ignore::Error>>, Resolver)> {
|
||||
// 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.
|
||||
let mut resolver = Resolver::default();
|
||||
|
|
|
@ -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.
|
||||
pub fn find_pyproject_toml(path: &Path) -> Result<Option<PathBuf>> {
|
||||
for directory in path.ancestors() {
|
||||
pub fn find_pyproject_toml<P: AsRef<Path>>(path: P) -> Result<Option<PathBuf>> {
|
||||
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<PathBuf> {
|
|||
}
|
||||
|
||||
/// Load `Options` from a `pyproject.toml`.
|
||||
pub fn load_options(pyproject: &Path) -> Result<Options> {
|
||||
Ok(parse_pyproject_toml(pyproject)
|
||||
.map_err(|err| anyhow!("Failed to parse `{}`: {}", pyproject.to_string_lossy(), err))?
|
||||
pub fn load_options<P: AsRef<Path>>(pyproject: P) -> Result<Options> {
|
||||
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")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue