diff options
Diffstat (limited to 'crates/project_model/src/project_json.rs')
-rw-r--r-- | crates/project_model/src/project_json.rs | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/crates/project_model/src/project_json.rs b/crates/project_model/src/project_json.rs index 5a0fe749a..a6895ecdd 100644 --- a/crates/project_model/src/project_json.rs +++ b/crates/project_model/src/project_json.rs | |||
@@ -7,12 +7,13 @@ use paths::{AbsPath, AbsPathBuf}; | |||
7 | use rustc_hash::FxHashMap; | 7 | use rustc_hash::FxHashMap; |
8 | use serde::{de, Deserialize}; | 8 | use serde::{de, Deserialize}; |
9 | 9 | ||
10 | use crate::{cfg_flag::CfgFlag, Sysroot}; | 10 | use crate::cfg_flag::CfgFlag; |
11 | 11 | ||
12 | /// Roots and crates that compose this Rust project. | 12 | /// Roots and crates that compose this Rust project. |
13 | #[derive(Clone, Debug, Eq, PartialEq)] | 13 | #[derive(Clone, Debug, Eq, PartialEq)] |
14 | pub struct ProjectJson { | 14 | pub struct ProjectJson { |
15 | pub(crate) sysroot: Option<Sysroot>, | 15 | pub(crate) sysroot_src: Option<AbsPathBuf>, |
16 | project_root: AbsPathBuf, | ||
16 | crates: Vec<Crate>, | 17 | crates: Vec<Crate>, |
17 | } | 18 | } |
18 | 19 | ||
@@ -33,9 +34,17 @@ pub struct Crate { | |||
33 | } | 34 | } |
34 | 35 | ||
35 | impl ProjectJson { | 36 | impl ProjectJson { |
37 | /// Create a new ProjectJson instance. | ||
38 | /// | ||
39 | /// # Arguments | ||
40 | /// | ||
41 | /// * `base` - The path to the workspace root (i.e. the folder containing `rust-project.json`) | ||
42 | /// * `data` - The parsed contents of `rust-project.json`, or project json that's passed via | ||
43 | /// configuration. | ||
36 | pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson { | 44 | pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson { |
37 | ProjectJson { | 45 | ProjectJson { |
38 | sysroot: data.sysroot_src.map(|it| base.join(it)).map(|it| Sysroot::load(&it)), | 46 | sysroot_src: data.sysroot_src.map(|it| base.join(it)), |
47 | project_root: base.to_path_buf(), | ||
39 | crates: data | 48 | crates: data |
40 | .crates | 49 | .crates |
41 | .into_iter() | 50 | .into_iter() |
@@ -83,12 +92,18 @@ impl ProjectJson { | |||
83 | .collect::<Vec<_>>(), | 92 | .collect::<Vec<_>>(), |
84 | } | 93 | } |
85 | } | 94 | } |
95 | /// Returns the number of crates in the project. | ||
86 | pub fn n_crates(&self) -> usize { | 96 | pub fn n_crates(&self) -> usize { |
87 | self.crates.len() | 97 | self.crates.len() |
88 | } | 98 | } |
99 | /// Returns an iterator over the crates in the project. | ||
89 | pub fn crates(&self) -> impl Iterator<Item = (CrateId, &Crate)> + '_ { | 100 | pub fn crates(&self) -> impl Iterator<Item = (CrateId, &Crate)> + '_ { |
90 | self.crates.iter().enumerate().map(|(idx, krate)| (CrateId(idx as u32), krate)) | 101 | self.crates.iter().enumerate().map(|(idx, krate)| (CrateId(idx as u32), krate)) |
91 | } | 102 | } |
103 | /// Returns the path to the project's root folder. | ||
104 | pub fn path(&self) -> &AbsPath { | ||
105 | &self.project_root | ||
106 | } | ||
92 | } | 107 | } |
93 | 108 | ||
94 | #[derive(Deserialize)] | 109 | #[derive(Deserialize)] |