@@ -56,7 +56,7 @@ use rustc_hash::FxHashMap;
56
56
use serde:: { de, Deserialize } ;
57
57
use std:: path:: PathBuf ;
58
58
59
- use crate :: cfg_flag:: CfgFlag ;
59
+ use crate :: { cfg_flag:: CfgFlag , TargetKind } ;
60
60
61
61
/// Roots and crates that compose this Rust project.
62
62
#[ derive( Clone , Debug , Eq , PartialEq ) ]
@@ -73,20 +73,37 @@ pub struct ProjectJson {
73
73
/// useful in creating the crate graph.
74
74
#[ derive( Clone , Debug , Eq , PartialEq ) ]
75
75
pub struct Crate {
76
- pub ( crate ) display_name : Option < CrateDisplayName > ,
77
- pub ( crate ) root_module : AbsPathBuf ,
78
- pub ( crate ) edition : Edition ,
79
- pub ( crate ) version : Option < String > ,
80
- pub ( crate ) deps : Vec < Dependency > ,
81
- pub ( crate ) cfg : Vec < CfgFlag > ,
82
- pub ( crate ) target : Option < String > ,
83
- pub ( crate ) env : FxHashMap < String , String > ,
84
- pub ( crate ) proc_macro_dylib_path : Option < AbsPathBuf > ,
85
- pub ( crate ) is_workspace_member : bool ,
86
- pub ( crate ) include : Vec < AbsPathBuf > ,
87
- pub ( crate ) exclude : Vec < AbsPathBuf > ,
88
- pub ( crate ) is_proc_macro : bool ,
89
- pub ( crate ) repository : Option < String > ,
76
+ pub display_name : Option < CrateDisplayName > ,
77
+ pub root_module : AbsPathBuf ,
78
+ pub edition : Edition ,
79
+ pub version : Option < String > ,
80
+ pub deps : Vec < Dependency > ,
81
+ pub cfg : Vec < CfgFlag > ,
82
+ pub target : Option < String > ,
83
+ pub env : FxHashMap < String , String > ,
84
+ pub proc_macro_dylib_path : Option < AbsPathBuf > ,
85
+ pub is_workspace_member : bool ,
86
+ pub include : Vec < AbsPathBuf > ,
87
+ pub exclude : Vec < AbsPathBuf > ,
88
+ pub is_proc_macro : bool ,
89
+ pub repository : Option < String > ,
90
+ pub target_spec : Option < TargetSpec > ,
91
+ }
92
+
93
+ #[ derive( Clone , Debug , Eq , PartialEq ) ]
94
+ pub struct TargetSpec {
95
+ pub manifest_file : AbsPathBuf ,
96
+ pub target_label : String ,
97
+ pub target_kind : TargetKind ,
98
+ pub runnables : Runnables ,
99
+ pub flycheck_command : Vec < String > ,
100
+ }
101
+
102
+ #[ derive( Clone , Debug , Eq , PartialEq ) ]
103
+ pub struct Runnables {
104
+ pub check : Vec < String > ,
105
+ pub run : Vec < String > ,
106
+ pub test : Vec < String > ,
90
107
}
91
108
92
109
impl ProjectJson {
@@ -121,6 +138,20 @@ impl ProjectJson {
121
138
None => ( vec ! [ root_module. parent( ) . unwrap( ) . to_path_buf( ) ] , Vec :: new ( ) ) ,
122
139
} ;
123
140
141
+ let target_spec = match crate_data. target_spec {
142
+ Some ( spec) => {
143
+ let spec = TargetSpec {
144
+ manifest_file : absolutize_on_base ( spec. manifest_file ) ,
145
+ target_label : spec. target_label ,
146
+ target_kind : spec. target_kind . into ( ) ,
147
+ runnables : spec. runnables . into ( ) ,
148
+ flycheck_command : spec. flycheck_command ,
149
+ } ;
150
+ Some ( spec)
151
+ }
152
+ None => None ,
153
+ } ;
154
+
124
155
Crate {
125
156
display_name : crate_data
126
157
. display_name
@@ -149,6 +180,7 @@ impl ProjectJson {
149
180
exclude,
150
181
is_proc_macro : crate_data. is_proc_macro ,
151
182
repository : crate_data. repository ,
183
+ target_spec,
152
184
}
153
185
} )
154
186
. collect ( ) ,
@@ -172,6 +204,14 @@ impl ProjectJson {
172
204
pub fn path ( & self ) -> & AbsPath {
173
205
& self . project_root
174
206
}
207
+
208
+ pub fn crate_by_root ( & self , root : & AbsPath ) -> Option < Crate > {
209
+ self . crates
210
+ . iter ( )
211
+ . filter ( |krate| krate. is_workspace_member )
212
+ . find ( |krate| & krate. root_module == root)
213
+ . cloned ( )
214
+ }
175
215
}
176
216
177
217
#[ derive( Deserialize , Debug , Clone ) ]
@@ -201,6 +241,8 @@ struct CrateData {
201
241
is_proc_macro : bool ,
202
242
#[ serde( default ) ]
203
243
repository : Option < String > ,
244
+ #[ serde( default ) ]
245
+ target_spec : Option < TargetSpecData > ,
204
246
}
205
247
206
248
#[ derive( Deserialize , Debug , Clone ) ]
@@ -216,6 +258,55 @@ enum EditionData {
216
258
Edition2024 ,
217
259
}
218
260
261
+ #[ derive( Deserialize , Debug , Clone ) ]
262
+ pub struct TargetSpecData {
263
+ manifest_file : PathBuf ,
264
+ target_label : String ,
265
+ target_kind : TargetKindData ,
266
+ runnables : RunnablesData ,
267
+ flycheck_command : Vec < String > ,
268
+ }
269
+
270
+ #[ derive( Deserialize , Debug , Clone ) ]
271
+ pub struct RunnablesData {
272
+ check : Vec < String > ,
273
+ run : Vec < String > ,
274
+ test : Vec < String > ,
275
+ }
276
+
277
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , serde:: Deserialize ) ]
278
+ #[ serde( rename_all = "camelCase" ) ]
279
+ pub enum TargetKindData {
280
+ Bin ,
281
+ /// Any kind of Cargo lib crate-type (dylib, rlib, proc-macro, ...).
282
+ Lib ,
283
+ Example ,
284
+ Test ,
285
+ Bench ,
286
+ BuildScript ,
287
+ Other ,
288
+ }
289
+
290
+ impl From < TargetKindData > for TargetKind {
291
+ fn from ( value : TargetKindData ) -> Self {
292
+ match value {
293
+ TargetKindData :: Bin => TargetKind :: Bin ,
294
+ TargetKindData :: Lib => TargetKind :: Lib { is_proc_macro : false } ,
295
+ TargetKindData :: Example => TargetKind :: Example ,
296
+ TargetKindData :: Test => TargetKind :: Test ,
297
+ TargetKindData :: Bench => TargetKind :: Bench ,
298
+ TargetKindData :: BuildScript => TargetKind :: BuildScript ,
299
+ TargetKindData :: Other => TargetKind :: Other ,
300
+ }
301
+ }
302
+ }
303
+
304
+ impl From < RunnablesData > for Runnables {
305
+ fn from ( value : RunnablesData ) -> Self {
306
+ Runnables { check : value. check , run : value. run , test : value. test }
307
+ }
308
+ }
309
+
219
310
impl From < EditionData > for Edition {
220
311
fn from ( data : EditionData ) -> Self {
221
312
match data {
0 commit comments