Simpify project discovery

This commit is contained in:
veetaha 2020-05-09 02:51:59 +03:00
parent aca9aa8b7e
commit e83a2912b8

View file

@ -88,46 +88,28 @@ impl ProjectRoot {
} }
pub fn discover(path: &Path) -> io::Result<Vec<ProjectRoot>> { pub fn discover(path: &Path) -> io::Result<Vec<ProjectRoot>> {
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 Ok(vec![ProjectRoot::ProjectJson(project_json)]);
} }
return find_cargo_toml(path) return find_cargo_toml(path)
.map(|paths| paths.into_iter().map(ProjectRoot::CargoToml).collect()); .map(|paths| paths.into_iter().map(ProjectRoot::CargoToml).collect());
fn find_rust_project_json(path: &Path) -> Option<PathBuf> {
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<Vec<PathBuf>> { fn find_cargo_toml(path: &Path) -> io::Result<Vec<PathBuf>> {
if path.ends_with("Cargo.toml") { match find_in_parent_dirs(path, "Cargo.toml") {
return Ok(vec![path.to_path_buf()]); 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]);
}
let entities = read_dir(path)?;
Ok(find_cargo_toml_in_child_dir(entities))
} }
fn find_cargo_toml_in_parent_dir(path: &Path) -> Option<PathBuf> { fn find_in_parent_dirs(path: &Path, target_file_name: &str) -> Option<PathBuf> {
if path.ends_with(target_file_name) {
return Some(path.to_owned());
}
let mut curr = Some(path); let mut curr = Some(path);
while let Some(path) = curr { while let Some(path) = curr {
let candidate = path.join("Cargo.toml"); let candidate = path.join(target_file_name);
if candidate.exists() { if candidate.exists() {
return Some(candidate); return Some(candidate);
} }
@ -139,14 +121,11 @@ impl ProjectRoot {
fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> { fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> {
// Only one level down to avoid cycles the easy way and stop a runaway scan with large projects // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects
let mut valid_canditates = vec![]; entities
for entity in entities.filter_map(Result::ok) { .filter_map(Result::ok)
let candidate = entity.path().join("Cargo.toml"); .map(|it| it.path().join("Cargo.toml"))
if candidate.exists() { .filter(|it| it.exists())
valid_canditates.push(candidate) .collect()
}
}
valid_canditates
} }
} }
} }