diff options
Diffstat (limited to 'crates/ra_db/src')
-rw-r--r-- | crates/ra_db/src/fixture.rs | 6 | ||||
-rw-r--r-- | crates/ra_db/src/input.rs | 37 | ||||
-rw-r--r-- | crates/ra_db/src/lib.rs | 4 |
3 files changed, 30 insertions, 17 deletions
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 30b598e9a..17cd138c2 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs | |||
@@ -8,8 +8,8 @@ use rustc_hash::FxHashMap; | |||
8 | use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; | 8 | use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf, SourceDatabaseExt, | 11 | input::CrateName, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf, |
12 | SourceRoot, SourceRootId, | 12 | SourceDatabaseExt, SourceRoot, SourceRootId, |
13 | }; | 13 | }; |
14 | 14 | ||
15 | pub const WORKSPACE: SourceRootId = SourceRootId(0); | 15 | pub const WORKSPACE: SourceRootId = SourceRootId(0); |
@@ -139,7 +139,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
139 | for (from, to) in crate_deps { | 139 | for (from, to) in crate_deps { |
140 | let from_id = crates[&from]; | 140 | let from_id = crates[&from]; |
141 | let to_id = crates[&to]; | 141 | let to_id = crates[&to]; |
142 | crate_graph.add_dep(from_id, to.into(), to_id).unwrap(); | 142 | crate_graph.add_dep(from_id, CrateName::new(&to).unwrap(), to_id).unwrap(); |
143 | } | 143 | } |
144 | } | 144 | } |
145 | 145 | ||
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index e65761c00..1f1dcea42 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs | |||
@@ -85,12 +85,21 @@ pub struct CrateId(pub u32); | |||
85 | 85 | ||
86 | pub struct CrateName(SmolStr); | 86 | pub struct CrateName(SmolStr); |
87 | 87 | ||
88 | impl<T: AsRef<str>> From<T> for CrateName { | 88 | impl CrateName { |
89 | fn from(name: T) -> Self { | 89 | /// Crates a crate name, checking for dashes in the string provided. |
90 | // For root projects with dashes in their name, | 90 | /// Dashes are not allowed in the crate names, |
91 | // cargo metadata does not do any normalization | 91 | /// hence the input string is returned as `Err` for those cases. |
92 | // so we do it ourselves | 92 | pub fn new(name: &str) -> Result<CrateName, &str> { |
93 | Self(SmolStr::new(name.as_ref().replace('-', "_"))) | 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('-', "_"))) | ||
94 | } | 103 | } |
95 | } | 104 | } |
96 | 105 | ||
@@ -279,7 +288,7 @@ pub struct CyclicDependenciesError; | |||
279 | 288 | ||
280 | #[cfg(test)] | 289 | #[cfg(test)] |
281 | mod tests { | 290 | mod tests { |
282 | use super::{CfgOptions, CrateGraph, Dependency, Edition::Edition2018, Env, FileId}; | 291 | use super::{CfgOptions, CrateGraph, CrateName, Dependency, Edition::Edition2018, Env, FileId}; |
283 | 292 | ||
284 | #[test] | 293 | #[test] |
285 | fn it_should_panic_because_of_cycle_dependencies() { | 294 | fn it_should_panic_because_of_cycle_dependencies() { |
@@ -290,9 +299,9 @@ mod tests { | |||
290 | graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); | 299 | graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); |
291 | let crate3 = | 300 | let crate3 = |
292 | graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); | 301 | graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); |
293 | assert!(graph.add_dep(crate1, "crate2".into(), crate2).is_ok()); | 302 | assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); |
294 | assert!(graph.add_dep(crate2, "crate3".into(), crate3).is_ok()); | 303 | assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); |
295 | assert!(graph.add_dep(crate3, "crate1".into(), crate1).is_err()); | 304 | assert!(graph.add_dep(crate3, CrateName::new("crate1").unwrap(), crate1).is_err()); |
296 | } | 305 | } |
297 | 306 | ||
298 | #[test] | 307 | #[test] |
@@ -304,8 +313,8 @@ mod tests { | |||
304 | graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); | 313 | graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); |
305 | let crate3 = | 314 | let crate3 = |
306 | graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); | 315 | graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); |
307 | assert!(graph.add_dep(crate1, "crate2".into(), crate2).is_ok()); | 316 | assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); |
308 | assert!(graph.add_dep(crate2, "crate3".into(), crate3).is_ok()); | 317 | assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); |
309 | } | 318 | } |
310 | 319 | ||
311 | #[test] | 320 | #[test] |
@@ -315,7 +324,9 @@ mod tests { | |||
315 | graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); | 324 | graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); |
316 | let crate2 = | 325 | let crate2 = |
317 | graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); | 326 | 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()); | 327 | assert!(graph |
328 | .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) | ||
329 | .is_ok()); | ||
319 | assert_eq!( | 330 | assert_eq!( |
320 | graph.dependencies(crate1).collect::<Vec<_>>(), | 331 | graph.dependencies(crate1).collect::<Vec<_>>(), |
321 | vec![&Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] | 332 | vec![&Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] |
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 21341b769..fb002d717 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs | |||
@@ -10,7 +10,9 @@ use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit}; | |||
10 | 10 | ||
11 | pub use crate::{ | 11 | pub use crate::{ |
12 | cancellation::Canceled, | 12 | cancellation::Canceled, |
13 | input::{CrateGraph, CrateId, Dependency, Edition, Env, FileId, SourceRoot, SourceRootId}, | 13 | input::{ |
14 | CrateGraph, CrateId, CrateName, Dependency, Edition, Env, FileId, SourceRoot, SourceRootId, | ||
15 | }, | ||
14 | }; | 16 | }; |
15 | pub use relative_path::{RelativePath, RelativePathBuf}; | 17 | pub use relative_path::{RelativePath, RelativePathBuf}; |
16 | pub use salsa; | 18 | pub use salsa; |