From 0e2fe028e6362afdf25d4dee409a794829064626 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Sun, 17 Jun 2018 11:43:25 +0300 Subject: [PATCH] Utils module --- src/git.rs | 2 +- src/linux.rs | 2 +- src/main.rs | 43 ++++--------------------------------------- src/npm.rs | 2 +- src/steps.rs | 2 +- src/utils.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 49 insertions(+), 43 deletions(-) create mode 100644 src/utils.rs diff --git a/src/git.rs b/src/git.rs index f351f77c..42a695fa 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,4 +1,4 @@ -use super::Check; +use super::utils::Check; use failure::Error; use std::collections::HashSet; use std::path::{Path, PathBuf}; diff --git a/src/linux.rs b/src/linux.rs index ed147368..769a8b6f 100644 --- a/src/linux.rs +++ b/src/linux.rs @@ -1,5 +1,5 @@ use super::terminal::Terminal; -use super::Check; +use super::utils::Check; use failure; use std::fs; use std::path::PathBuf; diff --git a/src/main.rs b/src/main.rs index 25487e5d..5e7dee70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ mod npm; mod report; mod steps; mod terminal; +mod utils; mod vim; use clap::App; @@ -27,53 +28,17 @@ use failure::Error; use git::{Git, Repositories}; use report::{Report, Reporter}; use std::env::home_dir; -use std::path::{Path, PathBuf}; -use std::process::{exit, ExitStatus}; +use std::path::PathBuf; +use std::process::exit; use steps::*; use terminal::Terminal; +use utils::{home_path, is_ancestor}; use which::which; -#[derive(Fail, Debug)] -#[fail(display = "Process failed")] -struct ProcessFailed; - #[derive(Fail, Debug)] #[fail(display = "A step failed")] struct StepFailed; -trait Check { - fn check(self) -> Result<(), Error>; -} - -impl Check for ExitStatus { - fn check(self) -> Result<(), Error> { - if self.success() { - Ok(()) - } else { - Err(Error::from(ProcessFailed {})) - } - } -} - -fn home_path(p: &str) -> PathBuf { - let mut path = home_dir().unwrap(); - path.push(p); - path -} - -fn is_ancestor(ancestor: &Path, path: &Path) -> bool { - let mut p = path; - while let Some(parent) = p.parent() { - if parent == ancestor { - return true; - } - - p = parent; - } - - false -} - #[cfg(unix)] fn tpm() -> Option { let mut path = home_dir().unwrap(); diff --git a/src/npm.rs b/src/npm.rs index 42c43235..2f775f33 100644 --- a/src/npm.rs +++ b/src/npm.rs @@ -1,4 +1,4 @@ -use super::Check; +use super::utils::Check; use failure; use std::path::PathBuf; use std::process::Command; diff --git a/src/steps.rs b/src/steps.rs index c677559b..0d70dec3 100644 --- a/src/steps.rs +++ b/src/steps.rs @@ -1,4 +1,4 @@ -use super::Check; +use super::utils::Check; use failure; use std::path::PathBuf; use std::process::Command; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 00000000..374147c8 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,41 @@ +use failure::Error; +use std::env::home_dir; +use std::path::{Path, PathBuf}; +use std::process::ExitStatus; + +#[derive(Fail, Debug)] +#[fail(display = "Process failed")] +pub struct ProcessFailed; + +pub trait Check { + fn check(self) -> Result<(), Error>; +} + +impl Check for ExitStatus { + fn check(self) -> Result<(), Error> { + if self.success() { + Ok(()) + } else { + Err(Error::from(ProcessFailed {})) + } + } +} + +pub fn home_path(p: &str) -> PathBuf { + let mut path = home_dir().unwrap(); + path.push(p); + path +} + +pub fn is_ancestor(ancestor: &Path, path: &Path) -> bool { + let mut p = path; + while let Some(parent) = p.parent() { + if parent == ancestor { + return true; + } + + p = parent; + } + + false +}