aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db/src/input.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_db/src/input.rs')
-rw-r--r--crates/ra_db/src/input.rs114
1 files changed, 62 insertions, 52 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
9use rustc_hash::FxHashMap; 9use std::{fmt, str::FromStr};
10 10
11use ra_cfg::CfgOptions; 11use ra_cfg::CfgOptions;
12use ra_syntax::SmolStr; 12use ra_syntax::SmolStr;
13use rustc_hash::FxHashMap;
13use rustc_hash::FxHashSet; 14use rustc_hash::FxHashSet;
14 15
15use crate::{RelativePath, RelativePathBuf}; 16use crate::{RelativePath, RelativePathBuf};
16use 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)]
84pub struct CyclicDependencies;
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 83#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
87pub struct CrateId(pub u32); 84pub struct CrateId(pub u32);
88 85
89impl CrateId { 86#[derive(Debug, Clone, PartialEq, Eq)]
90 pub fn shift(self, amount: u32) -> CrateId { 87struct 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)]
102pub struct ParseEditionError {
103 pub msg: String,
104}
105
106impl 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)]
118pub struct Env { 102pub 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)]
123struct CrateData {
124 file_id: FileId,
125 edition: Edition,
126 dependencies: Vec<Dependency>,
127 cfg_options: CfgOptions,
128 env: Env,
129}
130
131impl 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)]
142pub struct Dependency { 107pub struct Dependency {
143 pub crate_id: CrateId, 108 pub crate_id: CrateId,
144 pub name: SmolStr, 109 pub name: SmolStr,
145} 110}
146 111
147impl Dependency {
148 pub fn crate_id(&self) -> CrateId {
149 self.crate_id
150 }
151}
152
153impl CrateGraph { 112impl 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
209impl CrateId {
210 pub fn shift(self, amount: u32) -> CrateId {
211 CrateId(self.0 + amount)
212 }
213}
214
215impl 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
225impl 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
238impl Dependency {
239 pub fn crate_id(&self) -> CrateId {
240 self.crate_id
241 }
242}
243
244#[derive(Debug)]
245pub struct ParseEditionError {
246 invalid_input: String,
247}
248
249impl 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
255impl std::error::Error for ParseEditionError {}
256
257#[derive(Debug)]
258pub struct CyclicDependenciesError;
259
250#[cfg(test)] 260#[cfg(test)]
251mod tests { 261mod tests {
252 use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr}; 262 use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr};