From 78092c7c66a665ada55ea09476a387014f6665b0 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Wed, 5 Feb 2020 12:47:28 +0200 Subject: Apply the reviews suggestions --- crates/ra_db/src/fixture.rs | 6 +++--- crates/ra_db/src/input.rs | 37 ++++++++++++++++++++++++------------- crates/ra_db/src/lib.rs | 4 +++- 3 files changed, 30 insertions(+), 17 deletions(-) (limited to 'crates/ra_db') 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; use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; use crate::{ - CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf, SourceDatabaseExt, - SourceRoot, SourceRootId, + input::CrateName, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf, + SourceDatabaseExt, SourceRoot, SourceRootId, }; pub const WORKSPACE: SourceRootId = SourceRootId(0); @@ -139,7 +139,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option> From for CrateName { - fn from(name: T) -> Self { - // For root projects with dashes in their name, - // cargo metadata does not do any normalization - // so we do it ourselves - Self(SmolStr::new(name.as_ref().replace('-', "_"))) +impl CrateName { + /// Crates a crate name, checking for dashes in the string provided. + /// Dashes are not allowed in the crate names, + /// hence the input string is returned as `Err` for those cases. + pub fn new(name: &str) -> Result { + if name.contains('-') { + Err(name) + } else { + Ok(Self(SmolStr::new(name))) + } + } + + /// Crates a crate name, unconditionally replacing the dashes with underscores. + pub fn normalize_dashes(name: &str) -> CrateName { + Self(SmolStr::new(name.replace('-', "_"))) } } @@ -279,7 +288,7 @@ pub struct CyclicDependenciesError; #[cfg(test)] mod tests { - use super::{CfgOptions, CrateGraph, Dependency, Edition::Edition2018, Env, FileId}; + use super::{CfgOptions, CrateGraph, CrateName, Dependency, Edition::Edition2018, Env, FileId}; #[test] fn it_should_panic_because_of_cycle_dependencies() { @@ -290,9 +299,9 @@ mod tests { graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); - assert!(graph.add_dep(crate1, "crate2".into(), crate2).is_ok()); - assert!(graph.add_dep(crate2, "crate3".into(), crate3).is_ok()); - assert!(graph.add_dep(crate3, "crate1".into(), crate1).is_err()); + assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); + assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); + assert!(graph.add_dep(crate3, CrateName::new("crate1").unwrap(), crate1).is_err()); } #[test] @@ -304,8 +313,8 @@ mod tests { graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); - assert!(graph.add_dep(crate1, "crate2".into(), crate2).is_ok()); - assert!(graph.add_dep(crate2, "crate3".into(), crate3).is_ok()); + assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); + assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); } #[test] @@ -315,7 +324,9 @@ mod tests { graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); - assert!(graph.add_dep(crate1, "crate-name-with-dashes".into(), crate2).is_ok()); + assert!(graph + .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) + .is_ok()); assert_eq!( graph.dependencies(crate1).collect::>(), 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}; pub use crate::{ cancellation::Canceled, - input::{CrateGraph, CrateId, Dependency, Edition, Env, FileId, SourceRoot, SourceRootId}, + input::{ + CrateGraph, CrateId, CrateName, Dependency, Edition, Env, FileId, SourceRoot, SourceRootId, + }, }; pub use relative_path::{RelativePath, RelativePathBuf}; pub use salsa; -- cgit v1.2.3