From cb25d13028ec1cdf986a3567ea52562ea654a7b8 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Tue, 23 Jan 2024 13:36:53 -0500 Subject: Improve error handling for `which::which` failures Co-authored-by: Pascal Kuthe --- helix-stdx/src/env.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'helix-stdx') diff --git a/helix-stdx/src/env.rs b/helix-stdx/src/env.rs index 3676727f..90a0aee8 100644 --- a/helix-stdx/src/env.rs +++ b/helix-stdx/src/env.rs @@ -1,6 +1,5 @@ -pub use which::which; - use std::{ + ffi::OsStr, path::{Path, PathBuf}, sync::RwLock, }; @@ -36,10 +35,33 @@ pub fn env_var_is_set(env_var_name: &str) -> bool { std::env::var_os(env_var_name).is_some() } -pub fn binary_exists(binary_name: &str) -> bool { +pub fn binary_exists>(binary_name: T) -> bool { which::which(binary_name).is_ok() } +pub fn which>( + binary_name: T, +) -> Result { + which::which(binary_name.as_ref()).map_err(|err| ExecutableNotFoundError { + command: binary_name.as_ref().to_string_lossy().into_owned(), + inner: err, + }) +} + +#[derive(Debug)] +pub struct ExecutableNotFoundError { + command: String, + inner: which::Error, +} + +impl std::fmt::Display for ExecutableNotFoundError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "command '{}' not found: {}", self.command, self.inner) + } +} + +impl std::error::Error for ExecutableNotFoundError {} + #[cfg(test)] mod tests { use super::{current_working_dir, set_current_working_dir}; -- cgit v1.2.3-70-g09d2