From 0861ac061ef5c8b5d7ed2a95ef30f4a17710229d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 14:08:18 +0300 Subject: Cleanup errors --- crates/ra_db/src/input.rs | 46 ++++++++++++++++---------- crates/ra_project_model/src/cargo_workspace.rs | 4 +-- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 0015d6b5e..777e63442 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -6,14 +6,14 @@ //! actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how //! actual IO is done and lowered to input. -use rustc_hash::FxHashMap; +use std::{fmt, str::FromStr}; use ra_cfg::CfgOptions; use ra_syntax::SmolStr; +use rustc_hash::FxHashMap; use rustc_hash::FxHashSet; use crate::{RelativePath, RelativePathBuf}; -use std::str::FromStr; /// `FileId` is an integer which uniquely identifies a file. File paths are /// messy and system-dependent, so most of the code should work directly with @@ -98,22 +98,6 @@ pub enum Edition { Edition2015, } -#[derive(Debug)] -pub struct ParseEditionError { - pub msg: String, -} - -impl FromStr for Edition { - type Err = ParseEditionError; - fn from_str(s: &str) -> Result { - match s { - "2015" => Ok(Edition::Edition2015), - "2018" => Ok(Edition::Edition2018), - _ => Err(ParseEditionError { msg: format!("unknown edition: {}", s) }), - } - } -} - #[derive(Default, Debug, Clone, PartialEq, Eq)] pub struct Env { entries: FxHashMap, @@ -247,6 +231,32 @@ impl CrateGraph { } } +#[derive(Debug)] +pub struct ParseEditionError { + invalid_input: String, +} + +impl FromStr for Edition { + type Err = ParseEditionError; + + fn from_str(s: &str) -> Result { + let res = match s { + "2015" => Edition::Edition2015, + "2018" => Edition::Edition2018, + _ => Err(ParseEditionError { invalid_input: s.to_string() })?, + }; + Ok(res) + } +} + +impl fmt::Display for ParseEditionError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "invalid edition: {:?}", self.invalid_input) + } +} + +impl std::error::Error for ParseEditionError {} + #[cfg(test)] mod tests { use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr}; diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index cf88911b7..c128e608d 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -1,7 +1,6 @@ //! FIXME: write short doc here use std::path::{Path, PathBuf}; -use std::str::FromStr; use cargo_metadata::{CargoOpt, MetadataCommand}; use ra_arena::{impl_arena_id, Arena, RawId}; @@ -143,8 +142,7 @@ impl CargoWorkspace { for meta_pkg in meta.packages { let cargo_metadata::Package { id, edition, name, manifest_path, .. } = meta_pkg; let is_member = ws_members.contains(&id); - let edition = Edition::from_str(&edition) - .map_err(|e| (format!("metadata for package {} failed: {}", &name, e.msg)))?; + let edition = edition.parse::()?; let pkg = packages.alloc(PackageData { name, manifest: manifest_path, -- cgit v1.2.3