Skip to content

Commit e15424c

Browse files
committed
keep one CargoTomlNotFoundError
1 parent 12b595e commit e15424c

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

crates/ra_project_model/src/lib.rs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,23 @@ pub use crate::{
2525
};
2626

2727
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
28-
pub struct CargoTomlNoneFoundError(pub PathBuf);
29-
30-
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
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-
}
28+
pub struct CargoTomlNotFoundError {
29+
pub searched_at: PathBuf,
30+
pub reason: String,
4631
}
4732

48-
impl std::fmt::Display for CargoTomlSearchFileSystemError {
33+
impl std::fmt::Display for CargoTomlNotFoundError {
4934
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+
)
5141
}
5242
}
5343

54-
impl Error for CargoTomlNoneFoundError {}
55-
impl Error for CargoTomlMultipleValidFoundError {}
56-
impl Error for CargoTomlSearchFileSystemError {}
44+
impl Error for CargoTomlNotFoundError {}
5745

5846
#[derive(Debug, Clone)]
5947
pub enum ProjectWorkspace {
@@ -452,8 +440,6 @@ fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> {
452440
}
453441

454442
fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
455-
let path_as_buf = path.to_path_buf();
456-
457443
if path.ends_with("Cargo.toml") {
458444
return Ok(path.to_path_buf());
459445
}
@@ -464,14 +450,31 @@ fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
464450

465451
let entities = match read_dir(path) {
466452
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+
}
468460
};
469461

470462
let mut valid_canditates = find_cargo_toml_in_child_dir(entities);
471463
match valid_canditates.len() {
472464
1 => Ok(valid_canditates.remove(0)),
473-
0 => Err(CargoTomlNoneFoundError(path_as_buf).into()),
474-
_ => Err(CargoTomlMultipleValidFoundError(valid_canditates).into()),
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()),
475478
}
476479
}
477480

crates/rust-analyzer/src/main_loop.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,17 @@ pub fn main_loop(
115115
Ok(workspace) => loaded_workspaces.push(workspace),
116116
Err(e) => {
117117
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()
119123
{
120124
if !feature_flags.get("notifications.cargo-toml-not-found") {
121125
continue;
122126
}
123127
}
128+
124129
show_message(
125130
req::MessageType::Error,
126131
format!("rust-analyzer failed to load workspace: {:?}", e),

0 commit comments

Comments
 (0)