use thiserror::Error;
use std::path::{Path, PathBuf};
#[derive(Debug, Error)]
pub enum WhichError {
#[error("failed to access the PATH environment variable")]
PathVariable(#[source] std::env::VarError),
}
pub fn which<P: AsRef<Path>>(command: P) -> Result<Option<PathBuf>, WhichError> {
let path_var = std::env::var("PATH").map_err(WhichError::PathVariable)?;
for dir in path_var.split(':') {
let bin_path = Path::new(dir).join(&command);
if bin_path.exists() && bin_path.is_file() {
return Ok(Some(bin_path));
}
}
Ok(None)
}