mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 22:54:58 +00:00
keep one CargoTomlNotFoundError
This commit is contained in:
parent
12b595e817
commit
e15424c1b7
2 changed files with 38 additions and 30 deletions
|
@ -25,35 +25,23 @@ pub use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
||||||
pub struct CargoTomlNoneFoundError(pub PathBuf);
|
pub struct CargoTomlNotFoundError {
|
||||||
|
pub searched_at: PathBuf,
|
||||||
|
pub reason: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
impl std::fmt::Display for CargoTomlNotFoundError {
|
||||||
pub struct CargoTomlMultipleValidFoundError(pub Vec<PathBuf>);
|
|
||||||
|
|
||||||
#[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 {
|
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(fmt, "can't find Cargo.toml at {}", self.0.display())
|
write!(
|
||||||
|
fmt,
|
||||||
|
"can't find Cargo.toml at {}, due to {}",
|
||||||
|
self.searched_at.display(),
|
||||||
|
self.reason
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for CargoTomlMultipleValidFoundError {
|
impl Error for CargoTomlNotFoundError {}
|
||||||
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(fmt, "found multiple valid Cargo.toml files {:?}", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for CargoTomlSearchFileSystemError {
|
|
||||||
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(fmt, "a filesystem error occurred while searching for Cargo.toml: {}", self.1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Error for CargoTomlNoneFoundError {}
|
|
||||||
impl Error for CargoTomlMultipleValidFoundError {}
|
|
||||||
impl Error for CargoTomlSearchFileSystemError {}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum ProjectWorkspace {
|
pub enum ProjectWorkspace {
|
||||||
|
@ -452,8 +440,6 @@ fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<PathBuf> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
|
fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
|
||||||
let path_as_buf = path.to_path_buf();
|
|
||||||
|
|
||||||
if path.ends_with("Cargo.toml") {
|
if path.ends_with("Cargo.toml") {
|
||||||
return Ok(path.to_path_buf());
|
return Ok(path.to_path_buf());
|
||||||
}
|
}
|
||||||
|
@ -464,14 +450,31 @@ fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
|
||||||
|
|
||||||
let entities = match read_dir(path) {
|
let entities = match read_dir(path) {
|
||||||
Ok(entities) => entities,
|
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);
|
let mut valid_canditates = find_cargo_toml_in_child_dir(entities);
|
||||||
match valid_canditates.len() {
|
match valid_canditates.len() {
|
||||||
1 => Ok(valid_canditates.remove(0)),
|
1 => Ok(valid_canditates.remove(0)),
|
||||||
0 => Err(CargoTomlNoneFoundError(path_as_buf).into()),
|
0 => Err(CargoTomlNotFoundError {
|
||||||
_ => Err(CargoTomlMultipleValidFoundError(valid_canditates).into()),
|
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()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -115,12 +115,17 @@ pub fn main_loop(
|
||||||
Ok(workspace) => loaded_workspaces.push(workspace),
|
Ok(workspace) => loaded_workspaces.push(workspace),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("loading workspace failed: {:?}", e);
|
log::error!("loading workspace failed: {:?}", e);
|
||||||
if let Some(ra_project_model::CargoTomlNoneFoundError(_)) = e.downcast_ref()
|
|
||||||
|
if let Some(ra_project_model::CargoTomlNotFoundError {
|
||||||
|
searched_at: _,
|
||||||
|
reason: _,
|
||||||
|
}) = e.downcast_ref()
|
||||||
{
|
{
|
||||||
if !feature_flags.get("notifications.cargo-toml-not-found") {
|
if !feature_flags.get("notifications.cargo-toml-not-found") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
show_message(
|
show_message(
|
||||||
req::MessageType::Error,
|
req::MessageType::Error,
|
||||||
format!("rust-analyzer failed to load workspace: {:?}", e),
|
format!("rust-analyzer failed to load workspace: {:?}", e),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue