Skip to content

Commit a132cc7

Browse files
bors[bot]matklad
andauthored
Merge #2354
2354: Cleanup errors r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents c866efd + f4b1fb1 commit a132cc7

File tree

2 files changed

+63
-55
lines changed

2 files changed

+63
-55
lines changed

crates/ra_db/src/input.rs

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
//! actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how
77
//! actual IO is done and lowered to input.
88
9-
use rustc_hash::FxHashMap;
9+
use std::{fmt, str::FromStr};
1010

1111
use ra_cfg::CfgOptions;
1212
use ra_syntax::SmolStr;
13+
use rustc_hash::FxHashMap;
1314
use rustc_hash::FxHashSet;
1415

1516
use crate::{RelativePath, RelativePathBuf};
16-
use std::str::FromStr;
1717

1818
/// `FileId` is an integer which uniquely identifies a file. File paths are
1919
/// messy and system-dependent, so most of the code should work directly with
@@ -80,16 +80,16 @@ pub struct CrateGraph {
8080
arena: FxHashMap<CrateId, CrateData>,
8181
}
8282

83-
#[derive(Debug)]
84-
pub struct CyclicDependencies;
85-
8683
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8784
pub struct CrateId(pub u32);
8885

89-
impl CrateId {
90-
pub fn shift(self, amount: u32) -> CrateId {
91-
CrateId(self.0 + amount)
92-
}
86+
#[derive(Debug, Clone, PartialEq, Eq)]
87+
struct CrateData {
88+
file_id: FileId,
89+
edition: Edition,
90+
cfg_options: CfgOptions,
91+
env: Env,
92+
dependencies: Vec<Dependency>,
9393
}
9494

9595
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -98,58 +98,17 @@ pub enum Edition {
9898
Edition2015,
9999
}
100100

101-
#[derive(Debug)]
102-
pub struct ParseEditionError {
103-
pub msg: String,
104-
}
105-
106-
impl FromStr for Edition {
107-
type Err = ParseEditionError;
108-
fn from_str(s: &str) -> Result<Self, Self::Err> {
109-
match s {
110-
"2015" => Ok(Edition::Edition2015),
111-
"2018" => Ok(Edition::Edition2018),
112-
_ => Err(ParseEditionError { msg: format!("unknown edition: {}", s) }),
113-
}
114-
}
115-
}
116-
117101
#[derive(Default, Debug, Clone, PartialEq, Eq)]
118102
pub struct Env {
119103
entries: FxHashMap<String, String>,
120104
}
121105

122-
#[derive(Debug, Clone, PartialEq, Eq)]
123-
struct CrateData {
124-
file_id: FileId,
125-
edition: Edition,
126-
dependencies: Vec<Dependency>,
127-
cfg_options: CfgOptions,
128-
env: Env,
129-
}
130-
131-
impl CrateData {
132-
fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions, env: Env) -> CrateData {
133-
CrateData { file_id, edition, dependencies: Vec::new(), cfg_options, env }
134-
}
135-
136-
fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
137-
self.dependencies.push(Dependency { name, crate_id })
138-
}
139-
}
140-
141106
#[derive(Debug, Clone, PartialEq, Eq)]
142107
pub struct Dependency {
143108
pub crate_id: CrateId,
144109
pub name: SmolStr,
145110
}
146111

147-
impl Dependency {
148-
pub fn crate_id(&self) -> CrateId {
149-
self.crate_id
150-
}
151-
}
152-
153112
impl CrateGraph {
154113
pub fn add_crate_root(
155114
&mut self,
@@ -174,9 +133,9 @@ impl CrateGraph {
174133
from: CrateId,
175134
name: SmolStr,
176135
to: CrateId,
177-
) -> Result<(), CyclicDependencies> {
136+
) -> Result<(), CyclicDependenciesError> {
178137
if self.dfs_find(from, to, &mut FxHashSet::default()) {
179-
return Err(CyclicDependencies);
138+
return Err(CyclicDependenciesError);
180139
}
181140
self.arena.get_mut(&from).unwrap().add_dep(name, to);
182141
Ok(())
@@ -247,6 +206,57 @@ impl CrateGraph {
247206
}
248207
}
249208

209+
impl CrateId {
210+
pub fn shift(self, amount: u32) -> CrateId {
211+
CrateId(self.0 + amount)
212+
}
213+
}
214+
215+
impl CrateData {
216+
fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions, env: Env) -> CrateData {
217+
CrateData { file_id, edition, dependencies: Vec::new(), cfg_options, env }
218+
}
219+
220+
fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
221+
self.dependencies.push(Dependency { name, crate_id })
222+
}
223+
}
224+
225+
impl FromStr for Edition {
226+
type Err = ParseEditionError;
227+
228+
fn from_str(s: &str) -> Result<Self, Self::Err> {
229+
let res = match s {
230+
"2015" => Edition::Edition2015,
231+
"2018" => Edition::Edition2018,
232+
_ => Err(ParseEditionError { invalid_input: s.to_string() })?,
233+
};
234+
Ok(res)
235+
}
236+
}
237+
238+
impl Dependency {
239+
pub fn crate_id(&self) -> CrateId {
240+
self.crate_id
241+
}
242+
}
243+
244+
#[derive(Debug)]
245+
pub struct ParseEditionError {
246+
invalid_input: String,
247+
}
248+
249+
impl fmt::Display for ParseEditionError {
250+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
251+
write!(f, "invalid edition: {:?}", self.invalid_input)
252+
}
253+
}
254+
255+
impl std::error::Error for ParseEditionError {}
256+
257+
#[derive(Debug)]
258+
pub struct CyclicDependenciesError;
259+
250260
#[cfg(test)]
251261
mod tests {
252262
use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr};

crates/ra_project_model/src/cargo_workspace.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! FIXME: write short doc here
22
33
use std::path::{Path, PathBuf};
4-
use std::str::FromStr;
54

65
use cargo_metadata::{CargoOpt, MetadataCommand};
76
use ra_arena::{impl_arena_id, Arena, RawId};
@@ -143,8 +142,7 @@ impl CargoWorkspace {
143142
for meta_pkg in meta.packages {
144143
let cargo_metadata::Package { id, edition, name, manifest_path, .. } = meta_pkg;
145144
let is_member = ws_members.contains(&id);
146-
let edition = Edition::from_str(&edition)
147-
.map_err(|e| (format!("metadata for package {} failed: {}", &name, e.msg)))?;
145+
let edition = edition.parse::<Edition>()?;
148146
let pkg = packages.alloc(PackageData {
149147
name,
150148
manifest: manifest_path,

0 commit comments

Comments
 (0)