#![forbid(unsafe_code)]
mod checker;
mod error;
mod finder;
#[cfg(windows)]
mod helper;
#[cfg(feature = "regex")]
use std::borrow::Borrow;
use std::env;
use std::fmt;
use std::path;
use std::ffi::{OsStr, OsString};
use crate::checker::{CompositeChecker, ExecutableChecker, ExistedChecker};
pub use crate::error::*;
use crate::finder::Finder;
pub fn which<T: AsRef<OsStr>>(binary_name: T) -> Result<path::PathBuf> {
which_all(binary_name).and_then(|mut i| i.next().ok_or(Error::CannotFindBinaryPath))
}
pub fn which_global<T: AsRef<OsStr>>(binary_name: T) -> Result<path::PathBuf> {
which_all_global(binary_name).and_then(|mut i| i.next().ok_or(Error::CannotFindBinaryPath))
}
pub fn which_all<T: AsRef<OsStr>>(binary_name: T) -> Result<impl Iterator<Item = path::PathBuf>> {
let cwd = env::current_dir().ok();
let binary_checker = build_binary_checker();
let finder = Finder::new();
finder.find(binary_name, env::var_os("PATH"), cwd, binary_checker)
}
pub fn which_all_global<T: AsRef<OsStr>>(
binary_name: T,
) -> Result<impl Iterator<Item = path::PathBuf>> {
let binary_checker = build_binary_checker();
let finder = Finder::new();
finder.find(
binary_name,
env::var_os("PATH"),
Option::<&Path>::None,
binary_checker,
)
}
#[cfg(feature = "regex")]
pub fn which_re(regex: impl Borrow<Regex>) -> Result<impl Iterator<Item = path::PathBuf>> {
which_re_in(regex, env::var_os("PATH"))
}
pub fn which_in<T, U, V>(binary_name: T, paths: Option<U>, cwd: V) -> Result<path::PathBuf>
where
T: AsRef<OsStr>,
U: AsRef<OsStr>,
V: AsRef<path::Path>,
{
which_in_all(binary_name, paths, cwd)
.and_then(|mut i| i.next().ok_or(Error::CannotFindBinaryPath))
}
#[cfg(feature = "regex")]
pub fn which_re_in<T>(
regex: impl Borrow<Regex>,
paths: Option<T>,
) -> Result<impl Iterator<Item = path::PathBuf>>
where
T: AsRef<OsStr>,
{
let binary_checker = build_binary_checker();
let finder = Finder::new();
finder.find_re(regex, paths, binary_checker)
}
pub fn which_in_all<T, U, V>(
binary_name: T,
paths: Option<U>,
cwd: V,
) -> Result<impl Iterator<Item = path::PathBuf>>
where
T: AsRef<OsStr>,
U: AsRef<OsStr>,
V: AsRef<path::Path>,
{
let binary_checker = build_binary_checker();
let finder = Finder::new();
finder.find(binary_name, paths, Some(cwd), binary_checker)
}
pub fn which_in_global<T, U>(
binary_name: T,
paths: Option<U>,
) -> Result<impl Iterator<Item = path::PathBuf>>
where
T: AsRef<OsStr>,
U: AsRef<OsStr>,
{
let binary_checker = build_binary_checker();
let finder = Finder::new();
finder.find(binary_name, paths, Option::<&Path>::None, binary_checker)
}
fn build_binary_checker() -> CompositeChecker {
CompositeChecker::new()
.add_checker(Box::new(ExistedChecker::new()))
.add_checker(Box::new(ExecutableChecker::new()))
}
pub struct WhichConfig {
cwd: Option<either::Either<bool, path::PathBuf>>,
custom_path_list: Option<OsString>,
binary_name: Option<OsString>,
#[cfg(feature = "regex")]
regex: Option<Regex>,
}
impl Default for WhichConfig {
fn default() -> Self {
Self {
cwd: Some(either::Either::Left(true)),
custom_path_list: None,
binary_name: None,
#[cfg(feature = "regex")]
regex: None,
}
}
}
#[cfg(feature = "regex")]
type Regex = regex::Regex;
#[cfg(not(feature = "regex"))]
type Regex = ();
impl WhichConfig {
pub fn new() -> Self {
Self::default()
}
pub fn system_cwd(mut self, use_cwd: bool) -> Self {
#[cfg(feature = "regex")]
if self.regex.is_some() && use_cwd {
panic!("which can't use regex and cwd at the same time!")
}
self.cwd = Some(either::Either::Left(use_cwd));
self
}
pub fn custom_cwd(mut self, cwd: path::PathBuf) -> Self {
#[cfg(feature = "regex")]
if self.regex.is_some() {
panic!("which can't use regex and cwd at the same time!")
}
self.cwd = Some(either::Either::Right(cwd));
self
}
#[allow(unused_variables)]
#[allow(unused_mut)]
pub fn regex(mut self, regex: Regex) -> Self {
#[cfg(not(feature = "regex"))]
{
panic!("which's regex feature was not enabled in your Cargo.toml!")
}
#[cfg(feature = "regex")]
{
if self.cwd != Some(either::Either::Left(false)) && self.cwd.is_some() {
panic!("which can't use regex and cwd at the same time!")
}
if self.binary_name.is_some() {
panic!("which can't use `binary_name` and `regex` at the same time!");
}
self.regex = Some(regex);
self
}
}
pub fn binary_name(mut self, name: OsString) -> Self {
#[cfg(feature = "regex")]
if self.regex.is_some() {
panic!("which can't use `binary_name` and `regex` at the same time!");
}
self.binary_name = Some(name);
self
}
pub fn custom_path_list(mut self, custom_path_list: OsString) -> Self {
self.custom_path_list = Some(custom_path_list);
self
}
pub fn system_path_list(mut self) -> Self {
self.custom_path_list = None;
self
}
pub fn first_result(self) -> Result<path::PathBuf> {
self.all_results()
.and_then(|mut i| i.next().ok_or(Error::CannotFindBinaryPath))
}
pub fn all_results(self) -> Result<impl Iterator<Item = path::PathBuf>> {
let binary_checker = build_binary_checker();
let finder = Finder::new();
let paths = self.custom_path_list.or_else(|| env::var_os("PATH"));
#[cfg(feature = "regex")]
if let Some(regex) = self.regex {
return finder
.find_re(regex, paths, binary_checker)
.map(|i| Box::new(i) as Box<dyn Iterator<Item = path::PathBuf>>);
}
let cwd = match self.cwd {
Some(either::Either::Left(false)) => None,
Some(either::Either::Right(custom)) => Some(custom),
None | Some(either::Either::Left(true)) => env::current_dir().ok(),
};
finder
.find(
self.binary_name.expect(
"binary_name not set! You must set binary_name or regex before searching!",
),
paths,
cwd,
binary_checker,
)
.map(|i| Box::new(i) as Box<dyn Iterator<Item = path::PathBuf>>)
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct Path {
inner: path::PathBuf,
}
impl Path {
pub fn new<T: AsRef<OsStr>>(binary_name: T) -> Result<Path> {
which(binary_name).map(|inner| Path { inner })
}
pub fn all<T: AsRef<OsStr>>(binary_name: T) -> Result<impl Iterator<Item = Path>> {
which_all(binary_name).map(|inner| inner.map(|inner| Path { inner }))
}
pub fn new_in<T, U, V>(binary_name: T, paths: Option<U>, cwd: V) -> Result<Path>
where
T: AsRef<OsStr>,
U: AsRef<OsStr>,
V: AsRef<path::Path>,
{
which_in(binary_name, paths, cwd).map(|inner| Path { inner })
}
pub fn all_in<T, U, V>(
binary_name: T,
paths: Option<U>,
cwd: V,
) -> Result<impl Iterator<Item = Path>>
where
T: AsRef<OsStr>,
U: AsRef<OsStr>,
V: AsRef<path::Path>,
{
which_in_all(binary_name, paths, cwd).map(|inner| inner.map(|inner| Path { inner }))
}
pub fn as_path(&self) -> &path::Path {
self.inner.as_path()
}
pub fn into_path_buf(self) -> path::PathBuf {
self.inner
}
}
impl fmt::Debug for Path {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.inner, f)
}
}
impl std::ops::Deref for Path {
type Target = path::Path;
fn deref(&self) -> &path::Path {
self.inner.deref()
}
}
impl AsRef<path::Path> for Path {
fn as_ref(&self) -> &path::Path {
self.as_path()
}
}
impl AsRef<OsStr> for Path {
fn as_ref(&self) -> &OsStr {
self.as_os_str()
}
}
impl PartialEq<path::PathBuf> for Path {
fn eq(&self, other: &path::PathBuf) -> bool {
self.inner == *other
}
}
impl PartialEq<Path> for path::PathBuf {
fn eq(&self, other: &Path) -> bool {
*self == other.inner
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct CanonicalPath {
inner: path::PathBuf,
}
impl CanonicalPath {
pub fn new<T: AsRef<OsStr>>(binary_name: T) -> Result<CanonicalPath> {
which(binary_name)
.and_then(|p| p.canonicalize().map_err(|_| Error::CannotCanonicalize))
.map(|inner| CanonicalPath { inner })
}
pub fn all<T: AsRef<OsStr>>(
binary_name: T,
) -> Result<impl Iterator<Item = Result<CanonicalPath>>> {
which_all(binary_name).map(|inner| {
inner.map(|inner| {
inner
.canonicalize()
.map_err(|_| Error::CannotCanonicalize)
.map(|inner| CanonicalPath { inner })
})
})
}
pub fn new_in<T, U, V>(binary_name: T, paths: Option<U>, cwd: V) -> Result<CanonicalPath>
where
T: AsRef<OsStr>,
U: AsRef<OsStr>,
V: AsRef<path::Path>,
{
which_in(binary_name, paths, cwd)
.and_then(|p| p.canonicalize().map_err(|_| Error::CannotCanonicalize))
.map(|inner| CanonicalPath { inner })
}
pub fn all_in<T, U, V>(
binary_name: T,
paths: Option<U>,
cwd: V,
) -> Result<impl Iterator<Item = Result<CanonicalPath>>>
where
T: AsRef<OsStr>,
U: AsRef<OsStr>,
V: AsRef<path::Path>,
{
which_in_all(binary_name, paths, cwd).map(|inner| {
inner.map(|inner| {
inner
.canonicalize()
.map_err(|_| Error::CannotCanonicalize)
.map(|inner| CanonicalPath { inner })
})
})
}
pub fn as_path(&self) -> &path::Path {
self.inner.as_path()
}
pub fn into_path_buf(self) -> path::PathBuf {
self.inner
}
}
impl fmt::Debug for CanonicalPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.inner, f)
}
}
impl std::ops::Deref for CanonicalPath {
type Target = path::Path;
fn deref(&self) -> &path::Path {
self.inner.deref()
}
}
impl AsRef<path::Path> for CanonicalPath {
fn as_ref(&self) -> &path::Path {
self.as_path()
}
}
impl AsRef<OsStr> for CanonicalPath {
fn as_ref(&self) -> &OsStr {
self.as_os_str()
}
}
impl PartialEq<path::PathBuf> for CanonicalPath {
fn eq(&self, other: &path::PathBuf) -> bool {
self.inner == *other
}
}
impl PartialEq<CanonicalPath> for path::PathBuf {
fn eq(&self, other: &CanonicalPath) -> bool {
*self == other.inner
}
}