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-03 16:54:05 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
2019-03-05 15:29:23 -06:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
2020-06-03 03:33:01 -05:00
|
|
|
/// Roots and crates that compose this Rust project.
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
2020-06-24 07:57:37 -05:00
|
|
|
pub struct ProjectJson {
|
2020-06-03 03:33:01 -05:00
|
|
|
pub(crate) roots: Vec<Root>,
|
|
|
|
pub(crate) crates: Vec<Crate>,
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:29:23 -06:00
|
|
|
/// A root points to the directory which contains Rust crates. rust-analyzer watches all files in
|
|
|
|
/// all roots. Roots might be nested.
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
#[serde(transparent)]
|
|
|
|
pub struct Root {
|
|
|
|
pub(crate) path: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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, Deserialize)]
|
|
|
|
pub struct Crate {
|
|
|
|
pub(crate) root_module: PathBuf,
|
|
|
|
pub(crate) edition: Edition,
|
|
|
|
pub(crate) deps: Vec<Dep>,
|
2020-05-08 18:59:52 -05:00
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
pub(crate) cfg: FxHashSet<String>,
|
|
|
|
|
2020-03-16 08:17:32 -05:00
|
|
|
pub(crate) out_dir: Option<PathBuf>,
|
2020-03-18 07:56:46 -05:00
|
|
|
pub(crate) proc_macro_dylib_path: Option<PathBuf>,
|
2019-03-05 15:29:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Deserialize)]
|
|
|
|
#[serde(rename = "edition")]
|
|
|
|
pub enum Edition {
|
|
|
|
#[serde(rename = "2015")]
|
|
|
|
Edition2015,
|
|
|
|
#[serde(rename = "2018")]
|
|
|
|
Edition2018,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Identifies a crate by position in the crates array.
|
|
|
|
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
|
|
|
#[serde(transparent)]
|
|
|
|
pub struct CrateId(pub usize);
|
|
|
|
|
|
|
|
/// A dependency of a crate, identified by its id in the crates array and name.
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
pub struct Dep {
|
|
|
|
#[serde(rename = "crate")]
|
|
|
|
pub(crate) krate: CrateId,
|
|
|
|
pub(crate) name: String,
|
|
|
|
}
|
|
|
|
|
2020-05-08 18:59:52 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_crate_deserialization() {
|
|
|
|
let raw_json = json!( {
|
|
|
|
"crate_id": 2,
|
|
|
|
"root_module": "this/is/a/file/path.rs",
|
|
|
|
"deps": [
|
|
|
|
{
|
|
|
|
"crate": 1,
|
|
|
|
"name": "some_dep_crate"
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"edition": "2015",
|
|
|
|
"cfg": [
|
|
|
|
"atom_1",
|
|
|
|
"atom_2",
|
|
|
|
"feature=feature_1",
|
|
|
|
"feature=feature_2",
|
|
|
|
"other=value",
|
|
|
|
],
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
let krate: Crate = serde_json::from_value(raw_json).unwrap();
|
|
|
|
|
|
|
|
assert!(krate.cfg.contains(&"atom_1".to_string()));
|
|
|
|
assert!(krate.cfg.contains(&"atom_2".to_string()));
|
|
|
|
assert!(krate.cfg.contains(&"feature=feature_1".to_string()));
|
|
|
|
assert!(krate.cfg.contains(&"feature=feature_2".to_string()));
|
|
|
|
assert!(krate.cfg.contains(&"other=value".to_string()));
|
|
|
|
}
|
|
|
|
}
|