diff --git a/src/macros.rs b/src/macros.rs index 11411a4..f595b42 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -62,13 +62,13 @@ macro_rules! os_vendors { ($($os:literal => $($tool:literal),+);+$(;)?) => { pub fn detect_vendor() -> std::result::Result<$crate::Vendor, $crate::UptError> { let os = crate::utils::detect_os().unwrap_or_default(); - let tools: Vec<&str> = match os.as_str() { + let pairs: Vec<(&str, &str)> = match os.as_str() { $( - $os => vec![$($tool),+].into_iter().filter_map(|v| which_cmd(v)).collect(), + $os => vec![$($tool),+].into_iter().filter_map(|tool| which_cmd(tool).map(|bin_name| (tool, bin_name))).collect(), )+ - _ => vec!["apt", "dnf", "pacman"], + _ => ["apt", "dnf", "pacman"].into_iter().map(|tool| (tool, tool)).collect(), }; - match $crate::utils::find_tool(&tools) { + match $crate::utils::find_tool(&pairs) { Some(tool) => $crate::vendor::init_vendor(&tool), None => Err(UptError::NoDetectVendor), } diff --git a/src/utils.rs b/src/utils.rs index c2aaf9c..fb3971e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,23 +1,24 @@ use which::which; -pub fn find_tool(tools: &[&str]) -> Option { - match tools.len() { +pub fn find_tool(pairs: &[(&str, &str)]) -> Option { + match pairs.len() { 0 => None, 1 => { - let tool = &tools[0]; - if which(tool).is_ok() { + let (tool, bin_name) = &pairs[0]; + if which(bin_name).is_ok() { Some(tool.to_string()) } else { None } } _ => { - let handles: Vec<_> = tools + let handles: Vec<_> = pairs .iter() - .map(|tool| { + .map(|(tool, bin_name)| { let tool = tool.to_string(); + let bin_name = bin_name.to_string(); std::thread::spawn(move || { - if which(&tool).is_ok() { + if which(&bin_name).is_ok() { Some(tool) } else { None