1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
//! `rust-project.json` file format.
//!
//! This format is spiritually a serialization of [`base_db::CrateGraph`]. The
//! idea here is that people who do not use Cargo, can instead teach their build
//! system to generate `rust-project.json` which can be ingested by
//! rust-analyzer.
use std::path::PathBuf;
use base_db::{CrateDisplayName, CrateId, CrateName, Dependency, Edition};
use paths::{AbsPath, AbsPathBuf};
use rustc_hash::FxHashMap;
use serde::{de, Deserialize};
use crate::cfg_flag::CfgFlag;
/// Roots and crates that compose this Rust project.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProjectJson {
pub(crate) sysroot_src: Option<AbsPathBuf>,
project_root: AbsPathBuf,
crates: Vec<Crate>,
}
/// A crate points to the root module of a crate and lists the dependencies of the crate. This is
/// useful in creating the crate graph.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Crate {
pub(crate) display_name: Option<CrateDisplayName>,
pub(crate) root_module: AbsPathBuf,
pub(crate) edition: Edition,
pub(crate) deps: Vec<Dependency>,
pub(crate) cfg: Vec<CfgFlag>,
pub(crate) target: Option<String>,
pub(crate) env: FxHashMap<String, String>,
pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>,
pub(crate) is_workspace_member: bool,
pub(crate) include: Vec<AbsPathBuf>,
pub(crate) exclude: Vec<AbsPathBuf>,
}
impl ProjectJson {
/// Create a new ProjectJson instance.
///
/// # Arguments
///
/// * `base` - The path to the workspace root (i.e. the folder containing `rust-project.json`)
/// * `data` - The parsed contents of `rust-project.json`, or project json that's passed via
/// configuration.
pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
ProjectJson {
sysroot_src: data.sysroot_src.map(|it| base.join(it)),
project_root: base.to_path_buf(),
crates: data
.crates
.into_iter()
.map(|crate_data| {
let is_workspace_member = crate_data.is_workspace_member.unwrap_or_else(|| {
crate_data.root_module.is_relative()
&& !crate_data.root_module.starts_with("..")
|| crate_data.root_module.starts_with(base)
});
let root_module = base.join(crate_data.root_module).normalize();
let (include, exclude) = match crate_data.source {
Some(src) => {
let absolutize = |dirs: Vec<PathBuf>| {
dirs.into_iter()
.map(|it| base.join(it).normalize())
.collect::<Vec<_>>()
};
(absolutize(src.include_dirs), absolutize(src.exclude_dirs))
}
None => (vec![root_module.parent().unwrap().to_path_buf()], Vec::new()),
};
Crate {
display_name: crate_data
.display_name
.map(CrateDisplayName::from_canonical_name),
root_module,
edition: crate_data.edition.into(),
deps: crate_data
.deps
.into_iter()
.map(|dep_data| Dependency {
crate_id: CrateId(dep_data.krate as u32),
name: dep_data.name,
})
.collect::<Vec<_>>(),
cfg: crate_data.cfg,
target: crate_data.target,
env: crate_data.env,
proc_macro_dylib_path: crate_data
.proc_macro_dylib_path
.map(|it| base.join(it)),
is_workspace_member,
include,
exclude,
}
})
.collect::<Vec<_>>(),
}
}
/// Returns the number of crates in the project.
pub fn n_crates(&self) -> usize {
self.crates.len()
}
/// 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(idx as u32), krate))
}
/// Returns the path to the project's root folder.
pub fn path(&self) -> &AbsPath {
&self.project_root
}
}
#[derive(Deserialize, Debug, Clone)]
pub struct ProjectJsonData {
sysroot_src: Option<PathBuf>,
crates: Vec<CrateData>,
}
#[derive(Deserialize, Debug, Clone)]
struct CrateData {
display_name: Option<String>,
root_module: PathBuf,
edition: EditionData,
deps: Vec<DepData>,
#[serde(default)]
cfg: Vec<CfgFlag>,
target: Option<String>,
#[serde(default)]
env: FxHashMap<String, String>,
proc_macro_dylib_path: Option<PathBuf>,
is_workspace_member: Option<bool>,
source: Option<CrateSource>,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename = "edition")]
enum EditionData {
#[serde(rename = "2015")]
Edition2015,
#[serde(rename = "2018")]
Edition2018,
#[serde(rename = "2021")]
Edition2021,
}
impl From<EditionData> for Edition {
fn from(data: EditionData) -> Self {
match data {
EditionData::Edition2015 => Edition::Edition2015,
EditionData::Edition2018 => Edition::Edition2018,
EditionData::Edition2021 => Edition::Edition2021,
}
}
}
#[derive(Deserialize, Debug, Clone)]
struct DepData {
/// Identifies a crate by position in the crates array.
#[serde(rename = "crate")]
krate: usize,
#[serde(deserialize_with = "deserialize_crate_name")]
name: CrateName,
}
#[derive(Deserialize, Debug, Clone)]
struct CrateSource {
include_dirs: Vec<PathBuf>,
exclude_dirs: Vec<PathBuf>,
}
fn deserialize_crate_name<'de, D>(de: D) -> Result<CrateName, D::Error>
where
D: de::Deserializer<'de>,
{
let name = String::deserialize(de)?;
CrateName::new(&name).map_err(|err| de::Error::custom(format!("invalid crate name: {:?}", err)))
}
|