aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-02-05 09:53:54 +0000
committerKirill Bulatov <[email protected]>2020-02-05 09:53:54 +0000
commit2b9952625bd1a5d56c0955fa7887e0296b30ae76 (patch)
treed1671ff1b5b7a32f6d29889554a6b8d19138c337
parentc9e1aab8803264a40023aa09600b0a8fab8e00ff (diff)
Normalize dashes in crate names
-rw-r--r--crates/ra_db/src/input.rs41
1 files changed, 33 insertions, 8 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index 07269237a..e65761c00 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -83,6 +83,17 @@ 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<T: AsRef<str>> From<T> for CrateName {
89 fn from(name: T) -> Self {
90 // For root projects with dashes in their name,
91 // cargo metadata does not do any normalization
92 // so we do it ourselves
93 Self(SmolStr::new(name.as_ref().replace('-', "_")))
94 }
95}
96
86#[derive(Debug, Clone, PartialEq, Eq)] 97#[derive(Debug, Clone, PartialEq, Eq)]
87struct CrateData { 98struct CrateData {
88 file_id: FileId, 99 file_id: FileId,
@@ -131,13 +142,13 @@ impl CrateGraph {
131 pub fn add_dep( 142 pub fn add_dep(
132 &mut self, 143 &mut self,
133 from: CrateId, 144 from: CrateId,
134 name: SmolStr, 145 name: CrateName,
135 to: CrateId, 146 to: CrateId,
136 ) -> Result<(), CyclicDependenciesError> { 147 ) -> Result<(), CyclicDependenciesError> {
137 if self.dfs_find(from, to, &mut FxHashSet::default()) { 148 if self.dfs_find(from, to, &mut FxHashSet::default()) {
138 return Err(CyclicDependenciesError); 149 return Err(CyclicDependenciesError);
139 } 150 }
140 self.arena.get_mut(&from).unwrap().add_dep(name, to); 151 self.arena.get_mut(&from).unwrap().add_dep(name.0, to);
141 Ok(()) 152 Ok(())
142 } 153 }
143 154
@@ -268,7 +279,7 @@ pub struct CyclicDependenciesError;
268 279
269#[cfg(test)] 280#[cfg(test)]
270mod tests { 281mod tests {
271 use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr}; 282 use super::{CfgOptions, CrateGraph, Dependency, Edition::Edition2018, Env, FileId};
272 283
273 #[test] 284 #[test]
274 fn it_should_panic_because_of_cycle_dependencies() { 285 fn it_should_panic_because_of_cycle_dependencies() {
@@ -279,9 +290,9 @@ mod tests {
279 graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); 290 graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default());
280 let crate3 = 291 let crate3 =
281 graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); 292 graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default());
282 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); 293 assert!(graph.add_dep(crate1, "crate2".into(), crate2).is_ok());
283 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); 294 assert!(graph.add_dep(crate2, "crate3".into(), crate3).is_ok());
284 assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err()); 295 assert!(graph.add_dep(crate3, "crate1".into(), crate1).is_err());
285 } 296 }
286 297
287 #[test] 298 #[test]
@@ -293,7 +304,21 @@ mod tests {
293 graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); 304 graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default());
294 let crate3 = 305 let crate3 =
295 graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); 306 graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default());
296 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); 307 assert!(graph.add_dep(crate1, "crate2".into(), crate2).is_ok());
297 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); 308 assert!(graph.add_dep(crate2, "crate3".into(), crate3).is_ok());
309 }
310
311 #[test]
312 fn dashes_are_normalized() {
313 let mut graph = CrateGraph::default();
314 let crate1 =
315 graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default());
316 let crate2 =
317 graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default());
318 assert!(graph.add_dep(crate1, "crate-name-with-dashes".into(), crate2).is_ok());
319 assert_eq!(
320 graph.dependencies(crate1).collect::<Vec<_>>(),
321 vec![&Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }]
322 );
298 } 323 }
299} 324}