diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-22 11:13:24 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-22 11:13:24 +0000 |
commit | a132cc715ed05cab83fd058156eb152f0be96982 (patch) | |
tree | ce2e04a611618bdd47bd81b6f15fcdf324308913 /crates | |
parent | c866efdf36139502ec824bc38892fb2bda14484f (diff) | |
parent | f4b1fb1554b639374adeffa50d4719f834a0d475 (diff) |
Merge #2354
2354: Cleanup errors r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_db/src/input.rs | 114 | ||||
-rw-r--r-- | crates/ra_project_model/src/cargo_workspace.rs | 4 |
2 files changed, 63 insertions, 55 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 0015d6b5e..b6d851776 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs | |||
@@ -6,14 +6,14 @@ | |||
6 | //! actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how | 6 | //! actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how |
7 | //! actual IO is done and lowered to input. | 7 | //! actual IO is done and lowered to input. |
8 | 8 | ||
9 | use rustc_hash::FxHashMap; | 9 | use std::{fmt, str::FromStr}; |
10 | 10 | ||
11 | use ra_cfg::CfgOptions; | 11 | use ra_cfg::CfgOptions; |
12 | use ra_syntax::SmolStr; | 12 | use ra_syntax::SmolStr; |
13 | use rustc_hash::FxHashMap; | ||
13 | use rustc_hash::FxHashSet; | 14 | use rustc_hash::FxHashSet; |
14 | 15 | ||
15 | use crate::{RelativePath, RelativePathBuf}; | 16 | use crate::{RelativePath, RelativePathBuf}; |
16 | use std::str::FromStr; | ||
17 | 17 | ||
18 | /// `FileId` is an integer which uniquely identifies a file. File paths are | 18 | /// `FileId` is an integer which uniquely identifies a file. File paths are |
19 | /// messy and system-dependent, so most of the code should work directly with | 19 | /// messy and system-dependent, so most of the code should work directly with |
@@ -80,16 +80,16 @@ pub struct CrateGraph { | |||
80 | arena: FxHashMap<CrateId, CrateData>, | 80 | arena: FxHashMap<CrateId, CrateData>, |
81 | } | 81 | } |
82 | 82 | ||
83 | #[derive(Debug)] | ||
84 | pub struct CyclicDependencies; | ||
85 | |||
86 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | 83 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
87 | pub struct CrateId(pub u32); | 84 | pub struct CrateId(pub u32); |
88 | 85 | ||
89 | impl CrateId { | 86 | #[derive(Debug, Clone, PartialEq, Eq)] |
90 | pub fn shift(self, amount: u32) -> CrateId { | 87 | struct CrateData { |
91 | CrateId(self.0 + amount) | 88 | file_id: FileId, |
92 | } | 89 | edition: Edition, |
90 | cfg_options: CfgOptions, | ||
91 | env: Env, | ||
92 | dependencies: Vec<Dependency>, | ||
93 | } | 93 | } |
94 | 94 | ||
95 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 95 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -98,58 +98,17 @@ pub enum Edition { | |||
98 | Edition2015, | 98 | Edition2015, |
99 | } | 99 | } |
100 | 100 | ||
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 | |||
117 | #[derive(Default, Debug, Clone, PartialEq, Eq)] | 101 | #[derive(Default, Debug, Clone, PartialEq, Eq)] |
118 | pub struct Env { | 102 | pub struct Env { |
119 | entries: FxHashMap<String, String>, | 103 | entries: FxHashMap<String, String>, |
120 | } | 104 | } |
121 | 105 | ||
122 | #[derive(Debug, Clone, PartialEq, Eq)] | 106 | #[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 | |||
141 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
142 | pub struct Dependency { | 107 | pub struct Dependency { |
143 | pub crate_id: CrateId, | 108 | pub crate_id: CrateId, |
144 | pub name: SmolStr, | 109 | pub name: SmolStr, |
145 | } | 110 | } |
146 | 111 | ||
147 | impl Dependency { | ||
148 | pub fn crate_id(&self) -> CrateId { | ||
149 | self.crate_id | ||
150 | } | ||
151 | } | ||
152 | |||
153 | impl CrateGraph { | 112 | impl CrateGraph { |
154 | pub fn add_crate_root( | 113 | pub fn add_crate_root( |
155 | &mut self, | 114 | &mut self, |
@@ -174,9 +133,9 @@ impl CrateGraph { | |||
174 | from: CrateId, | 133 | from: CrateId, |
175 | name: SmolStr, | 134 | name: SmolStr, |
176 | to: CrateId, | 135 | to: CrateId, |
177 | ) -> Result<(), CyclicDependencies> { | 136 | ) -> Result<(), CyclicDependenciesError> { |
178 | if self.dfs_find(from, to, &mut FxHashSet::default()) { | 137 | if self.dfs_find(from, to, &mut FxHashSet::default()) { |
179 | return Err(CyclicDependencies); | 138 | return Err(CyclicDependenciesError); |
180 | } | 139 | } |
181 | self.arena.get_mut(&from).unwrap().add_dep(name, to); | 140 | self.arena.get_mut(&from).unwrap().add_dep(name, to); |
182 | Ok(()) | 141 | Ok(()) |
@@ -247,6 +206,57 @@ impl CrateGraph { | |||
247 | } | 206 | } |
248 | } | 207 | } |
249 | 208 | ||
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 | |||
250 | #[cfg(test)] | 260 | #[cfg(test)] |
251 | mod tests { | 261 | mod tests { |
252 | use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr}; | 262 | use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr}; |
diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index cf88911b7..c128e608d 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs | |||
@@ -1,7 +1,6 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use std::path::{Path, PathBuf}; | 3 | use std::path::{Path, PathBuf}; |
4 | use std::str::FromStr; | ||
5 | 4 | ||
6 | use cargo_metadata::{CargoOpt, MetadataCommand}; | 5 | use cargo_metadata::{CargoOpt, MetadataCommand}; |
7 | use ra_arena::{impl_arena_id, Arena, RawId}; | 6 | use ra_arena::{impl_arena_id, Arena, RawId}; |
@@ -143,8 +142,7 @@ impl CargoWorkspace { | |||
143 | for meta_pkg in meta.packages { | 142 | for meta_pkg in meta.packages { |
144 | let cargo_metadata::Package { id, edition, name, manifest_path, .. } = meta_pkg; | 143 | let cargo_metadata::Package { id, edition, name, manifest_path, .. } = meta_pkg; |
145 | let is_member = ws_members.contains(&id); | 144 | let is_member = ws_members.contains(&id); |
146 | let edition = Edition::from_str(&edition) | 145 | let edition = edition.parse::<Edition>()?; |
147 | .map_err(|e| (format!("metadata for package {} failed: {}", &name, e.msg)))?; | ||
148 | let pkg = packages.alloc(PackageData { | 146 | let pkg = packages.alloc(PackageData { |
149 | name, | 147 | name, |
150 | manifest: manifest_path, | 148 | manifest: manifest_path, |