From 307c6fec618d5e509c754362687253ef44bb5f3f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 1 Jul 2020 09:53:53 +0200 Subject: Use CrateName for semantic names --- crates/ra_project_model/src/lib.rs | 5 +---- crates/ra_project_model/src/project_json.rs | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) (limited to 'crates/ra_project_model/src') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 8b85b4831..1533d32c9 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -288,10 +288,7 @@ impl ProjectWorkspace { if let (Some(&from), Some(&to)) = (crates.get(&from_crate_id), crates.get(&to_crate_id)) { - if crate_graph - .add_dep(from, CrateName::new(&dep.name).unwrap(), to) - .is_err() - { + if crate_graph.add_dep(from, dep.name.clone(), to).is_err() { log::error!( "cyclic dependency {:?} -> {:?}", from_crate_id, diff --git a/crates/ra_project_model/src/project_json.rs b/crates/ra_project_model/src/project_json.rs index 4b5dcd634..9fe1e2dcb 100644 --- a/crates/ra_project_model/src/project_json.rs +++ b/crates/ra_project_model/src/project_json.rs @@ -4,9 +4,9 @@ use std::path::PathBuf; use paths::{AbsPath, AbsPathBuf}; use ra_cfg::CfgOptions; -use ra_db::{CrateId, Dependency, Edition}; +use ra_db::{CrateId, CrateName, Dependency, Edition}; use rustc_hash::FxHashSet; -use serde::Deserialize; +use serde::{de, Deserialize}; use stdx::split_delim; /// Roots and crates that compose this Rust project. @@ -50,7 +50,7 @@ impl ProjectJson { .into_iter() .map(|dep_data| Dependency { crate_id: CrateId(dep_data.krate as u32), - name: dep_data.name.into(), + name: dep_data.name, }) .collect::>(), cfg: { @@ -113,5 +113,14 @@ struct DepData { /// Identifies a crate by position in the crates array. #[serde(rename = "crate")] krate: usize, - name: String, + #[serde(deserialize_with = "deserialize_crate_name")] + name: CrateName, +} + +fn deserialize_crate_name<'de, D>(de: D) -> Result +where + D: de::Deserializer<'de>, +{ + let name = String::deserialize(de)?; + CrateName::new(&name).map_err(|err| de::Error::custom(format!("invalid crate name: {:?}", err))) } -- cgit v1.2.3 From 80386ca5be78d8ea65483df3edeec1a89b09a5a3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 1 Jul 2020 10:03:07 +0200 Subject: Use Strings for display names --- crates/ra_project_model/src/lib.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'crates/ra_project_model/src') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 1533d32c9..8dbf4e6ea 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -309,13 +309,11 @@ impl ProjectWorkspace { let env = Env::default(); let proc_macro = vec![]; - let crate_name = CrateName::new(&sysroot[krate].name) - .expect("Sysroot crate names should not contain dashes"); - + let name = sysroot[krate].name.clone(); let crate_id = crate_graph.add_crate_root( file_id, Edition::Edition2018, - Some(crate_name), + Some(name), cfg_options.clone(), env, proc_macro, @@ -389,7 +387,7 @@ impl ProjectWorkspace { let crate_id = crate_graph.add_crate_root( file_id, edition, - Some(CrateName::normalize_dashes(&cargo[pkg].name)), + Some(cargo[pkg].name.clone()), cfg_options, env, proc_macro.clone(), -- cgit v1.2.3