aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_project_model/src/lib.rs')
-rw-r--r--crates/ra_project_model/src/lib.rs42
1 files changed, 27 insertions, 15 deletions
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs
index 9b30bef8d..a99612690 100644
--- a/crates/ra_project_model/src/lib.rs
+++ b/crates/ra_project_model/src/lib.rs
@@ -14,7 +14,7 @@ use std::{
14use anyhow::{bail, Context, Result}; 14use anyhow::{bail, Context, Result};
15use ra_cfg::CfgOptions; 15use ra_cfg::CfgOptions;
16use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId}; 16use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId};
17use rustc_hash::FxHashMap; 17use rustc_hash::{FxHashMap, FxHashSet};
18use serde_json::from_reader; 18use serde_json::from_reader;
19 19
20pub use crate::{ 20pub use crate::{
@@ -57,25 +57,25 @@ impl PackageRoot {
57 } 57 }
58} 58}
59 59
60#[derive(Debug, Clone, PartialEq, Eq, Hash)] 60#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
61pub enum ProjectRoot { 61pub enum ProjectManifest {
62 ProjectJson(PathBuf), 62 ProjectJson(PathBuf),
63 CargoToml(PathBuf), 63 CargoToml(PathBuf),
64} 64}
65 65
66impl ProjectRoot { 66impl ProjectManifest {
67 pub fn from_manifest_file(path: PathBuf) -> Result<ProjectRoot> { 67 pub fn from_manifest_file(path: PathBuf) -> Result<ProjectManifest> {
68 if path.ends_with("rust-project.json") { 68 if path.ends_with("rust-project.json") {
69 return Ok(ProjectRoot::ProjectJson(path)); 69 return Ok(ProjectManifest::ProjectJson(path));
70 } 70 }
71 if path.ends_with("Cargo.toml") { 71 if path.ends_with("Cargo.toml") {
72 return Ok(ProjectRoot::CargoToml(path)); 72 return Ok(ProjectManifest::CargoToml(path));
73 } 73 }
74 bail!("project root must point to Cargo.toml or rust-project.json: {}", path.display()) 74 bail!("project root must point to Cargo.toml or rust-project.json: {}", path.display())
75 } 75 }
76 76
77 pub fn discover_single(path: &Path) -> Result<ProjectRoot> { 77 pub fn discover_single(path: &Path) -> Result<ProjectManifest> {
78 let mut candidates = ProjectRoot::discover(path)?; 78 let mut candidates = ProjectManifest::discover(path)?;
79 let res = match candidates.pop() { 79 let res = match candidates.pop() {
80 None => bail!("no projects"), 80 None => bail!("no projects"),
81 Some(it) => it, 81 Some(it) => it,
@@ -87,12 +87,12 @@ impl ProjectRoot {
87 Ok(res) 87 Ok(res)
88 } 88 }
89 89
90 pub fn discover(path: &Path) -> io::Result<Vec<ProjectRoot>> { 90 pub fn discover(path: &Path) -> io::Result<Vec<ProjectManifest>> {
91 if let Some(project_json) = find_in_parent_dirs(path, "rust-project.json") { 91 if let Some(project_json) = find_in_parent_dirs(path, "rust-project.json") {
92 return Ok(vec![ProjectRoot::ProjectJson(project_json)]); 92 return Ok(vec![ProjectManifest::ProjectJson(project_json)]);
93 } 93 }
94 return find_cargo_toml(path) 94 return find_cargo_toml(path)
95 .map(|paths| paths.into_iter().map(ProjectRoot::CargoToml).collect()); 95 .map(|paths| paths.into_iter().map(ProjectManifest::CargoToml).collect());
96 96
97 fn find_cargo_toml(path: &Path) -> io::Result<Vec<PathBuf>> { 97 fn find_cargo_toml(path: &Path) -> io::Result<Vec<PathBuf>> {
98 match find_in_parent_dirs(path, "Cargo.toml") { 98 match find_in_parent_dirs(path, "Cargo.toml") {
@@ -128,16 +128,28 @@ impl ProjectRoot {
128 .collect() 128 .collect()
129 } 129 }
130 } 130 }
131
132 pub fn discover_all(paths: &[impl AsRef<Path>]) -> Vec<ProjectManifest> {
133 let mut res = paths
134 .iter()
135 .filter_map(|it| ProjectManifest::discover(it.as_ref()).ok())
136 .flatten()
137 .collect::<FxHashSet<_>>()
138 .into_iter()
139 .collect::<Vec<_>>();
140 res.sort();
141 res
142 }
131} 143}
132 144
133impl ProjectWorkspace { 145impl ProjectWorkspace {
134 pub fn load( 146 pub fn load(
135 root: ProjectRoot, 147 root: ProjectManifest,
136 cargo_features: &CargoConfig, 148 cargo_features: &CargoConfig,
137 with_sysroot: bool, 149 with_sysroot: bool,
138 ) -> Result<ProjectWorkspace> { 150 ) -> Result<ProjectWorkspace> {
139 let res = match root { 151 let res = match root {
140 ProjectRoot::ProjectJson(project_json) => { 152 ProjectManifest::ProjectJson(project_json) => {
141 let file = File::open(&project_json).with_context(|| { 153 let file = File::open(&project_json).with_context(|| {
142 format!("Failed to open json file {}", project_json.display()) 154 format!("Failed to open json file {}", project_json.display())
143 })?; 155 })?;
@@ -148,7 +160,7 @@ impl ProjectWorkspace {
148 })?, 160 })?,
149 } 161 }
150 } 162 }
151 ProjectRoot::CargoToml(cargo_toml) => { 163 ProjectManifest::CargoToml(cargo_toml) => {
152 let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, cargo_features) 164 let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, cargo_features)
153 .with_context(|| { 165 .with_context(|| {
154 format!( 166 format!(