Skip to content

Commit beb79ed

Browse files
committed
Begin transition to new fields for JsonProject crate cfgs
This starts the transition to a new method of documenting the cfgs that are enabled for a given crate in the json file. This is changing from a list of atoms and a dict of key:value pairs, to a list of strings that is equivalent to that returned by `rustc --print cfg ..`, and parsed in the same manner by rust-analyzer. This is the first of two changes, which adds the new field that contains the list of strings. Next change will complete the transition and remove the previous fields.
1 parent a5f2b16 commit beb79ed

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

crates/ra_project_model/src/json_project.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@ pub struct Crate {
2020
pub(crate) root_module: PathBuf,
2121
pub(crate) edition: Edition,
2222
pub(crate) deps: Vec<Dep>,
23+
24+
// This is the preferred method of providing cfg options.
25+
#[serde(default)]
26+
pub(crate) cfg: FxHashSet<String>,
27+
28+
// These two are here for transition only.
29+
#[serde(default)]
2330
pub(crate) atom_cfgs: FxHashSet<String>,
31+
#[serde(default)]
2432
pub(crate) key_value_cfgs: FxHashMap<String, String>,
33+
2534
pub(crate) out_dir: Option<PathBuf>,
2635
pub(crate) proc_macro_dylib_path: Option<PathBuf>,
2736
}
@@ -54,3 +63,73 @@ pub struct JsonProject {
5463
pub(crate) roots: Vec<Root>,
5564
pub(crate) crates: Vec<Crate>,
5665
}
66+
67+
#[cfg(test)]
68+
mod tests {
69+
use super::*;
70+
use serde_json::json;
71+
72+
#[test]
73+
fn test_crate_deserialization() {
74+
let raw_json = json!( {
75+
"crate_id": 2,
76+
"root_module": "this/is/a/file/path.rs",
77+
"deps": [
78+
{
79+
"crate": 1,
80+
"name": "some_dep_crate"
81+
},
82+
],
83+
"edition": "2015",
84+
"cfg": [
85+
"atom_1",
86+
"atom_2",
87+
"feature=feature_1",
88+
"feature=feature_2",
89+
"other=value",
90+
],
91+
92+
});
93+
94+
let krate: Crate = serde_json::from_value(raw_json).unwrap();
95+
96+
assert!(krate.cfg.contains(&"atom_1".to_string()));
97+
assert!(krate.cfg.contains(&"atom_2".to_string()));
98+
assert!(krate.cfg.contains(&"feature=feature_1".to_string()));
99+
assert!(krate.cfg.contains(&"feature=feature_2".to_string()));
100+
assert!(krate.cfg.contains(&"other=value".to_string()));
101+
}
102+
103+
#[test]
104+
fn test_crate_deserialization_old_json() {
105+
let raw_json = json!( {
106+
"crate_id": 2,
107+
"root_module": "this/is/a/file/path.rs",
108+
"deps": [
109+
{
110+
"crate": 1,
111+
"name": "some_dep_crate"
112+
},
113+
],
114+
"edition": "2015",
115+
"atom_cfgs": [
116+
"atom_1",
117+
"atom_2",
118+
],
119+
"key_value_cfgs": {
120+
"feature": "feature_1",
121+
"feature": "feature_2",
122+
"other": "value",
123+
},
124+
});
125+
126+
let krate: Crate = serde_json::from_value(raw_json).unwrap();
127+
128+
assert!(krate.atom_cfgs.contains(&"atom_1".to_string()));
129+
assert!(krate.atom_cfgs.contains(&"atom_2".to_string()));
130+
assert!(krate.key_value_cfgs.contains_key(&"feature".to_string()));
131+
assert_eq!(krate.key_value_cfgs.get("feature"), Some(&"feature_2".to_string()));
132+
assert!(krate.key_value_cfgs.contains_key(&"other".to_string()));
133+
assert_eq!(krate.key_value_cfgs.get("other"), Some(&"value".to_string()));
134+
}
135+
}

crates/ra_project_model/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ impl ProjectWorkspace {
273273
};
274274
let cfg_options = {
275275
let mut opts = default_cfg_options.clone();
276+
for cfg in &krate.cfg {
277+
match cfg.find('=') {
278+
None => opts.insert_atom(cfg.into()),
279+
Some(pos) => {
280+
let key = &cfg[..pos];
281+
let value = cfg[pos + 1..].trim_matches('"');
282+
opts.insert_key_value(key.into(), value.into());
283+
}
284+
}
285+
}
276286
for name in &krate.atom_cfgs {
277287
opts.insert_atom(name.into());
278288
}

crates/rust-analyzer/tests/heavy_tests/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,9 @@ fn test_missing_module_code_action_in_json_project() {
384384
"root_module": path.join("src/lib.rs"),
385385
"deps": [],
386386
"edition": "2015",
387-
"atom_cfgs": [],
388-
"key_value_cfgs": {}
387+
"cfg": [ "cfg_atom_1", "feature=cfg_1"],
388+
"atom_cfgs": ["atom_2"],
389+
"key_value_cfgs": { "feature": "key_value_feature", "other": "value"}
389390
} ]
390391
});
391392

0 commit comments

Comments
 (0)