From b69738590ca1c4823a030d317e7fa6e918618a4b Mon Sep 17 00:00:00 2001 From: Metabaron Date: Mon, 11 Nov 2019 23:16:59 +0100 Subject: Implement FromStr for enum Edition --- crates/ra_db/src/fixture.rs | 3 ++- crates/ra_db/src/input.rs | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) (limited to 'crates/ra_db/src') diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index f5dd59f84..ee883b615 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 60f7dc881..fb9a3297a 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}), } } } -- cgit v1.2.3 From 53b9c1c8d898a84a10b86f2fc31a7f6c2dfc46d0 Mon Sep 17 00:00:00 2001 From: Metabaron Date: Tue, 12 Nov 2019 11:53:31 +0100 Subject: return Error instead of panicking in from_cargo_metadata --- crates/ra_db/src/input.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'crates/ra_db/src') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index fb9a3297a..472a15f2b 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) }), } } } -- cgit v1.2.3 From dae087656abf5d120cd9c051bf4fc446fca101e1 Mon Sep 17 00:00:00 2001 From: Metabaron Date: Tue, 12 Nov 2019 11:59:25 +0100 Subject: Fix unused import --- crates/ra_db/src/input.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_db/src') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 472a15f2b..c0d95a13f 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 -- cgit v1.2.3