From b69738590ca1c4823a030d317e7fa6e918618a4b Mon Sep 17 00:00:00 2001 From: Metabaron Date: Mon, 11 Nov 2019 23:16:59 +0100 Subject: [PATCH 1/3] Implement FromStr for enum Edition --- crates/ra_db/src/fixture.rs | 3 ++- crates/ra_db/src/input.rs | 12 +++++++----- crates/ra_project_model/src/cargo_workspace.rs | 7 +++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index f5dd59f8401..ee883b61545 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -1,5 +1,6 @@ //! FIXME: write short doc here +use std::str::FromStr; use std::sync::Arc; use ra_cfg::CfgOptions; @@ -164,7 +165,7 @@ fn parse_meta(meta: &str) -> ParsedMeta { match key { "crate" => krate = Some(value.to_string()), "deps" => deps = value.split(',').map(|it| it.to_string()).collect(), - "edition" => edition = Edition::from_string(&value), + "edition" => edition = Edition::from_str(&value).unwrap(), "cfg" => { for key in value.split(',') { match split1(key, '=') { diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 60f7dc88156..fb9a3297a47 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -13,6 +13,7 @@ use ra_syntax::SmolStr; 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 @@ -97,12 +98,13 @@ pub enum Edition { Edition2015, } -impl Edition { - //FIXME: replace with FromStr with proper error handling - pub fn from_string(s: &str) -> Edition { +impl FromStr for Edition { + type Err = String; + fn from_str(s: &str) -> Result { match s { - "2015" => Edition::Edition2015, - "2018" | _ => Edition::Edition2018, + "2015" => Ok(Edition::Edition2015), + "2018" => Ok(Edition::Edition2018), + _ => Err(format! {"unknown edition: {}" , s}), } } } diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 28dadea9d7e..ff96bf904d2 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -1,6 +1,7 @@ //! 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}; @@ -141,12 +142,14 @@ impl CargoWorkspace { for meta_pkg in meta.packages { let is_member = ws_members.contains(&meta_pkg.id); + let name = meta_pkg.name; let pkg = packages.alloc(PackageData { - name: meta_pkg.name, + name: name.clone(), manifest: meta_pkg.manifest_path.clone(), targets: Vec::new(), is_member, - edition: Edition::from_string(&meta_pkg.edition), + edition: Edition::from_str(&meta_pkg.edition) + .unwrap_or_else(|e| panic!("unknown edition {} for package {:?}", e, &name)), dependencies: Vec::new(), features: Vec::new(), }); From 53b9c1c8d898a84a10b86f2fc31a7f6c2dfc46d0 Mon Sep 17 00:00:00 2001 From: Metabaron Date: Tue, 12 Nov 2019 11:53:31 +0100 Subject: [PATCH 2/3] return Error instead of panicking in from_cargo_metadata --- crates/ra_db/src/input.rs | 11 ++++++++--- crates/ra_project_model/src/cargo_workspace.rs | 15 ++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index fb9a3297a47..472a15f2b9a 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -13,7 +13,7 @@ use ra_syntax::SmolStr; use rustc_hash::FxHashSet; use crate::{RelativePath, RelativePathBuf}; -use std::str::FromStr; +use std::{error::Error, 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,13 +98,18 @@ pub enum Edition { Edition2015, } +#[derive(Debug)] +pub struct ParseEditionError { + pub msg: String, +} + impl FromStr for Edition { - type Err = String; + type Err = ParseEditionError; fn from_str(s: &str) -> Result { match s { "2015" => Ok(Edition::Edition2015), "2018" => Ok(Edition::Edition2018), - _ => Err(format! {"unknown edition: {}" , s}), + _ => Err(ParseEditionError { msg: format!("unknown edition: {}", s) }), } } } diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index ff96bf904d2..cf88911b751 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -141,20 +141,21 @@ impl CargoWorkspace { let ws_members = &meta.workspace_members; for meta_pkg in meta.packages { - let is_member = ws_members.contains(&meta_pkg.id); - let name = meta_pkg.name; + 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 pkg = packages.alloc(PackageData { - name: name.clone(), - manifest: meta_pkg.manifest_path.clone(), + name, + manifest: manifest_path, targets: Vec::new(), is_member, - edition: Edition::from_str(&meta_pkg.edition) - .unwrap_or_else(|e| panic!("unknown edition {} for package {:?}", e, &name)), + edition, dependencies: Vec::new(), features: Vec::new(), }); let pkg_data = &mut packages[pkg]; - pkg_by_id.insert(meta_pkg.id.clone(), pkg); + pkg_by_id.insert(id, pkg); for meta_tgt in meta_pkg.targets { let tgt = targets.alloc(TargetData { pkg, From dae087656abf5d120cd9c051bf4fc446fca101e1 Mon Sep 17 00:00:00 2001 From: Metabaron Date: Tue, 12 Nov 2019 11:59:25 +0100 Subject: [PATCH 3/3] Fix unused import --- crates/ra_db/src/input.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 472a15f2b9a..c0d95a13fa1 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -13,7 +13,7 @@ use ra_syntax::SmolStr; use rustc_hash::FxHashSet; use crate::{RelativePath, RelativePathBuf}; -use std::{error::Error, str::FromStr}; +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