chore: Avoid `collect`

This commit is contained in:
Dmitry Dygalo 2022-09-14 10:32:30 +02:00
parent b7e2a4b9a9
commit a711569c2c
No known key found for this signature in database
GPG Key ID: 0D78E60518FE18BB
3 changed files with 16 additions and 17 deletions

View File

@ -1,4 +1,4 @@
use std::path::{Path, PathBuf}; use std::path::PathBuf;
use std::process::ExitCode; use std::process::ExitCode;
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::time::Instant; use std::time::Instant;
@ -183,9 +183,7 @@ fn inner_main() -> Result<ExitCode> {
set_up_logging(cli.verbose)?; set_up_logging(cli.verbose)?;
// TODO(charlie): Can we avoid this cast? let mut settings = Settings::from_paths(&cli.files)?;
let paths: Vec<&Path> = cli.files.iter().map(PathBuf::as_path).collect();
let mut settings = Settings::from_paths(paths)?;
if !cli.select.is_empty() { if !cli.select.is_empty() {
settings.select(cli.select); settings.select(cli.select);
} }

View File

@ -8,7 +8,7 @@ use serde::Deserialize;
use crate::checks::CheckCode; use crate::checks::CheckCode;
use crate::fs; use crate::fs;
pub fn load_config<'a>(paths: impl IntoIterator<Item = &'a Path>) -> Result<(PathBuf, Config)> { pub fn load_config(paths: &[PathBuf]) -> (PathBuf, Config) {
match find_project_root(paths) { match find_project_root(paths) {
Some(project_root) => match find_pyproject_toml(&project_root) { Some(project_root) => match find_pyproject_toml(&project_root) {
Some(path) => { Some(path) => {
@ -19,18 +19,18 @@ pub fn load_config<'a>(paths: impl IntoIterator<Item = &'a Path>) -> Result<(Pat
.tool .tool
.and_then(|tool| tool.ruff) .and_then(|tool| tool.ruff)
.unwrap_or_default(); .unwrap_or_default();
Ok((project_root, config)) (project_root, config)
} }
Err(e) => { Err(e) => {
println!("Failed to load pyproject.toml: {:?}", e); println!("Failed to load pyproject.toml: {:?}", e);
println!("Falling back to default configuration..."); println!("Falling back to default configuration...");
Ok(Default::default()) Default::default()
} }
} }
} }
None => Ok(Default::default()), None => Default::default(),
}, },
None => Ok(Default::default()), None => Default::default(),
} }
} }
@ -70,8 +70,8 @@ fn find_user_pyproject_toml() -> Option<PathBuf> {
dirs::home_dir().map(|path| path.join(".ruff")) dirs::home_dir().map(|path| path.join(".ruff"))
} }
fn find_project_root<'a>(sources: impl IntoIterator<Item = &'a Path>) -> Option<PathBuf> { fn find_project_root(sources: &[PathBuf]) -> Option<PathBuf> {
if let Some(prefix) = common_path_all(sources) { if let Some(prefix) = common_path_all(sources.iter().map(PathBuf::as_path)) {
for directory in prefix.ancestors() { for directory in prefix.ancestors() {
if directory.join(".git").is_dir() { if directory.join(".git").is_dir() {
return Some(directory.to_path_buf()); return Some(directory.to_path_buf());
@ -90,7 +90,7 @@ fn find_project_root<'a>(sources: impl IntoIterator<Item = &'a Path>) -> Option<
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::path::Path; use std::path::{Path, PathBuf};
use anyhow::Result; use anyhow::Result;
@ -238,7 +238,8 @@ other-attribute = 1
#[test] #[test]
fn find_and_parse_pyproject_toml() -> Result<()> { fn find_and_parse_pyproject_toml() -> Result<()> {
let project_root = find_project_root([Path::new("resources/test/fixtures/__init__.py")]) let project_root =
find_project_root(&[PathBuf::from("resources/test/fixtures/__init__.py")])
.expect("Unable to find project root."); .expect("Unable to find project root.");
assert_eq!(project_root, Path::new("resources/test/fixtures")); assert_eq!(project_root, Path::new("resources/test/fixtures"));

View File

@ -1,6 +1,6 @@
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::path::Path; use std::path::PathBuf;
use anyhow::Result; use anyhow::Result;
use glob::Pattern; use glob::Pattern;
@ -25,8 +25,8 @@ impl Hash for Settings {
} }
impl Settings { impl Settings {
pub fn from_paths<'a>(paths: impl IntoIterator<Item = &'a Path>) -> Result<Self> { pub fn from_paths(paths: &[PathBuf]) -> Result<Self> {
let (project_root, config) = load_config(paths)?; let (project_root, config) = load_config(paths);
let mut settings = Settings { let mut settings = Settings {
line_length: config.line_length.unwrap_or(88), line_length: config.line_length.unwrap_or(88),
exclude: config exclude: config