use std::error::Error;
use std::ffi;
use std::fmt;
use std::path;
use std::process;
use escargot;
pub trait CommandCargoExt
where
Self: Sized,
{
fn main_binary() -> Result<Self, CargoError>;
fn cargo_bin<S: AsRef<ffi::OsStr>>(name: S) -> Result<Self, CargoError>;
fn cargo_example<S: AsRef<ffi::OsStr>>(name: S) -> Result<Self, CargoError>;
}
impl CommandCargoExt for process::Command {
fn main_binary() -> Result<Self, CargoError> {
let runner = escargot::CargoBuild::new()
.current_release()
.run()
.map_err(CargoError::with_cause)?;
Ok(runner.command())
}
fn cargo_bin<S: AsRef<ffi::OsStr>>(name: S) -> Result<Self, CargoError> {
let runner = escargot::CargoBuild::new()
.bin(name)
.current_release()
.run()
.map_err(CargoError::with_cause)?;
Ok(runner.command())
}
fn cargo_example<S: AsRef<ffi::OsStr>>(name: S) -> Result<Self, CargoError> {
let runner = escargot::CargoBuild::new()
.example(name)
.current_release()
.run()
.map_err(CargoError::with_cause)?;
Ok(runner.command())
}
}
#[deprecated(since = "0.9.1", note = "For caching, using escargot directly.")]
pub fn main_binary_path() -> Result<path::PathBuf, CargoError> {
let runner = escargot::CargoBuild::new()
.current_release()
.run()
.map_err(CargoError::with_cause)?;
Ok(runner.path().to_owned())
}
#[deprecated(since = "0.9.1", note = "For caching, using escargot directly.")]
pub fn cargo_bin_path<S: AsRef<ffi::OsStr>>(name: S) -> Result<path::PathBuf, CargoError> {
let runner = escargot::CargoBuild::new()
.bin(name)
.current_release()
.run()
.map_err(CargoError::with_cause)?;
Ok(runner.path().to_owned())
}
#[deprecated(since = "0.9.1", note = "For caching, using escargot directly.")]
pub fn cargo_example_path<S: AsRef<ffi::OsStr>>(name: S) -> Result<path::PathBuf, CargoError> {
let runner = escargot::CargoBuild::new()
.example(name)
.current_release()
.run()
.map_err(CargoError::with_cause)?;
Ok(runner.path().to_owned())
}
#[derive(Debug)]
pub struct CargoError {
cause: Option<Box<Error + Send + Sync + 'static>>,
}
impl CargoError {
fn with_cause<E>(cause: E) -> Self
where
E: Error + Send + Sync + 'static,
{
let cause = Box::new(cause);
Self { cause: Some(cause) }
}
}
impl Error for CargoError {
fn description(&self) -> &str {
"Cargo command failed."
}
fn cause(&self) -> Option<&Error> {
self.cause.as_ref().map(|c| {
let c: &Error = c.as_ref();
c
})
}
}
impl fmt::Display for CargoError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(ref cause) = self.cause {
writeln!(f, "Cause: {}", cause)?;
}
Ok(())
}
}