Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/project-model/src/project_json.rs')
-rw-r--r--crates/project-model/src/project_json.rs63
1 files changed, 33 insertions, 30 deletions
diff --git a/crates/project-model/src/project_json.rs b/crates/project-model/src/project_json.rs
index 512588cc8f..fac6eb8ad3 100644
--- a/crates/project-model/src/project_json.rs
+++ b/crates/project-model/src/project_json.rs
@@ -49,14 +49,13 @@
//! user explores them belongs to that extension (it's totally valid to change
//! rust-project.json over time via configuration request!)
-use base_db::{CrateDisplayName, CrateId, CrateName, Dependency};
-use la_arena::RawIdx;
+use base_db::{CrateDisplayName, CrateName};
use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
use rustc_hash::FxHashMap;
-use serde::{de, Deserialize};
+use serde::{de, Deserialize, Serialize};
use span::Edition;
-use crate::cfg_flag::CfgFlag;
+use crate::cfg::CfgFlag;
/// Roots and crates that compose this Rust project.
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -74,10 +73,10 @@ pub struct ProjectJson {
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Crate {
pub(crate) display_name: Option<CrateDisplayName>,
- pub(crate) root_module: AbsPathBuf,
+ pub root_module: AbsPathBuf,
pub(crate) edition: Edition,
pub(crate) version: Option<String>,
- pub(crate) deps: Vec<Dependency>,
+ pub(crate) deps: Vec<Dep>,
pub(crate) cfg: Vec<CfgFlag>,
pub(crate) target: Option<String>,
pub(crate) env: FxHashMap<String, String>,
@@ -128,16 +127,7 @@ impl ProjectJson {
root_module,
edition: crate_data.edition.into(),
version: crate_data.version.as_ref().map(ToString::to_string),
- deps: crate_data
- .deps
- .into_iter()
- .map(|dep_data| {
- Dependency::new(
- dep_data.name,
- CrateId::from_raw(RawIdx::from(dep_data.krate as u32)),
- )
- })
- .collect::<Vec<_>>(),
+ deps: crate_data.deps,
cfg: crate_data.cfg,
target: crate_data.target,
env: crate_data.env,
@@ -161,11 +151,8 @@ impl ProjectJson {
}
/// Returns an iterator over the crates in the project.
- pub fn crates(&self) -> impl Iterator<Item = (CrateId, &Crate)> + '_ {
- self.crates
- .iter()
- .enumerate()
- .map(|(idx, krate)| (CrateId::from_raw(RawIdx::from(idx as u32)), krate))
+ pub fn crates(&self) -> impl Iterator<Item = (CrateArrayIdx, &Crate)> {
+ self.crates.iter().enumerate().map(|(idx, krate)| (CrateArrayIdx(idx), krate))
}
/// Returns the path to the project's root folder.
@@ -174,21 +161,21 @@ impl ProjectJson {
}
}
-#[derive(Deserialize, Debug, Clone)]
+#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProjectJsonData {
sysroot: Option<Utf8PathBuf>,
sysroot_src: Option<Utf8PathBuf>,
crates: Vec<CrateData>,
}
-#[derive(Deserialize, Debug, Clone)]
+#[derive(Serialize, Deserialize, Debug, Clone)]
struct CrateData {
display_name: Option<String>,
root_module: Utf8PathBuf,
edition: EditionData,
#[serde(default)]
version: Option<semver::Version>,
- deps: Vec<DepData>,
+ deps: Vec<Dep>,
#[serde(default)]
cfg: Vec<CfgFlag>,
target: Option<String>,
@@ -203,7 +190,7 @@ struct CrateData {
repository: Option<String>,
}
-#[derive(Deserialize, Debug, Clone)]
+#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename = "edition")]
enum EditionData {
#[serde(rename = "2015")]
@@ -227,16 +214,25 @@ impl From<EditionData> for Edition {
}
}
-#[derive(Deserialize, Debug, Clone)]
-struct DepData {
+/// Identifies a crate by position in the crates array.
+///
+/// This will differ from `CrateId` when multiple `ProjectJson`
+/// workspaces are loaded.
+#[derive(Serialize, Deserialize, Debug, Clone, Copy, Eq, PartialEq, Hash)]
+#[serde(transparent)]
+pub struct CrateArrayIdx(pub usize);
+
+#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
+pub(crate) struct Dep {
/// Identifies a crate by position in the crates array.
#[serde(rename = "crate")]
- krate: usize,
+ pub(crate) krate: CrateArrayIdx,
+ #[serde(serialize_with = "serialize_crate_name")]
#[serde(deserialize_with = "deserialize_crate_name")]
- name: CrateName,
+ pub(crate) name: CrateName,
}
-#[derive(Deserialize, Debug, Clone)]
+#[derive(Serialize, Deserialize, Debug, Clone)]
struct CrateSource {
include_dirs: Vec<Utf8PathBuf>,
exclude_dirs: Vec<Utf8PathBuf>,
@@ -249,3 +245,10 @@ where
let name = String::deserialize(de)?;
CrateName::new(&name).map_err(|err| de::Error::custom(format!("invalid crate name: {err:?}")))
}
+
+fn serialize_crate_name<S>(name: &CrateName, se: S) -> Result<S::Ok, S::Error>
+where
+ S: serde::Serializer,
+{
+ se.serialize_str(name)
+}