diff options
author | nmio <[email protected]> | 2020-02-29 13:05:10 +0000 |
---|---|---|
committer | nmio <[email protected]> | 2020-02-29 13:05:10 +0000 |
commit | e15424c1b7d5a82545fbb3744af3123a124053d5 (patch) | |
tree | 77e6c328767cc7f62657f2c1872d9403cced09bf | |
parent | 12b595e817b9cb47b3abb5ca4455e2ae9e95f58b (diff) |
keep one CargoTomlNotFoundError
-rw-r--r-- | crates/ra_project_model/src/lib.rs | 59 | ||||
-rw-r--r-- | crates/rust-analyzer/src/main_loop.rs | 7 |
2 files changed, 37 insertions, 29 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)] |
28 | pub struct CargoTomlNoneFoundError(pub PathBuf); | 28 | pub struct CargoTomlNotFoundError { |
29 | 29 | pub searched_at: PathBuf, | |
30 | #[derive(Clone, PartialEq, Eq, Hash, Debug)] | 30 | pub reason: String, |
31 | pub struct CargoTomlMultipleValidFoundError(pub Vec<PathBuf>); | ||
32 | |||
33 | #[derive(Clone, PartialEq, Eq, Hash, Debug)] | ||
34 | pub struct CargoTomlSearchFileSystemError(pub PathBuf, pub String); | ||
35 | |||
36 | impl 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 | |||
42 | impl 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 | ||
48 | impl std::fmt::Display for CargoTomlSearchFileSystemError { | 33 | impl 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 | ||
54 | impl Error for CargoTomlNoneFoundError {} | 44 | impl Error for CargoTomlNotFoundError {} |
55 | impl Error for CargoTomlMultipleValidFoundError {} | ||
56 | impl Error for CargoTomlSearchFileSystemError {} | ||
57 | 45 | ||
58 | #[derive(Debug, Clone)] | 46 | #[derive(Debug, Clone)] |
59 | pub enum ProjectWorkspace { | 47 | pub enum ProjectWorkspace { |
@@ -452,8 +440,6 @@ fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> { | |||
452 | } | 440 | } |
453 | 441 | ||
454 | fn find_cargo_toml(path: &Path) -> Result<PathBuf> { | 442 | fn 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 | ||
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 37a46cd65..bcfeb6442 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs | |||
@@ -115,12 +115,17 @@ pub fn main_loop( | |||
115 | Ok(workspace) => loaded_workspaces.push(workspace), | 115 | Ok(workspace) => loaded_workspaces.push(workspace), |
116 | Err(e) => { | 116 | Err(e) => { |
117 | log::error!("loading workspace failed: {:?}", e); | 117 | log::error!("loading workspace failed: {:?}", e); |
118 | if let Some(ra_project_model::CargoTomlNoneFoundError(_)) = e.downcast_ref() | 118 | |
119 | if let Some(ra_project_model::CargoTomlNotFoundError { | ||
120 | searched_at: _, | ||
121 | reason: _, | ||
122 | }) = e.downcast_ref() | ||
119 | { | 123 | { |
120 | if !feature_flags.get("notifications.cargo-toml-not-found") { | 124 | if !feature_flags.get("notifications.cargo-toml-not-found") { |
121 | continue; | 125 | continue; |
122 | } | 126 | } |
123 | } | 127 | } |
128 | |||
124 | show_message( | 129 | show_message( |
125 | req::MessageType::Error, | 130 | req::MessageType::Error, |
126 | format!("rust-analyzer failed to load workspace: {:?}", e), | 131 | format!("rust-analyzer failed to load workspace: {:?}", e), |