1use base_db::{CrateDisplayName, CrateName};
53use cfg::CfgAtom;
54use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
55use rustc_hash::{FxHashMap, FxHashSet};
56use serde::{Deserialize, Serialize, de};
57use span::Edition;
58
59use crate::{ManifestPath, TargetKind};
60
61#[derive(Clone, Debug, Eq, PartialEq)]
63pub struct ProjectJson {
64 pub(crate) sysroot: Option<AbsPathBuf>,
66 pub(crate) sysroot_src: Option<AbsPathBuf>,
68 pub(crate) sysroot_project: Option<Box<ProjectJson>>,
70 project_root: AbsPathBuf,
71 manifest: Option<ManifestPath>,
74 crates: Vec<Crate>,
75 runnables: Vec<Runnable>,
79}
80
81impl ProjectJson {
82 pub fn new(
90 manifest: Option<ManifestPath>,
91 base: &AbsPath,
92 data: ProjectJsonData,
93 ) -> ProjectJson {
94 let absolutize_on_base = |p| base.absolutize(p);
95 let sysroot_src = data.sysroot_src.map(absolutize_on_base);
96 let sysroot_project =
97 data.sysroot_project.zip(sysroot_src.clone()).map(|(sysroot_data, sysroot_src)| {
98 Box::new(ProjectJson::new(None, &sysroot_src, *sysroot_data))
99 });
100
101 ProjectJson {
102 sysroot: data.sysroot.map(absolutize_on_base),
103 sysroot_src,
104 sysroot_project,
105 project_root: base.to_path_buf(),
106 manifest,
107 runnables: data.runnables.into_iter().map(Runnable::from).collect(),
108 crates: data
109 .crates
110 .into_iter()
111 .map(|crate_data| {
112 let root_module = absolutize_on_base(crate_data.root_module);
113 let is_workspace_member = crate_data
114 .is_workspace_member
115 .unwrap_or_else(|| root_module.starts_with(base));
116 let (include, exclude) = match crate_data.source {
117 Some(src) => {
118 let absolutize = |dirs: Vec<Utf8PathBuf>| {
119 dirs.into_iter().map(absolutize_on_base).collect::<Vec<_>>()
120 };
121 (absolutize(src.include_dirs), absolutize(src.exclude_dirs))
122 }
123 None => (vec![root_module.parent().unwrap().to_path_buf()], Vec::new()),
124 };
125
126 let build = match crate_data.build {
127 Some(build) => Some(Build {
128 label: build.label,
129 build_file: build.build_file,
130 target_kind: build.target_kind.into(),
131 }),
132 None => None,
133 };
134
135 let cfg = crate_data
136 .cfg_groups
137 .iter()
138 .flat_map(|cfg_extend| {
139 let cfg_group = data.cfg_groups.get(cfg_extend);
140 match cfg_group {
141 Some(cfg_group) => cfg_group.0.iter().cloned(),
142 None => {
143 tracing::error!(
144 "Unknown cfg group `{cfg_extend}` in crate `{}`",
145 crate_data.display_name.as_deref().unwrap_or("<unknown>"),
146 );
147 [].iter().cloned()
148 }
149 }
150 })
151 .chain(crate_data.cfg.0)
152 .collect();
153
154 Crate {
155 display_name: crate_data
156 .display_name
157 .as_deref()
158 .map(CrateDisplayName::from_canonical_name),
159 root_module,
160 edition: crate_data.edition.into(),
161 version: crate_data.version.as_ref().map(ToString::to_string),
162 deps: crate_data.deps,
163 cfg,
164 target: crate_data.target,
165 env: crate_data.env,
166 proc_macro_dylib_path: crate_data
167 .proc_macro_dylib_path
168 .map(absolutize_on_base),
169 is_workspace_member,
170 include,
171 exclude,
172 is_proc_macro: crate_data.is_proc_macro,
173 repository: crate_data.repository,
174 build,
175 proc_macro_cwd: crate_data.proc_macro_cwd.map(absolutize_on_base),
176 }
177 })
178 .collect(),
179 }
180 }
181
182 pub fn n_crates(&self) -> usize {
184 self.crates.len()
185 }
186
187 pub fn crates(&self) -> impl Iterator<Item = (CrateArrayIdx, &Crate)> {
189 self.crates.iter().enumerate().map(|(idx, krate)| (CrateArrayIdx(idx), krate))
190 }
191
192 pub fn path(&self) -> &AbsPath {
194 &self.project_root
195 }
196
197 pub fn crate_by_root(&self, root: &AbsPath) -> Option<Crate> {
198 self.crates
199 .iter()
200 .filter(|krate| krate.is_workspace_member)
201 .find(|krate| krate.root_module == root)
202 .cloned()
203 }
204
205 pub fn manifest(&self) -> Option<&ManifestPath> {
207 self.manifest.as_ref()
208 }
209
210 pub fn crate_by_buildfile(&self, path: &AbsPath) -> Option<Build> {
211 let path: &std::path::Path = path.as_ref();
213 self.crates
214 .iter()
215 .filter(|krate| krate.is_workspace_member)
216 .filter_map(|krate| krate.build.clone())
217 .find(|build| build.build_file.as_std_path() == path)
218 }
219
220 pub fn manifest_or_root(&self) -> &AbsPath {
222 self.manifest.as_ref().map_or(&self.project_root, |manifest| manifest.as_ref())
223 }
224
225 pub fn project_root(&self) -> &AbsPath {
227 &self.project_root
228 }
229
230 pub fn runnables(&self) -> &[Runnable] {
231 &self.runnables
232 }
233}
234
235#[derive(Clone, Debug, Eq, PartialEq)]
238pub struct Crate {
239 pub(crate) display_name: Option<CrateDisplayName>,
240 pub root_module: AbsPathBuf,
241 pub(crate) edition: Edition,
242 pub(crate) version: Option<String>,
243 pub(crate) deps: Vec<Dep>,
244 pub(crate) cfg: Vec<CfgAtom>,
245 pub(crate) target: Option<String>,
246 pub(crate) env: FxHashMap<String, String>,
247 pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>,
248 pub(crate) is_workspace_member: bool,
249 pub(crate) include: Vec<AbsPathBuf>,
250 pub(crate) exclude: Vec<AbsPathBuf>,
251 pub(crate) is_proc_macro: bool,
252 pub(crate) proc_macro_cwd: Option<AbsPathBuf>,
254 pub(crate) repository: Option<String>,
255 pub build: Option<Build>,
256}
257
258#[derive(Clone, Debug, Eq, PartialEq)]
260pub struct Build {
261 pub label: String,
270 pub build_file: Utf8PathBuf,
276 pub target_kind: TargetKind,
282}
283
284#[derive(Debug, Clone, PartialEq, Eq)]
309pub struct Runnable {
310 pub program: String,
314 pub args: Vec<String>,
320 pub cwd: Utf8PathBuf,
322 pub kind: RunnableKind,
323}
324
325#[derive(Debug, Clone, PartialEq, Eq)]
327pub enum RunnableKind {
328 Check,
329
330 Run,
332
333 TestOne,
335}
336
337#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
338pub struct ProjectJsonData {
339 sysroot: Option<Utf8PathBuf>,
340 sysroot_src: Option<Utf8PathBuf>,
341 sysroot_project: Option<Box<ProjectJsonData>>,
342 #[serde(default)]
343 cfg_groups: FxHashMap<String, CfgList>,
344 crates: Vec<CrateData>,
345 #[serde(default)]
346 runnables: Vec<RunnableData>,
347}
348
349#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Default)]
350#[serde(transparent)]
351struct CfgList(#[serde(with = "cfg_")] Vec<CfgAtom>);
352
353#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
354struct CrateData {
355 display_name: Option<String>,
356 root_module: Utf8PathBuf,
357 edition: EditionData,
358 #[serde(default)]
359 version: Option<semver::Version>,
360 deps: Vec<Dep>,
361 #[serde(default)]
362 cfg_groups: FxHashSet<String>,
363 #[serde(default)]
364 cfg: CfgList,
365 target: Option<String>,
366 #[serde(default)]
367 env: FxHashMap<String, String>,
368 proc_macro_dylib_path: Option<Utf8PathBuf>,
369 is_workspace_member: Option<bool>,
370 source: Option<CrateSource>,
371 #[serde(default)]
372 is_proc_macro: bool,
373 #[serde(default)]
374 repository: Option<String>,
375 #[serde(default)]
376 build: Option<BuildData>,
377 #[serde(default)]
378 proc_macro_cwd: Option<Utf8PathBuf>,
379}
380
381mod cfg_ {
382 use cfg::CfgAtom;
383 use serde::{Deserialize, Serialize};
384
385 pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<Vec<CfgAtom>, D::Error>
386 where
387 D: serde::Deserializer<'de>,
388 {
389 let cfg: Vec<String> = Vec::deserialize(deserializer)?;
390 cfg.into_iter().map(|it| crate::parse_cfg(&it).map_err(serde::de::Error::custom)).collect()
391 }
392 pub(super) fn serialize<S>(cfg: &[CfgAtom], serializer: S) -> Result<S::Ok, S::Error>
393 where
394 S: serde::Serializer,
395 {
396 cfg.iter()
397 .map(|cfg| match cfg {
398 CfgAtom::Flag(flag) => flag.as_str().to_owned(),
399 CfgAtom::KeyValue { key, value } => {
400 format!("{}=\"{}\"", key.as_str(), value.as_str())
401 }
402 })
403 .collect::<Vec<String>>()
404 .serialize(serializer)
405 }
406}
407
408#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
409#[serde(rename = "edition")]
410enum EditionData {
411 #[serde(rename = "2015")]
412 Edition2015,
413 #[serde(rename = "2018")]
414 Edition2018,
415 #[serde(rename = "2021")]
416 Edition2021,
417 #[serde(rename = "2024")]
418 Edition2024,
419}
420
421#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
422pub struct BuildData {
423 label: String,
424 build_file: Utf8PathBuf,
425 target_kind: TargetKindData,
426}
427
428#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
429pub struct RunnableData {
430 pub program: String,
431 pub args: Vec<String>,
432 pub cwd: Utf8PathBuf,
433 pub kind: RunnableKindData,
434}
435
436#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
437#[serde(rename_all = "camelCase")]
438pub enum RunnableKindData {
439 Check,
440 Run,
441 TestOne,
442}
443
444#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
445#[serde(rename_all = "camelCase")]
446pub enum TargetKindData {
447 Bin,
448 Lib,
450 Test,
451}
452#[derive(Serialize, Deserialize, Debug, Clone, Copy, Eq, PartialEq, Hash)]
457#[serde(transparent)]
458pub struct CrateArrayIdx(pub usize);
459
460#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
461pub(crate) struct Dep {
462 #[serde(rename = "crate")]
464 pub(crate) krate: CrateArrayIdx,
465 #[serde(serialize_with = "serialize_crate_name")]
466 #[serde(deserialize_with = "deserialize_crate_name")]
467 pub(crate) name: CrateName,
468}
469
470#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
471struct CrateSource {
472 include_dirs: Vec<Utf8PathBuf>,
473 exclude_dirs: Vec<Utf8PathBuf>,
474}
475
476impl From<TargetKindData> for TargetKind {
477 fn from(data: TargetKindData) -> Self {
478 match data {
479 TargetKindData::Bin => TargetKind::Bin,
480 TargetKindData::Lib => TargetKind::Lib { is_proc_macro: false },
481 TargetKindData::Test => TargetKind::Test,
482 }
483 }
484}
485
486impl From<EditionData> for Edition {
487 fn from(data: EditionData) -> Self {
488 match data {
489 EditionData::Edition2015 => Edition::Edition2015,
490 EditionData::Edition2018 => Edition::Edition2018,
491 EditionData::Edition2021 => Edition::Edition2021,
492 EditionData::Edition2024 => Edition::Edition2024,
493 }
494 }
495}
496
497impl From<RunnableData> for Runnable {
498 fn from(data: RunnableData) -> Self {
499 Runnable { program: data.program, args: data.args, cwd: data.cwd, kind: data.kind.into() }
500 }
501}
502
503impl From<RunnableKindData> for RunnableKind {
504 fn from(data: RunnableKindData) -> Self {
505 match data {
506 RunnableKindData::Check => RunnableKind::Check,
507 RunnableKindData::Run => RunnableKind::Run,
508 RunnableKindData::TestOne => RunnableKind::TestOne,
509 }
510 }
511}
512
513fn deserialize_crate_name<'de, D>(de: D) -> std::result::Result<CrateName, D::Error>
514where
515 D: de::Deserializer<'de>,
516{
517 let name = String::deserialize(de)?;
518 CrateName::new(&name).map_err(|err| de::Error::custom(format!("invalid crate name: {err:?}")))
519}
520
521fn serialize_crate_name<S>(name: &CrateName, se: S) -> Result<S::Ok, S::Error>
522where
523 S: serde::Serializer,
524{
525 se.serialize_str(name.as_str())
526}