Step refactoring
This commit is contained in:
29
src/utils.rs
29
src/utils.rs
@@ -32,6 +32,9 @@ where
|
||||
{
|
||||
fn if_exists(self) -> Option<Self>;
|
||||
fn is_descendant_of(&self, ancestor: &Path) -> bool;
|
||||
|
||||
/// Returns the path if it exists or ErrorKind::SkipStep otherwise
|
||||
fn require(self) -> Result<Self, Error>;
|
||||
}
|
||||
|
||||
impl PathExt for PathBuf {
|
||||
@@ -46,6 +49,14 @@ impl PathExt for PathBuf {
|
||||
fn is_descendant_of(&self, ancestor: &Path) -> bool {
|
||||
self.iter().zip(ancestor.iter()).all(|(a, b)| a == b)
|
||||
}
|
||||
|
||||
fn require(self) -> Result<Self, Error> {
|
||||
if self.exists() {
|
||||
Ok(self)
|
||||
} else {
|
||||
Err(ErrorKind::SkipStep)?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn which<T: AsRef<OsStr> + Debug>(binary_name: T) -> Option<PathBuf> {
|
||||
@@ -151,3 +162,21 @@ mod tests {
|
||||
assert_eq!("C:\\hi", humanize("//?/C:/hi"));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn require<T: AsRef<OsStr> + Debug>(binary_name: T) -> Result<PathBuf, Error> {
|
||||
match which_crate::which(&binary_name) {
|
||||
Ok(path) => {
|
||||
debug!("Detected {:?} as {:?}", &path, &binary_name);
|
||||
Ok(path)
|
||||
}
|
||||
Err(e) => match e.kind() {
|
||||
which_crate::ErrorKind::CannotFindBinaryPath => {
|
||||
debug!("Cannot find {:?}", &binary_name);
|
||||
Err(ErrorKind::SkipStep)?
|
||||
}
|
||||
_ => {
|
||||
panic!("Detecting {:?} failed: {}", &binary_name, e);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user