return Error instead of panicking in from_cargo_metadata
This commit is contained in:
parent
b69738590c
commit
53b9c1c8d8
2 changed files with 16 additions and 10 deletions
|
@ -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<Self, Self::Err> {
|
||||
match s {
|
||||
"2015" => Ok(Edition::Edition2015),
|
||||
"2018" => Ok(Edition::Edition2018),
|
||||
_ => Err(format! {"unknown edition: {}" , s}),
|
||||
_ => Err(ParseEditionError { msg: format!("unknown edition: {}", s) }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Reference in a new issue