2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-03-05 15:29:23 -06:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-06-24 08:52:07 -05:00
|
|
|
use paths::{AbsPath, AbsPathBuf};
|
|
|
|
use ra_cfg::CfgOptions;
|
2020-07-01 02:53:53 -05:00
|
|
|
use ra_db::{CrateId, CrateName, Dependency, Edition};
|
2020-06-03 16:54:05 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
2020-07-01 02:53:53 -05:00
|
|
|
use serde::{de, Deserialize};
|
2020-06-24 08:52:07 -05:00
|
|
|
use stdx::split_delim;
|
2019-03-05 15:29:23 -06:00
|
|
|
|
2020-06-03 03:33:01 -05:00
|
|
|
/// Roots and crates that compose this Rust project.
|
2020-07-01 09:42:14 -05:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2020-06-24 07:57:37 -05:00
|
|
|
pub struct ProjectJson {
|
2020-06-03 03:33:01 -05:00
|
|
|
pub(crate) crates: Vec<Crate>,
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:29:23 -06:00
|
|
|
/// 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.
|
2020-07-01 09:42:14 -05:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2019-03-05 15:29:23 -06:00
|
|
|
pub struct Crate {
|
2020-06-24 08:52:07 -05:00
|
|
|
pub(crate) root_module: AbsPathBuf,
|
2019-03-05 15:29:23 -06:00
|
|
|
pub(crate) edition: Edition,
|
2020-06-24 08:52:07 -05:00
|
|
|
pub(crate) deps: Vec<Dependency>,
|
|
|
|
pub(crate) cfg: CfgOptions,
|
2020-06-12 12:08:51 -05:00
|
|
|
pub(crate) target: Option<String>,
|
2020-06-24 08:52:07 -05:00
|
|
|
pub(crate) out_dir: Option<AbsPathBuf>,
|
|
|
|
pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>,
|
2020-07-20 10:57:10 -05:00
|
|
|
pub(crate) is_workspace_member: bool,
|
2020-07-21 07:57:20 -05:00
|
|
|
pub(crate) include: Vec<AbsPathBuf>,
|
|
|
|
pub(crate) exclude: Vec<AbsPathBuf>,
|
2020-06-24 08:52:07 -05:00
|
|
|
}
|
2020-05-08 18:59:52 -05:00
|
|
|
|
2020-06-24 08:52:07 -05:00
|
|
|
impl ProjectJson {
|
|
|
|
pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
|
|
|
|
ProjectJson {
|
|
|
|
crates: data
|
|
|
|
.crates
|
|
|
|
.into_iter()
|
2020-07-20 10:57:10 -05:00
|
|
|
.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)
|
|
|
|
});
|
2020-07-21 07:57:20 -05:00
|
|
|
let root_module = base.join(crate_data.root_module);
|
|
|
|
let (include, exclude) = match crate_data.source {
|
|
|
|
Some(src) => {
|
|
|
|
let absolutize = |dirs: Vec<PathBuf>| {
|
|
|
|
dirs.into_iter().map(|it| base.join(it)).collect::<Vec<_>>()
|
|
|
|
};
|
|
|
|
(absolutize(src.include_dirs), absolutize(src.exclude_dirs))
|
|
|
|
}
|
|
|
|
None => (vec![root_module.parent().unwrap().to_path_buf()], Vec::new()),
|
|
|
|
};
|
|
|
|
|
2020-07-20 10:57:10 -05:00
|
|
|
Crate {
|
2020-07-21 07:57:20 -05:00
|
|
|
root_module,
|
2020-07-20 10:57:10 -05:00
|
|
|
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: {
|
|
|
|
let mut cfg = CfgOptions::default();
|
|
|
|
for entry in &crate_data.cfg {
|
|
|
|
match split_delim(entry, '=') {
|
|
|
|
Some((key, value)) => {
|
|
|
|
cfg.insert_key_value(key.into(), value.into());
|
|
|
|
}
|
|
|
|
None => cfg.insert_atom(entry.into()),
|
2020-06-24 08:52:07 -05:00
|
|
|
}
|
|
|
|
}
|
2020-07-20 10:57:10 -05:00
|
|
|
cfg
|
|
|
|
},
|
|
|
|
target: crate_data.target,
|
|
|
|
out_dir: crate_data.out_dir.map(|it| base.join(it)),
|
|
|
|
proc_macro_dylib_path: crate_data
|
|
|
|
.proc_macro_dylib_path
|
|
|
|
.map(|it| base.join(it)),
|
|
|
|
is_workspace_member,
|
2020-07-21 07:57:20 -05:00
|
|
|
include,
|
|
|
|
exclude,
|
2020-07-20 10:57:10 -05:00
|
|
|
}
|
2020-06-24 08:52:07 -05:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 18:59:52 -05:00
|
|
|
|
2020-06-24 08:52:07 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct ProjectJsonData {
|
|
|
|
crates: Vec<CrateData>,
|
2019-03-05 15:29:23 -06:00
|
|
|
}
|
|
|
|
|
2020-06-24 08:52:07 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct CrateData {
|
|
|
|
root_module: PathBuf,
|
|
|
|
edition: EditionData,
|
|
|
|
deps: Vec<DepData>,
|
|
|
|
#[serde(default)]
|
|
|
|
cfg: FxHashSet<String>,
|
2020-06-12 12:08:51 -05:00
|
|
|
target: Option<String>,
|
2020-06-24 08:52:07 -05:00
|
|
|
out_dir: Option<PathBuf>,
|
|
|
|
proc_macro_dylib_path: Option<PathBuf>,
|
2020-07-20 10:57:10 -05:00
|
|
|
is_workspace_member: Option<bool>,
|
2020-07-21 07:57:20 -05:00
|
|
|
source: Option<CrateSource>,
|
2020-06-24 08:52:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2019-03-05 15:29:23 -06:00
|
|
|
#[serde(rename = "edition")]
|
2020-06-24 08:52:07 -05:00
|
|
|
enum EditionData {
|
2019-03-05 15:29:23 -06:00
|
|
|
#[serde(rename = "2015")]
|
|
|
|
Edition2015,
|
|
|
|
#[serde(rename = "2018")]
|
|
|
|
Edition2018,
|
|
|
|
}
|
|
|
|
|
2020-06-24 08:52:07 -05:00
|
|
|
impl From<EditionData> for Edition {
|
|
|
|
fn from(data: EditionData) -> Self {
|
|
|
|
match data {
|
|
|
|
EditionData::Edition2015 => Edition::Edition2015,
|
|
|
|
EditionData::Edition2018 => Edition::Edition2018,
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 15:29:23 -06:00
|
|
|
}
|
|
|
|
|
2020-06-24 08:52:07 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct DepData {
|
|
|
|
/// Identifies a crate by position in the crates array.
|
|
|
|
#[serde(rename = "crate")]
|
|
|
|
krate: usize,
|
2020-07-01 02:53:53 -05:00
|
|
|
#[serde(deserialize_with = "deserialize_crate_name")]
|
|
|
|
name: CrateName,
|
|
|
|
}
|
|
|
|
|
2020-07-21 07:57:20 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct CrateSource {
|
|
|
|
include_dirs: Vec<PathBuf>,
|
|
|
|
exclude_dirs: Vec<PathBuf>,
|
|
|
|
}
|
|
|
|
|
2020-07-01 02:53:53 -05:00
|
|
|
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)))
|
2020-05-08 18:59:52 -05:00
|
|
|
}
|