use std::ffi::OsStr;
use std::path::PathBuf;
pub trait PathLookup {
fn which_on_path(&self, prefixed_name: &str) -> Option<PathBuf>;
}
pub struct EnvPath;
impl PathLookup for EnvPath {
fn which_on_path(&self, prefixed_name: &str) -> Option<PathBuf> {
which_in_path(prefixed_name)
}
}
pub(crate) fn which_in_path(name: &str) -> Option<PathBuf> {
which_in_path_env(name, std::env::var_os("PATH").as_deref())
}
pub(crate) fn which_in_path_env(name: &str, path: Option<&OsStr>) -> Option<PathBuf> {
let path = path?;
for dir in std::env::split_paths(path) {
let candidate = dir.join(name);
if candidate.is_file() {
return Some(candidate);
}
}
None
}