aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model/src/project_json.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-06-24 13:57:37 +0100
committerAleksey Kladov <[email protected]>2020-06-24 15:03:24 +0100
commita07cad16ab6271809d30ecf723420b3e41ec42ef (patch)
tree264e725302ec5475e4bf3b967bd72ca1d5d40dbe /crates/ra_project_model/src/project_json.rs
parent97c4d06258bace7c9caf211d3a307fff05bdb58e (diff)
Rename json_project -> project_json
Diffstat (limited to 'crates/ra_project_model/src/project_json.rs')
-rw-r--r--crates/ra_project_model/src/project_json.rs95
1 files changed, 95 insertions, 0 deletions
diff --git a/crates/ra_project_model/src/project_json.rs b/crates/ra_project_model/src/project_json.rs
new file mode 100644
index 000000000..e663bb4d7
--- /dev/null
+++ b/crates/ra_project_model/src/project_json.rs
@@ -0,0 +1,95 @@
1//! FIXME: write short doc here
2
3use std::path::PathBuf;
4
5use rustc_hash::FxHashSet;
6use serde::Deserialize;
7
8/// Roots and crates that compose this Rust project.
9#[derive(Clone, Debug, Deserialize)]
10pub struct ProjectJson {
11 pub(crate) roots: Vec<Root>,
12 pub(crate) crates: Vec<Crate>,
13}
14
15/// A root points to the directory which contains Rust crates. rust-analyzer watches all files in
16/// all roots. Roots might be nested.
17#[derive(Clone, Debug, Deserialize)]
18#[serde(transparent)]
19pub struct Root {
20 pub(crate) path: PathBuf,
21}
22
23/// A crate points to the root module of a crate and lists the dependencies of the crate. This is
24/// useful in creating the crate graph.
25#[derive(Clone, Debug, Deserialize)]
26pub struct Crate {
27 pub(crate) root_module: PathBuf,
28 pub(crate) edition: Edition,
29 pub(crate) deps: Vec<Dep>,
30
31 #[serde(default)]
32 pub(crate) cfg: FxHashSet<String>,
33
34 pub(crate) out_dir: Option<PathBuf>,
35 pub(crate) proc_macro_dylib_path: Option<PathBuf>,
36}
37
38#[derive(Clone, Copy, Debug, Deserialize)]
39#[serde(rename = "edition")]
40pub enum Edition {
41 #[serde(rename = "2015")]
42 Edition2015,
43 #[serde(rename = "2018")]
44 Edition2018,
45}
46
47/// Identifies a crate by position in the crates array.
48#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd)]
49#[serde(transparent)]
50pub struct CrateId(pub usize);
51
52/// A dependency of a crate, identified by its id in the crates array and name.
53#[derive(Clone, Debug, Deserialize)]
54pub struct Dep {
55 #[serde(rename = "crate")]
56 pub(crate) krate: CrateId,
57 pub(crate) name: String,
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63 use serde_json::json;
64
65 #[test]
66 fn test_crate_deserialization() {
67 let raw_json = json!( {
68 "crate_id": 2,
69 "root_module": "this/is/a/file/path.rs",
70 "deps": [
71 {
72 "crate": 1,
73 "name": "some_dep_crate"
74 },
75 ],
76 "edition": "2015",
77 "cfg": [
78 "atom_1",
79 "atom_2",
80 "feature=feature_1",
81 "feature=feature_2",
82 "other=value",
83 ],
84
85 });
86
87 let krate: Crate = serde_json::from_value(raw_json).unwrap();
88
89 assert!(krate.cfg.contains(&"atom_1".to_string()));
90 assert!(krate.cfg.contains(&"atom_2".to_string()));
91 assert!(krate.cfg.contains(&"feature=feature_1".to_string()));
92 assert!(krate.cfg.contains(&"feature=feature_2".to_string()));
93 assert!(krate.cfg.contains(&"other=value".to_string()));
94 }
95}