From 6c321d7318b94ee93dc60dc88d1e68afa94e8c4f Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Fri, 3 Jan 2020 14:10:50 +0100 Subject: Move error to new file --- crates/ra_project_model/src/lib.rs | 58 ++------------------------ crates/ra_project_model/src/workspace_error.rs | 57 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 54 deletions(-) create mode 100644 crates/ra_project_model/src/workspace_error.rs (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 49710b358..c07c54936 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -3,9 +3,9 @@ mod cargo_workspace; mod json_project; mod sysroot; +mod workspace_error; use std::{ - fmt, fs::File, io::BufReader, path::{Path, PathBuf}, @@ -13,68 +13,18 @@ use std::{ }; use ra_cfg::CfgOptions; -use ra_db::{CrateGraph, CrateId, Edition, Env, FileId, ParseEditionError}; +use ra_db::{CrateGraph, CrateId, Edition, Env, FileId}; use rustc_hash::FxHashMap; use serde_json::from_reader; +use crate::workspace_error::WorkspaceError; + pub use crate::{ cargo_workspace::{CargoFeatures, CargoWorkspace, Package, Target, TargetKind}, json_project::JsonProject, sysroot::Sysroot, }; -#[derive(Debug)] -pub enum WorkspaceError { - CargoMetadataFailed(cargo_metadata::Error), - CargoTomlNotFound(PathBuf), - NoStdLib(PathBuf), - OpenWorkspaceError(std::io::Error), - ParseEditionError(ParseEditionError), - ReadWorkspaceError(serde_json::Error), - RustcCfgError, - RustcError(std::io::Error), - RustcOutputError(std::string::FromUtf8Error), - SysrootNotFound, -} - -impl fmt::Display for WorkspaceError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::OpenWorkspaceError(err) | Self::RustcError(err) => write!(f, "{}", err), - Self::ParseEditionError(err) => write!(f, "{}", err), - Self::ReadWorkspaceError(err) => write!(f, "{}", err), - Self::RustcOutputError(err) => write!(f, "{}", err), - Self::CargoMetadataFailed(err) => write!(f, "cargo metadata failed: {}", err), - Self::RustcCfgError => write!(f, "failed to get rustc cfgs"), - Self::SysrootNotFound => write!(f, "failed to locate sysroot"), - Self::CargoTomlNotFound(path) => { - write!(f, "can't find Cargo.toml at {}", path.display()) - } - Self::NoStdLib(sysroot) => write!( - f, - "can't load standard library from sysroot\n\ - {:?}\n\ - try running `rustup component add rust-src` or set `RUST_SRC_PATH`", - sysroot, - ), - } - } -} - -impl std::error::Error for WorkspaceError {} - -impl From for WorkspaceError { - fn from(err: ParseEditionError) -> Self { - Self::ParseEditionError(err.into()) - } -} - -impl From for WorkspaceError { - fn from(err: cargo_metadata::Error) -> Self { - Self::CargoMetadataFailed(err.into()) - } -} - #[derive(Debug, Clone)] pub enum ProjectWorkspace { /// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`. diff --git a/crates/ra_project_model/src/workspace_error.rs b/crates/ra_project_model/src/workspace_error.rs new file mode 100644 index 000000000..5f7384968 --- /dev/null +++ b/crates/ra_project_model/src/workspace_error.rs @@ -0,0 +1,57 @@ +//! Workspace-related errors + +use std::{error::Error, fmt, io, path::PathBuf, string::FromUtf8Error}; + +use ra_db::ParseEditionError; + +#[derive(Debug)] +pub enum WorkspaceError { + CargoMetadataFailed(cargo_metadata::Error), + CargoTomlNotFound(PathBuf), + NoStdLib(PathBuf), + OpenWorkspaceError(io::Error), + ParseEditionError(ParseEditionError), + ReadWorkspaceError(serde_json::Error), + RustcCfgError, + RustcError(io::Error), + RustcOutputError(FromUtf8Error), + SysrootNotFound, +} + +impl fmt::Display for WorkspaceError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::OpenWorkspaceError(err) | Self::RustcError(err) => write!(f, "{}", err), + Self::ParseEditionError(err) => write!(f, "{}", err), + Self::ReadWorkspaceError(err) => write!(f, "{}", err), + Self::RustcOutputError(err) => write!(f, "{}", err), + Self::CargoMetadataFailed(err) => write!(f, "cargo metadata failed: {}", err), + Self::RustcCfgError => write!(f, "failed to get rustc cfgs"), + Self::SysrootNotFound => write!(f, "failed to locate sysroot"), + Self::CargoTomlNotFound(path) => { + write!(f, "can't find Cargo.toml at {}", path.display()) + } + Self::NoStdLib(sysroot) => write!( + f, + "can't load standard library from sysroot\n\ + {:?}\n\ + try running `rustup component add rust-src` or set `RUST_SRC_PATH`", + sysroot, + ), + } + } +} + +impl From for WorkspaceError { + fn from(err: ParseEditionError) -> Self { + Self::ParseEditionError(err.into()) + } +} + +impl From for WorkspaceError { + fn from(err: cargo_metadata::Error) -> Self { + Self::CargoMetadataFailed(err.into()) + } +} + +impl Error for WorkspaceError {} -- cgit v1.2.3