@@ -143,40 +143,6 @@ impl CommandCargoExt for process::Command {
143
143
}
144
144
}
145
145
146
- #[ derive( Deserialize ) ]
147
- struct MessageTarget < ' a > {
148
- #[ serde( borrow) ]
149
- crate_types : Vec < & ' a str > ,
150
- #[ serde( borrow) ]
151
- kind : Vec < & ' a str > ,
152
- }
153
-
154
- #[ derive( Deserialize ) ]
155
- struct MessageFilter < ' a > {
156
- #[ serde( borrow) ]
157
- reason : & ' a str ,
158
- target : MessageTarget < ' a > ,
159
- filenames : Vec < path:: PathBuf > ,
160
- }
161
-
162
- fn extract_filenames ( msg : & escargot:: Message , kind : & str ) -> Option < path:: PathBuf > {
163
- let filter: MessageFilter = msg. convert ( ) . ok ( ) ?;
164
- if filter. reason != "compiler-artifact"
165
- || filter. target . crate_types != [ "bin" ]
166
- || filter. target . kind != [ kind]
167
- {
168
- None
169
- } else {
170
- Some (
171
- filter
172
- . filenames
173
- . into_iter ( )
174
- . next ( )
175
- . expect ( "files must exist" ) ,
176
- )
177
- }
178
- }
179
-
180
146
/// Get the path to the crate's main binary.
181
147
///
182
148
/// Intended for caching the location, reducing the cargo overhead.
@@ -195,21 +161,12 @@ fn extract_filenames(msg: &escargot::Message, kind: &str) -> Option<path::PathBu
195
161
/// .unwrap();
196
162
/// ```
197
163
pub fn main_binary_path ( ) -> Result < path:: PathBuf , CargoError > {
198
- let cargo = escargot:: Cargo :: new ( ) . build ( ) . current_release ( ) ;
199
- let bins: Vec < _ > = cargo
200
- . exec ( )
201
- . map_err ( CargoError :: with_cause) ?
202
- . filter_map ( |m| extract_filenames ( & m, "bin" ) )
203
- . collect ( ) ;
204
- if bins. is_empty ( ) {
205
- return Err ( CargoError :: with_context ( "No binaries in crate" ) ) ;
206
- } else if bins. len ( ) != 1 {
207
- return Err ( CargoError :: with_context ( format ! (
208
- "Ambiguous which binary is intended: {:?}" ,
209
- bins
210
- ) ) ) ;
211
- }
212
- Ok ( bins. into_iter ( ) . next ( ) . expect ( "already validated" ) )
164
+ let runner = escargot:: Cargo :: new ( )
165
+ . build ( )
166
+ . current_release ( )
167
+ . run ( )
168
+ . map_err ( CargoError :: with_cause) ?;
169
+ Ok ( runner. path ( ) . to_owned ( ) )
213
170
}
214
171
215
172
/// Get the path to the specified binary of the current crate.
@@ -228,14 +185,13 @@ pub fn main_binary_path() -> Result<path::PathBuf, CargoError> {
228
185
/// .unwrap();
229
186
/// ```
230
187
pub fn cargo_bin_path < S : AsRef < ffi:: OsStr > > ( name : S ) -> Result < path:: PathBuf , CargoError > {
231
- let cargo = escargot:: Cargo :: new ( ) . build ( ) . bin ( name) . current_release ( ) ;
232
- let bins: Vec < _ > = cargo
233
- . exec ( )
234
- . map_err ( CargoError :: with_cause) ?
235
- . filter_map ( |m| extract_filenames ( & m, "bin" ) )
236
- . collect ( ) ;
237
- assert_eq ! ( bins. len( ) , 1 ) ;
238
- Ok ( bins. into_iter ( ) . next ( ) . expect ( "already validated" ) )
188
+ let runner = escargot:: Cargo :: new ( )
189
+ . build ( )
190
+ . bin ( name)
191
+ . current_release ( )
192
+ . run ( )
193
+ . map_err ( CargoError :: with_cause) ?;
194
+ Ok ( runner. path ( ) . to_owned ( ) )
239
195
}
240
196
241
197
/// Get the path to the specified example of the current crate.
@@ -254,47 +210,28 @@ pub fn cargo_bin_path<S: AsRef<ffi::OsStr>>(name: S) -> Result<path::PathBuf, Ca
254
210
/// .unwrap();
255
211
/// ```
256
212
pub fn cargo_example_path < S : AsRef < ffi:: OsStr > > ( name : S ) -> Result < path:: PathBuf , CargoError > {
257
- let cargo = escargot:: Cargo :: new ( )
213
+ let runner = escargot:: Cargo :: new ( )
258
214
. build ( )
259
215
. example ( name)
260
- . current_release ( ) ;
261
- let bins: Vec < _ > = cargo
262
- . exec ( )
263
- . map_err ( CargoError :: with_cause) ?
264
- . filter_map ( |m| extract_filenames ( & m, "example" ) )
265
- . collect ( ) ;
266
- assert_eq ! ( bins. len( ) , 1 ) ;
267
- Ok ( bins. into_iter ( ) . next ( ) . expect ( "already validated" ) )
216
+ . current_release ( )
217
+ . run ( )
218
+ . map_err ( CargoError :: with_cause) ?;
219
+ Ok ( runner. path ( ) . to_owned ( ) )
268
220
}
269
221
270
222
/// Error when finding crate binary.
271
223
#[ derive( Debug ) ]
272
224
pub struct CargoError {
273
- context : Option < String > ,
274
225
cause : Option < Box < Error + Send + Sync + ' static > > ,
275
226
}
276
227
277
228
impl CargoError {
278
- fn with_context < S > ( context : S ) -> Self
279
- where
280
- S : Into < String > ,
281
- {
282
- let context = context. into ( ) ;
283
- Self {
284
- context : Some ( context) ,
285
- cause : None ,
286
- }
287
- }
288
-
289
229
fn with_cause < E > ( cause : E ) -> Self
290
230
where
291
231
E : Error + Send + Sync + ' static ,
292
232
{
293
233
let cause = Box :: new ( cause) ;
294
- Self {
295
- context : None ,
296
- cause : Some ( cause) ,
297
- }
234
+ Self { cause : Some ( cause) }
298
235
}
299
236
}
300
237
@@ -313,9 +250,6 @@ impl Error for CargoError {
313
250
314
251
impl fmt:: Display for CargoError {
315
252
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
316
- if let Some ( ref context) = self . context {
317
- writeln ! ( f, "{}" , context) ?;
318
- }
319
253
if let Some ( ref cause) = self . cause {
320
254
writeln ! ( f, "Cause: {}" , cause) ?;
321
255
}
0 commit comments