From e83a2912b8deaab560d1ea39232c06a29530d6e5 Mon Sep 17 00:00:00 2001 From: veetaha Date: Sat, 9 May 2020 02:51:59 +0300 Subject: Simpify project discovery --- crates/ra_project_model/src/lib.rs | 51 +++++++++++--------------------------- 1 file changed, 15 insertions(+), 36 deletions(-) (limited to 'crates') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 3adb8baf6..4f098b706 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -88,46 +88,28 @@ impl ProjectRoot { } pub fn discover(path: &Path) -> io::Result> { - if let Some(project_json) = find_rust_project_json(path) { + if let Some(project_json) = find_in_parent_dirs(path, "rust-project.json") { return Ok(vec![ProjectRoot::ProjectJson(project_json)]); } return find_cargo_toml(path) .map(|paths| paths.into_iter().map(ProjectRoot::CargoToml).collect()); - fn find_rust_project_json(path: &Path) -> Option { - if path.ends_with("rust-project.json") { - return Some(path.to_path_buf()); - } - - let mut curr = Some(path); - while let Some(path) = curr { - let candidate = path.join("rust-project.json"); - if candidate.exists() { - return Some(candidate); - } - curr = path.parent(); - } - - None - } - fn find_cargo_toml(path: &Path) -> io::Result> { - if path.ends_with("Cargo.toml") { - return Ok(vec![path.to_path_buf()]); + match find_in_parent_dirs(path, "Cargo.toml") { + Some(it) => Ok(vec![it]), + None => Ok(find_cargo_toml_in_child_dir(read_dir(path)?)), } + } - if let Some(p) = find_cargo_toml_in_parent_dir(path) { - return Ok(vec![p]); + fn find_in_parent_dirs(path: &Path, target_file_name: &str) -> Option { + if path.ends_with(target_file_name) { + return Some(path.to_owned()); } - let entities = read_dir(path)?; - Ok(find_cargo_toml_in_child_dir(entities)) - } - - fn find_cargo_toml_in_parent_dir(path: &Path) -> Option { let mut curr = Some(path); + while let Some(path) = curr { - let candidate = path.join("Cargo.toml"); + let candidate = path.join(target_file_name); if candidate.exists() { return Some(candidate); } @@ -139,14 +121,11 @@ impl ProjectRoot { fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec { // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects - let mut valid_canditates = vec![]; - for entity in entities.filter_map(Result::ok) { - let candidate = entity.path().join("Cargo.toml"); - if candidate.exists() { - valid_canditates.push(candidate) - } - } - valid_canditates + entities + .filter_map(Result::ok) + .map(|it| it.path().join("Cargo.toml")) + .filter(|it| it.exists()) + .collect() } } } -- cgit v1.2.3