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.rs44
1 files changed, 32 insertions, 12 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index 8decc65c5..e45a510b3 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -56,15 +56,31 @@ pub struct CyclicDependencies;
56#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 56#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
57pub struct CrateId(pub u32); 57pub struct CrateId(pub u32);
58 58
59#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
60pub enum Edition {
61 Edition2018,
62 Edition2015,
63}
64
65impl Edition {
66 pub fn from_string(s: &str) -> Edition {
67 match s {
68 "2015" => Edition::Edition2015,
69 "2018" | _ => Edition::Edition2018,
70 }
71 }
72}
73
59#[derive(Debug, Clone, PartialEq, Eq)] 74#[derive(Debug, Clone, PartialEq, Eq)]
60struct CrateData { 75struct CrateData {
61 file_id: FileId, 76 file_id: FileId,
77 edition: Edition,
62 dependencies: Vec<Dependency>, 78 dependencies: Vec<Dependency>,
63} 79}
64 80
65impl CrateData { 81impl CrateData {
66 fn new(file_id: FileId) -> CrateData { 82 fn new(file_id: FileId, edition: Edition) -> CrateData {
67 CrateData { file_id, dependencies: Vec::new() } 83 CrateData { file_id, edition, dependencies: Vec::new() }
68 } 84 }
69 85
70 fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { 86 fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
@@ -85,9 +101,9 @@ impl Dependency {
85} 101}
86 102
87impl CrateGraph { 103impl CrateGraph {
88 pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId { 104 pub fn add_crate_root(&mut self, file_id: FileId, edition: Edition) -> CrateId {
89 let crate_id = CrateId(self.arena.len() as u32); 105 let crate_id = CrateId(self.arena.len() as u32);
90 let prev = self.arena.insert(crate_id, CrateData::new(file_id)); 106 let prev = self.arena.insert(crate_id, CrateData::new(file_id, edition));
91 assert!(prev.is_none()); 107 assert!(prev.is_none());
92 crate_id 108 crate_id
93 } 109 }
@@ -112,6 +128,10 @@ impl CrateGraph {
112 self.arena[&crate_id].file_id 128 self.arena[&crate_id].file_id
113 } 129 }
114 130
131 pub fn edition(&self, crate_id: CrateId) -> Edition {
132 self.arena[&crate_id].edition
133 }
134
115 // TODO: this only finds one crate with the given root; we could have multiple 135 // TODO: this only finds one crate with the given root; we could have multiple
116 pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> { 136 pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
117 let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?; 137 let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?;
@@ -159,14 +179,14 @@ impl CrateGraph {
159 179
160#[cfg(test)] 180#[cfg(test)]
161mod tests { 181mod tests {
162 use super::{CrateGraph, FileId, SmolStr}; 182 use super::{CrateGraph, FileId, SmolStr, Edition::Edition2018};
163 183
164 #[test] 184 #[test]
165 fn it_should_painc_because_of_cycle_dependencies() { 185 fn it_should_panic_because_of_cycle_dependencies() {
166 let mut graph = CrateGraph::default(); 186 let mut graph = CrateGraph::default();
167 let crate1 = graph.add_crate_root(FileId(1u32)); 187 let crate1 = graph.add_crate_root(FileId(1u32), Edition2018);
168 let crate2 = graph.add_crate_root(FileId(2u32)); 188 let crate2 = graph.add_crate_root(FileId(2u32), Edition2018);
169 let crate3 = graph.add_crate_root(FileId(3u32)); 189 let crate3 = graph.add_crate_root(FileId(3u32), Edition2018);
170 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); 190 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
171 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); 191 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
172 assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err()); 192 assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err());
@@ -175,9 +195,9 @@ mod tests {
175 #[test] 195 #[test]
176 fn it_works() { 196 fn it_works() {
177 let mut graph = CrateGraph::default(); 197 let mut graph = CrateGraph::default();
178 let crate1 = graph.add_crate_root(FileId(1u32)); 198 let crate1 = graph.add_crate_root(FileId(1u32), Edition2018);
179 let crate2 = graph.add_crate_root(FileId(2u32)); 199 let crate2 = graph.add_crate_root(FileId(2u32), Edition2018);
180 let crate3 = graph.add_crate_root(FileId(3u32)); 200 let crate3 = graph.add_crate_root(FileId(3u32), Edition2018);
181 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); 201 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
182 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); 202 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
183 } 203 }