diff options
author | Aleksey Kladov <[email protected]> | 2018-09-03 19:03:37 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-09-03 19:03:37 +0100 |
commit | 47cbaeba6f21e59ee8735bfe8bcbf06300767b57 (patch) | |
tree | 779f75f740a1717ab225a52f26f53562c28ace4d /crates/server/src/project_model.rs | |
parent | b04c14d4ad51433b0055e2e5799f98da20d15d58 (diff) |
Index deps
Diffstat (limited to 'crates/server/src/project_model.rs')
-rw-r--r-- | crates/server/src/project_model.rs | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/crates/server/src/project_model.rs b/crates/server/src/project_model.rs index 12233f258..517836e62 100644 --- a/crates/server/src/project_model.rs +++ b/crates/server/src/project_model.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | collections::HashMap, | 2 | collections::{HashMap, HashSet}, |
3 | path::{Path, PathBuf}, | 3 | path::{Path, PathBuf}, |
4 | }; | 4 | }; |
5 | use cargo_metadata::{metadata_run, CargoOpt}; | 5 | use cargo_metadata::{metadata_run, CargoOpt}; |
@@ -13,7 +13,6 @@ use { | |||
13 | 13 | ||
14 | #[derive(Debug, Serialize, Clone)] | 14 | #[derive(Debug, Serialize, Clone)] |
15 | pub struct CargoWorkspace { | 15 | pub struct CargoWorkspace { |
16 | ws_members: Vec<Package>, | ||
17 | packages: Vec<PackageData>, | 16 | packages: Vec<PackageData>, |
18 | targets: Vec<TargetData>, | 17 | targets: Vec<TargetData>, |
19 | } | 18 | } |
@@ -27,7 +26,8 @@ pub struct Target(usize); | |||
27 | struct PackageData { | 26 | struct PackageData { |
28 | name: SmolStr, | 27 | name: SmolStr, |
29 | manifest: PathBuf, | 28 | manifest: PathBuf, |
30 | targets: Vec<Target> | 29 | targets: Vec<Target>, |
30 | is_member: bool, | ||
31 | } | 31 | } |
32 | 32 | ||
33 | #[derive(Debug, Serialize, Clone)] | 33 | #[derive(Debug, Serialize, Clone)] |
@@ -50,9 +50,15 @@ impl Package { | |||
50 | pub fn manifest(self, ws: &CargoWorkspace) -> &Path { | 50 | pub fn manifest(self, ws: &CargoWorkspace) -> &Path { |
51 | ws.pkg(self).manifest.as_path() | 51 | ws.pkg(self).manifest.as_path() |
52 | } | 52 | } |
53 | pub fn root(self, ws: &CargoWorkspace) -> &Path { | ||
54 | ws.pkg(self).manifest.parent().unwrap() | ||
55 | } | ||
53 | pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item=Target> + 'a { | 56 | pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item=Target> + 'a { |
54 | ws.pkg(self).targets.iter().cloned() | 57 | ws.pkg(self).targets.iter().cloned() |
55 | } | 58 | } |
59 | pub fn is_member(self, ws: &CargoWorkspace) -> bool { | ||
60 | ws.pkg(self).is_member | ||
61 | } | ||
56 | } | 62 | } |
57 | 63 | ||
58 | impl Target { | 64 | impl Target { |
@@ -81,13 +87,21 @@ impl CargoWorkspace { | |||
81 | let mut pkg_by_id = HashMap::new(); | 87 | let mut pkg_by_id = HashMap::new(); |
82 | let mut packages = Vec::new(); | 88 | let mut packages = Vec::new(); |
83 | let mut targets = Vec::new(); | 89 | let mut targets = Vec::new(); |
90 | |||
91 | let ws_members: HashSet<String> = meta.workspace_members | ||
92 | .into_iter() | ||
93 | .map(|it| it.raw) | ||
94 | .collect(); | ||
95 | |||
84 | for meta_pkg in meta.packages { | 96 | for meta_pkg in meta.packages { |
85 | let pkg = Package(packages.len()); | 97 | let pkg = Package(packages.len()); |
98 | let is_member = ws_members.contains(&meta_pkg.id); | ||
86 | pkg_by_id.insert(meta_pkg.id.clone(), pkg); | 99 | pkg_by_id.insert(meta_pkg.id.clone(), pkg); |
87 | let mut pkg_data = PackageData { | 100 | let mut pkg_data = PackageData { |
88 | name: meta_pkg.name.into(), | 101 | name: meta_pkg.name.into(), |
89 | manifest: PathBuf::from(meta_pkg.manifest_path), | 102 | manifest: PathBuf::from(meta_pkg.manifest_path), |
90 | targets: Vec::new(), | 103 | targets: Vec::new(), |
104 | is_member, | ||
91 | }; | 105 | }; |
92 | for meta_tgt in meta_pkg.targets { | 106 | for meta_tgt in meta_pkg.targets { |
93 | let tgt = Target(targets.len()); | 107 | let tgt = Target(targets.len()); |
@@ -101,19 +115,12 @@ impl CargoWorkspace { | |||
101 | } | 115 | } |
102 | packages.push(pkg_data) | 116 | packages.push(pkg_data) |
103 | } | 117 | } |
104 | let ws_members = meta.workspace_members | ||
105 | .iter() | ||
106 | .map(|it| pkg_by_id[&it.raw]) | ||
107 | .collect(); | ||
108 | 118 | ||
109 | Ok(CargoWorkspace { packages, targets, ws_members }) | 119 | Ok(CargoWorkspace { packages, targets }) |
110 | } | 120 | } |
111 | pub fn packages<'a>(&'a self) -> impl Iterator<Item=Package> + 'a { | 121 | pub fn packages<'a>(&'a self) -> impl Iterator<Item=Package> + 'a { |
112 | (0..self.packages.len()).map(Package) | 122 | (0..self.packages.len()).map(Package) |
113 | } | 123 | } |
114 | pub fn ws_members<'a>(&'a self) -> impl Iterator<Item=Package> + 'a { | ||
115 | self.ws_members.iter().cloned() | ||
116 | } | ||
117 | pub fn target_by_root(&self, root: &Path) -> Option<Target> { | 124 | pub fn target_by_root(&self, root: &Path) -> Option<Target> { |
118 | self.packages() | 125 | self.packages() |
119 | .filter_map(|pkg| pkg.targets(self).find(|it| it.root(self) == root)) | 126 | .filter_map(|pkg| pkg.targets(self).find(|it| it.root(self) == root)) |