From e15424c1b7d5a82545fbb3744af3123a124053d5 Mon Sep 17 00:00:00 2001 From: nmio Date: Sat, 29 Feb 2020 13:05:10 +0000 Subject: keep one CargoTomlNotFoundError --- crates/ra_project_model/src/lib.rs | 59 ++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 28 deletions(-) (limited to 'crates/ra_project_model/src') 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::{ }; #[derive(Clone, PartialEq, Eq, Hash, Debug)] -pub struct CargoTomlNoneFoundError(pub PathBuf); - -#[derive(Clone, PartialEq, Eq, Hash, Debug)] -pub struct CargoTomlMultipleValidFoundError(pub Vec); - -#[derive(Clone, PartialEq, Eq, Hash, Debug)] -pub struct CargoTomlSearchFileSystemError(pub PathBuf, pub String); - -impl std::fmt::Display for CargoTomlNoneFoundError { - fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(fmt, "can't find Cargo.toml at {}", self.0.display()) - } -} - -impl std::fmt::Display for CargoTomlMultipleValidFoundError { - fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(fmt, "found multiple valid Cargo.toml files {:?}", self.0) - } +pub struct CargoTomlNotFoundError { + pub searched_at: PathBuf, + pub reason: String, } -impl std::fmt::Display for CargoTomlSearchFileSystemError { +impl std::fmt::Display for CargoTomlNotFoundError { fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(fmt, "a filesystem error occurred while searching for Cargo.toml: {}", self.1) + write!( + fmt, + "can't find Cargo.toml at {}, due to {}", + self.searched_at.display(), + self.reason + ) } } -impl Error for CargoTomlNoneFoundError {} -impl Error for CargoTomlMultipleValidFoundError {} -impl Error for CargoTomlSearchFileSystemError {} +impl Error for CargoTomlNotFoundError {} #[derive(Debug, Clone)] pub enum ProjectWorkspace { @@ -452,8 +440,6 @@ fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec { } fn find_cargo_toml(path: &Path) -> Result { - let path_as_buf = path.to_path_buf(); - if path.ends_with("Cargo.toml") { return Ok(path.to_path_buf()); } @@ -464,14 +450,31 @@ fn find_cargo_toml(path: &Path) -> Result { let entities = match read_dir(path) { Ok(entities) => entities, - Err(e) => return Err(CargoTomlSearchFileSystemError(path_as_buf, e.to_string()).into()), + Err(e) => { + return Err(CargoTomlNotFoundError { + searched_at: path.to_path_buf(), + reason: format!("file system error: {}", e), + } + .into()); + } }; let mut valid_canditates = find_cargo_toml_in_child_dir(entities); match valid_canditates.len() { 1 => Ok(valid_canditates.remove(0)), - 0 => Err(CargoTomlNoneFoundError(path_as_buf).into()), - _ => Err(CargoTomlMultipleValidFoundError(valid_canditates).into()), + 0 => Err(CargoTomlNotFoundError { + searched_at: path.to_path_buf(), + reason: "no Cargo.toml file found".to_string(), + } + .into()), + _ => Err(CargoTomlNotFoundError { + searched_at: path.to_path_buf(), + reason: format!( + "multiple equally valid Cargo.toml files found: {:?}", + valid_canditates + ), + } + .into()), } } -- cgit v1.2.3