aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_project_model')
-rw-r--r--crates/ra_project_model/src/lib.rs75
1 files changed, 66 insertions, 9 deletions
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs
index 9df6a0e07..bcf12460d 100644
--- a/crates/ra_project_model/src/lib.rs
+++ b/crates/ra_project_model/src/lib.rs
@@ -6,7 +6,7 @@ mod sysroot;
6 6
7use std::{ 7use std::{
8 error::Error, 8 error::Error,
9 fs::File, 9 fs::{read_dir, File, ReadDir},
10 io::BufReader, 10 io::BufReader,
11 path::{Path, PathBuf}, 11 path::{Path, PathBuf},
12 process::Command, 12 process::Command,
@@ -25,11 +25,19 @@ pub use crate::{
25}; 25};
26 26
27#[derive(Clone, PartialEq, Eq, Hash, Debug)] 27#[derive(Clone, PartialEq, Eq, Hash, Debug)]
28pub struct CargoTomlNotFoundError(pub PathBuf); 28pub struct CargoTomlNotFoundError {
29 pub searched_at: PathBuf,
30 pub reason: String,
31}
29 32
30impl std::fmt::Display for CargoTomlNotFoundError { 33impl std::fmt::Display for CargoTomlNotFoundError {
31 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 34 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32 write!(fmt, "can't find Cargo.toml at {}", self.0.display()) 35 write!(
36 fmt,
37 "can't find Cargo.toml at {}, due to {}",
38 self.searched_at.display(),
39 self.reason
40 )
33 } 41 }
34} 42}
35 43
@@ -406,19 +414,68 @@ fn find_rust_project_json(path: &Path) -> Option<PathBuf> {
406 None 414 None
407} 415}
408 416
409fn find_cargo_toml(path: &Path) -> Result<PathBuf> { 417fn find_cargo_toml_in_parent_dir(path: &Path) -> Option<PathBuf> {
410 if path.ends_with("Cargo.toml") {
411 return Ok(path.to_path_buf());
412 }
413 let mut curr = Some(path); 418 let mut curr = Some(path);
414 while let Some(path) = curr { 419 while let Some(path) = curr {
415 let candidate = path.join("Cargo.toml"); 420 let candidate = path.join("Cargo.toml");
416 if candidate.exists() { 421 if candidate.exists() {
417 return Ok(candidate); 422 return Some(candidate);
418 } 423 }
419 curr = path.parent(); 424 curr = path.parent();
420 } 425 }
421 Err(CargoTomlNotFoundError(path.to_path_buf()).into()) 426
427 None
428}
429
430fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> {
431 // Only one level down to avoid cycles the easy way and stop a runaway scan with large projects
432 let mut valid_canditates = vec![];
433 for entity in entities.filter_map(Result::ok) {
434 let candidate = entity.path().join("Cargo.toml");
435 if candidate.exists() {
436 valid_canditates.push(candidate)
437 }
438 }
439 valid_canditates
440}
441
442fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
443 if path.ends_with("Cargo.toml") {
444 return Ok(path.to_path_buf());
445 }
446
447 if let Some(p) = find_cargo_toml_in_parent_dir(path) {
448 return Ok(p);
449 }
450
451 let entities = match read_dir(path) {
452 Ok(entities) => entities,
453 Err(e) => {
454 return Err(CargoTomlNotFoundError {
455 searched_at: path.to_path_buf(),
456 reason: format!("file system error: {}", e),
457 }
458 .into());
459 }
460 };
461
462 let mut valid_canditates = find_cargo_toml_in_child_dir(entities);
463 match valid_canditates.len() {
464 1 => Ok(valid_canditates.remove(0)),
465 0 => Err(CargoTomlNotFoundError {
466 searched_at: path.to_path_buf(),
467 reason: "no Cargo.toml file found".to_string(),
468 }
469 .into()),
470 _ => Err(CargoTomlNotFoundError {
471 searched_at: path.to_path_buf(),
472 reason: format!(
473 "multiple equally valid Cargo.toml files found: {:?}",
474 valid_canditates
475 ),
476 }
477 .into()),
478 }
422} 479}
423 480
424pub fn get_rustc_cfg_options() -> CfgOptions { 481pub fn get_rustc_cfg_options() -> CfgOptions {