aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_project_model/src')
-rw-r--r--crates/ra_project_model/src/lib.rs59
1 files changed, 31 insertions, 28 deletions
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs
index c4d67d3dc..bcf12460d 100644
--- a/crates/ra_project_model/src/lib.rs
+++ b/crates/ra_project_model/src/lib.rs
@@ -25,35 +25,23 @@ pub use crate::{
25}; 25};
26 26
27#[derive(Clone, PartialEq, Eq, Hash, Debug)] 27#[derive(Clone, PartialEq, Eq, Hash, Debug)]
28pub struct CargoTomlNoneFoundError(pub PathBuf); 28pub struct CargoTomlNotFoundError {
29 29 pub searched_at: PathBuf,
30#[derive(Clone, PartialEq, Eq, Hash, Debug)] 30 pub reason: String,
31pub struct CargoTomlMultipleValidFoundError(pub Vec<PathBuf>);
32
33#[derive(Clone, PartialEq, Eq, Hash, Debug)]
34pub struct CargoTomlSearchFileSystemError(pub PathBuf, pub String);
35
36impl std::fmt::Display for CargoTomlNoneFoundError {
37 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 write!(fmt, "can't find Cargo.toml at {}", self.0.display())
39 }
40}
41
42impl std::fmt::Display for CargoTomlMultipleValidFoundError {
43 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 write!(fmt, "found multiple valid Cargo.toml files {:?}", self.0)
45 }
46} 31}
47 32
48impl std::fmt::Display for CargoTomlSearchFileSystemError { 33impl std::fmt::Display for CargoTomlNotFoundError {
49 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 34 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 write!(fmt, "a filesystem error occurred while searching for Cargo.toml: {}", self.1) 35 write!(
36 fmt,
37 "can't find Cargo.toml at {}, due to {}",
38 self.searched_at.display(),
39 self.reason
40 )
51 } 41 }
52} 42}
53 43
54impl Error for CargoTomlNoneFoundError {} 44impl Error for CargoTomlNotFoundError {}
55impl Error for CargoTomlMultipleValidFoundError {}
56impl Error for CargoTomlSearchFileSystemError {}
57 45
58#[derive(Debug, Clone)] 46#[derive(Debug, Clone)]
59pub enum ProjectWorkspace { 47pub enum ProjectWorkspace {
@@ -452,8 +440,6 @@ fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> {
452} 440}
453 441
454fn find_cargo_toml(path: &Path) -> Result<PathBuf> { 442fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
455 let path_as_buf = path.to_path_buf();
456
457 if path.ends_with("Cargo.toml") { 443 if path.ends_with("Cargo.toml") {
458 return Ok(path.to_path_buf()); 444 return Ok(path.to_path_buf());
459 } 445 }
@@ -464,14 +450,31 @@ fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
464 450
465 let entities = match read_dir(path) { 451 let entities = match read_dir(path) {
466 Ok(entities) => entities, 452 Ok(entities) => entities,
467 Err(e) => return Err(CargoTomlSearchFileSystemError(path_as_buf, e.to_string()).into()), 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 }
468 }; 460 };
469 461
470 let mut valid_canditates = find_cargo_toml_in_child_dir(entities); 462 let mut valid_canditates = find_cargo_toml_in_child_dir(entities);
471 match valid_canditates.len() { 463 match valid_canditates.len() {
472 1 => Ok(valid_canditates.remove(0)), 464 1 => Ok(valid_canditates.remove(0)),
473 0 => Err(CargoTomlNoneFoundError(path_as_buf).into()), 465 0 => Err(CargoTomlNotFoundError {
474 _ => Err(CargoTomlMultipleValidFoundError(valid_canditates).into()), 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()),
475 } 478 }
476} 479}
477 480