diff options
author | Aleksey Kladov <[email protected]> | 2020-06-03 09:28:15 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-06-03 09:28:15 +0100 |
commit | ac4782ef11706c39117286ddffc3e44fb7e61888 (patch) | |
tree | a3435fe6564cc7a33f38335e1b339ca66c1a6fc9 /crates/ra_project_model/src/json_project.rs | |
parent | c29175f1ff115bb21f5fec8d98f39930615dfcbf (diff) | |
parent | beb79ed104c686d8704eb7042318eefea78770df (diff) |
Merge pull request #4382 from woody77/json_cfgs
Begin transition to new fields for JsonProject crate cfgs
Diffstat (limited to 'crates/ra_project_model/src/json_project.rs')
-rw-r--r-- | crates/ra_project_model/src/json_project.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/crates/ra_project_model/src/json_project.rs b/crates/ra_project_model/src/json_project.rs index b030c8a6a..bd2bae15e 100644 --- a/crates/ra_project_model/src/json_project.rs +++ b/crates/ra_project_model/src/json_project.rs | |||
@@ -20,8 +20,17 @@ pub struct Crate { | |||
20 | pub(crate) root_module: PathBuf, | 20 | pub(crate) root_module: PathBuf, |
21 | pub(crate) edition: Edition, | 21 | pub(crate) edition: Edition, |
22 | pub(crate) deps: Vec<Dep>, | 22 | 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)] | ||
23 | pub(crate) atom_cfgs: FxHashSet<String>, | 30 | pub(crate) atom_cfgs: FxHashSet<String>, |
31 | #[serde(default)] | ||
24 | pub(crate) key_value_cfgs: FxHashMap<String, String>, | 32 | pub(crate) key_value_cfgs: FxHashMap<String, String>, |
33 | |||
25 | pub(crate) out_dir: Option<PathBuf>, | 34 | pub(crate) out_dir: Option<PathBuf>, |
26 | pub(crate) proc_macro_dylib_path: Option<PathBuf>, | 35 | pub(crate) proc_macro_dylib_path: Option<PathBuf>, |
27 | } | 36 | } |
@@ -54,3 +63,73 @@ pub struct JsonProject { | |||
54 | pub(crate) roots: Vec<Root>, | 63 | pub(crate) roots: Vec<Root>, |
55 | pub(crate) crates: Vec<Crate>, | 64 | pub(crate) crates: Vec<Crate>, |
56 | } | 65 | } |
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 | } | ||