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.rs52
1 files changed, 44 insertions, 8 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index 07269237a..1f1dcea42 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -83,6 +83,26 @@ pub struct CrateGraph {
83#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 83#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
84pub struct CrateId(pub u32); 84pub struct CrateId(pub u32);
85 85
86pub struct CrateName(SmolStr);
87
88impl CrateName {
89 /// Crates a crate name, checking for dashes in the string provided.
90 /// Dashes are not allowed in the crate names,
91 /// hence the input string is returned as `Err` for those cases.
92 pub fn new(name: &str) -> Result<CrateName, &str> {
93 if name.contains('-') {
94 Err(name)
95 } else {
96 Ok(Self(SmolStr::new(name)))
97 }
98 }
99
100 /// Crates a crate name, unconditionally replacing the dashes with underscores.
101 pub fn normalize_dashes(name: &str) -> CrateName {
102 Self(SmolStr::new(name.replace('-', "_")))
103 }
104}
105
86#[derive(Debug, Clone, PartialEq, Eq)] 106#[derive(Debug, Clone, PartialEq, Eq)]
87struct CrateData { 107struct CrateData {
88 file_id: FileId, 108 file_id: FileId,
@@ -131,13 +151,13 @@ impl CrateGraph {
131 pub fn add_dep( 151 pub fn add_dep(
132 &mut self, 152 &mut self,
133 from: CrateId, 153 from: CrateId,
134 name: SmolStr, 154 name: CrateName,
135 to: CrateId, 155 to: CrateId,
136 ) -> Result<(), CyclicDependenciesError> { 156 ) -> Result<(), CyclicDependenciesError> {
137 if self.dfs_find(from, to, &mut FxHashSet::default()) { 157 if self.dfs_find(from, to, &mut FxHashSet::default()) {
138 return Err(CyclicDependenciesError); 158 return Err(CyclicDependenciesError);
139 } 159 }
140 self.arena.get_mut(&from).unwrap().add_dep(name, to); 160 self.arena.get_mut(&from).unwrap().add_dep(name.0, to);
141 Ok(()) 161 Ok(())
142 } 162 }
143 163
@@ -268,7 +288,7 @@ pub struct CyclicDependenciesError;
268 288
269#[cfg(test)] 289#[cfg(test)]
270mod tests { 290mod tests {
271 use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr}; 291 use super::{CfgOptions, CrateGraph, CrateName, Dependency, Edition::Edition2018, Env, FileId};
272 292
273 #[test] 293 #[test]
274 fn it_should_panic_because_of_cycle_dependencies() { 294 fn it_should_panic_because_of_cycle_dependencies() {
@@ -279,9 +299,9 @@ mod tests {
279 graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); 299 graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default());
280 let crate3 = 300 let crate3 =
281 graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); 301 graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default());
282 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); 302 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
283 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); 303 assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
284 assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err()); 304 assert!(graph.add_dep(crate3, CrateName::new("crate1").unwrap(), crate1).is_err());
285 } 305 }
286 306
287 #[test] 307 #[test]
@@ -293,7 +313,23 @@ mod tests {
293 graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); 313 graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default());
294 let crate3 = 314 let crate3 =
295 graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); 315 graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default());
296 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); 316 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
297 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); 317 assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
318 }
319
320 #[test]
321 fn dashes_are_normalized() {
322 let mut graph = CrateGraph::default();
323 let crate1 =
324 graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default());
325 let crate2 =
326 graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default());
327 assert!(graph
328 .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)
329 .is_ok());
330 assert_eq!(
331 graph.dependencies(crate1).collect::<Vec<_>>(),
332 vec![&Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }]
333 );
298 } 334 }
299} 335}