2217: Implement FromStr for enum Edition r=matklad a=clemarescx

Just did this as I came across the comment in the code asking for implementing `std::str::FromStr` for `input::Edition`.
Not sure what was meant by "proper error handling" though, `panic!` with a descriptive message might not be it 😅 

Co-authored-by: Metabaron <metabaron@tuta.io>
This commit is contained in:
bors[bot] 2019-11-12 15:30:36 +00:00 committed by GitHub
commit 2549be750e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 11 deletions

View File

@ -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, '=') {

View File

@ -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,18 @@ pub enum Edition {
Edition2015,
}
impl Edition {
//FIXME: replace with FromStr with proper error handling
pub fn from_string(s: &str) -> Edition {
#[derive(Debug)]
pub struct ParseEditionError {
pub msg: String,
}
impl FromStr for Edition {
type Err = ParseEditionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"2015" => Edition::Edition2015,
"2018" | _ => Edition::Edition2018,
"2015" => Ok(Edition::Edition2015),
"2018" => Ok(Edition::Edition2018),
_ => Err(ParseEditionError { msg: format!("unknown edition: {}", s) }),
}
}
}

View File

@ -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};
@ -140,18 +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 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: meta_pkg.name,
manifest: meta_pkg.manifest_path.clone(),
name,
manifest: manifest_path,
targets: Vec::new(),
is_member,
edition: Edition::from_string(&meta_pkg.edition),
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,