diff options
Diffstat (limited to 'crates/ra_project_model/src/project_json.rs')
-rw-r--r-- | crates/ra_project_model/src/project_json.rs | 129 |
1 files changed, 129 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..b96227949 --- /dev/null +++ b/crates/ra_project_model/src/project_json.rs | |||
@@ -0,0 +1,129 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use std::path::PathBuf; | ||
4 | |||
5 | use paths::{AbsPath, AbsPathBuf}; | ||
6 | use ra_cfg::CfgOptions; | ||
7 | use ra_db::{CrateId, CrateName, Dependency, Edition}; | ||
8 | use rustc_hash::FxHashSet; | ||
9 | use serde::{de, Deserialize}; | ||
10 | use stdx::split_delim; | ||
11 | |||
12 | /// Roots and crates that compose this Rust project. | ||
13 | #[derive(Clone, Debug, Eq, PartialEq)] | ||
14 | pub struct ProjectJson { | ||
15 | pub(crate) roots: Vec<Root>, | ||
16 | pub(crate) crates: Vec<Crate>, | ||
17 | } | ||
18 | |||
19 | /// A root points to the directory which contains Rust crates. rust-analyzer watches all files in | ||
20 | /// all roots. Roots might be nested. | ||
21 | #[derive(Clone, Debug, Eq, PartialEq)] | ||
22 | pub struct Root { | ||
23 | pub(crate) path: AbsPathBuf, | ||
24 | } | ||
25 | |||
26 | /// A crate points to the root module of a crate and lists the dependencies of the crate. This is | ||
27 | /// useful in creating the crate graph. | ||
28 | #[derive(Clone, Debug, Eq, PartialEq)] | ||
29 | pub struct Crate { | ||
30 | pub(crate) root_module: AbsPathBuf, | ||
31 | pub(crate) edition: Edition, | ||
32 | pub(crate) deps: Vec<Dependency>, | ||
33 | pub(crate) cfg: CfgOptions, | ||
34 | pub(crate) target: Option<String>, | ||
35 | pub(crate) out_dir: Option<AbsPathBuf>, | ||
36 | pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>, | ||
37 | } | ||
38 | |||
39 | impl ProjectJson { | ||
40 | pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson { | ||
41 | ProjectJson { | ||
42 | roots: data.roots.into_iter().map(|path| Root { path: base.join(path) }).collect(), | ||
43 | crates: data | ||
44 | .crates | ||
45 | .into_iter() | ||
46 | .map(|crate_data| Crate { | ||
47 | root_module: base.join(crate_data.root_module), | ||
48 | edition: crate_data.edition.into(), | ||
49 | deps: crate_data | ||
50 | .deps | ||
51 | .into_iter() | ||
52 | .map(|dep_data| Dependency { | ||
53 | crate_id: CrateId(dep_data.krate as u32), | ||
54 | name: dep_data.name, | ||
55 | }) | ||
56 | .collect::<Vec<_>>(), | ||
57 | cfg: { | ||
58 | let mut cfg = CfgOptions::default(); | ||
59 | for entry in &crate_data.cfg { | ||
60 | match split_delim(entry, '=') { | ||
61 | Some((key, value)) => { | ||
62 | cfg.insert_key_value(key.into(), value.into()); | ||
63 | } | ||
64 | None => cfg.insert_atom(entry.into()), | ||
65 | } | ||
66 | } | ||
67 | cfg | ||
68 | }, | ||
69 | target: crate_data.target, | ||
70 | out_dir: crate_data.out_dir.map(|it| base.join(it)), | ||
71 | proc_macro_dylib_path: crate_data.proc_macro_dylib_path.map(|it| base.join(it)), | ||
72 | }) | ||
73 | .collect::<Vec<_>>(), | ||
74 | } | ||
75 | } | ||
76 | } | ||
77 | |||
78 | #[derive(Deserialize)] | ||
79 | pub struct ProjectJsonData { | ||
80 | roots: Vec<PathBuf>, | ||
81 | crates: Vec<CrateData>, | ||
82 | } | ||
83 | |||
84 | #[derive(Deserialize)] | ||
85 | struct CrateData { | ||
86 | root_module: PathBuf, | ||
87 | edition: EditionData, | ||
88 | deps: Vec<DepData>, | ||
89 | #[serde(default)] | ||
90 | cfg: FxHashSet<String>, | ||
91 | target: Option<String>, | ||
92 | out_dir: Option<PathBuf>, | ||
93 | proc_macro_dylib_path: Option<PathBuf>, | ||
94 | } | ||
95 | |||
96 | #[derive(Deserialize)] | ||
97 | #[serde(rename = "edition")] | ||
98 | enum EditionData { | ||
99 | #[serde(rename = "2015")] | ||
100 | Edition2015, | ||
101 | #[serde(rename = "2018")] | ||
102 | Edition2018, | ||
103 | } | ||
104 | |||
105 | impl From<EditionData> for Edition { | ||
106 | fn from(data: EditionData) -> Self { | ||
107 | match data { | ||
108 | EditionData::Edition2015 => Edition::Edition2015, | ||
109 | EditionData::Edition2018 => Edition::Edition2018, | ||
110 | } | ||
111 | } | ||
112 | } | ||
113 | |||
114 | #[derive(Deserialize)] | ||
115 | struct DepData { | ||
116 | /// Identifies a crate by position in the crates array. | ||
117 | #[serde(rename = "crate")] | ||
118 | krate: usize, | ||
119 | #[serde(deserialize_with = "deserialize_crate_name")] | ||
120 | name: CrateName, | ||
121 | } | ||
122 | |||
123 | fn deserialize_crate_name<'de, D>(de: D) -> Result<CrateName, D::Error> | ||
124 | where | ||
125 | D: de::Deserializer<'de>, | ||
126 | { | ||
127 | let name = String::deserialize(de)?; | ||
128 | CrateName::new(&name).map_err(|err| de::Error::custom(format!("invalid crate name: {:?}", err))) | ||
129 | } | ||