Skip to content

Commit fe0d12c

Browse files
committed
refactor: Use CargoRun
1 parent 59cdbad commit fe0d12c

File tree

3 files changed

+19
-88
lines changed

3 files changed

+19
-88
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ predicates = "0.9.0"
1919
predicates-core = "0.9.0"
2020
predicates-tree = "0.9.0"
2121
escargot = "0.3"
22-
serde = { version = "1.0", features = ["derive"] }
2322

2423
[dev-dependencies]
2524
docmatic = "0.1"

src/cargo.rs

+19-85
Original file line numberDiff line numberDiff line change
@@ -143,40 +143,6 @@ impl CommandCargoExt for process::Command {
143143
}
144144
}
145145

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-
180146
/// Get the path to the crate's main binary.
181147
///
182148
/// Intended for caching the location, reducing the cargo overhead.
@@ -195,21 +161,12 @@ fn extract_filenames(msg: &escargot::Message, kind: &str) -> Option<path::PathBu
195161
/// .unwrap();
196162
/// ```
197163
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())
213170
}
214171

215172
/// Get the path to the specified binary of the current crate.
@@ -228,14 +185,13 @@ pub fn main_binary_path() -> Result<path::PathBuf, CargoError> {
228185
/// .unwrap();
229186
/// ```
230187
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())
239195
}
240196

241197
/// 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
254210
/// .unwrap();
255211
/// ```
256212
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()
258214
.build()
259215
.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())
268220
}
269221

270222
/// Error when finding crate binary.
271223
#[derive(Debug)]
272224
pub struct CargoError {
273-
context: Option<String>,
274225
cause: Option<Box<Error + Send + Sync + 'static>>,
275226
}
276227

277228
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-
289229
fn with_cause<E>(cause: E) -> Self
290230
where
291231
E: Error + Send + Sync + 'static,
292232
{
293233
let cause = Box::new(cause);
294-
Self {
295-
context: None,
296-
cause: Some(cause),
297-
}
234+
Self { cause: Some(cause) }
298235
}
299236
}
300237

@@ -313,9 +250,6 @@ impl Error for CargoError {
313250

314251
impl fmt::Display for CargoError {
315252
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
316-
if let Some(ref context) = self.context {
317-
writeln!(f, "{}", context)?;
318-
}
319253
if let Some(ref cause) = self.cause {
320254
writeln!(f, "Cause: {}", cause)?;
321255
}

src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ extern crate escargot;
6464
extern crate predicates;
6565
extern crate predicates_core;
6666
extern crate predicates_tree;
67-
#[macro_use]
68-
extern crate serde;
6967

7068
pub mod assert;
7169
pub use assert::Assert;

0 commit comments

Comments
 (0)