use crate::{WallSwitchError, WallSwitchResult};
use std::path::PathBuf;
pub fn is_installed(binary: &str) -> bool {
which::which(binary).is_ok()
}
pub fn where_is(cmd: &str) -> WallSwitchResult<PathBuf> {
which::which(cmd).map_err(|_| WallSwitchError::UnableToFind(cmd.to_string()))
}
pub fn get_magick_path(verbose: bool) -> WallSwitchResult<PathBuf> {
let path = where_is("magick").or_else(|_| where_is("convert"));
match path {
Ok(magick_path) => {
if verbose {
println!("ImageMagick found at: {}", magick_path.display());
}
Ok(magick_path)
}
Err(_) => Err(WallSwitchError::UnableToFind(
"magick (or convert)".to_string(),
)),
}
}
pub fn get_identify_path(verbose: bool) -> WallSwitchResult<PathBuf> {
match where_is("identify") {
Ok(identify_path) => {
if verbose {
println!(
"ImageMagick 'identify' found at: {}",
identify_path.display()
);
}
Ok(identify_path)
}
Err(_) => Err(WallSwitchError::UnableToFind("identify".to_string())),
}
}
pub fn get_feh_path(verbose: bool) -> WallSwitchResult<PathBuf> {
match where_is("feh") {
Ok(feh_path) => {
if verbose {
println!("feh found at: {}", feh_path.display());
}
Ok(feh_path)
}
Err(_) => Err(WallSwitchError::UnableToFind("feh".to_string())),
}
}
pub fn get_awww_path(verbose: bool) -> WallSwitchResult<PathBuf> {
match where_is("awww") {
Ok(awww_path) => {
if verbose {
println!("awww daemon found at: {}", awww_path.display());
}
Ok(awww_path)
}
Err(_) => {
let install_instructions = "\
The 'awww' wallpaper daemon was not found on your system.\n\n\
Please install it to enable modern Wayland transitions:\n\n\
- Arch Linux / Manjaro (AUR):\n \
paru -S awww (or yay -S awww)\n\n\
- Debian / Ubuntu:\n \
Download the latest .deb from the GitHub releases page.\n\n\
- Fedora / RPM-based:\n \
Download the latest .rpm from the GitHub releases page.\n\n\
- Compile from Source (Any distro):\n \
git clone https://codeberg.org/LGFae/awww.git\n \
cd awww\n \
cargo build --release\n \
cargo install --path .\n"
.to_string();
Err(WallSwitchError::UnableToFind(install_instructions))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_installed() {
assert!(
is_installed("sh") || is_installed("ls"),
"Standard shell utilities should be detected"
);
assert!(
!is_installed("some_non_existent_binary_xyz_123"),
"Non-existent binaries should return false"
);
}
#[test]
fn test_get_paths_error_handling() {
let feh_result = get_feh_path(false);
if let Err(err) = feh_result {
assert!(
matches!(err, WallSwitchError::UnableToFind(_)),
"Expected UnableToFind error, got {:?}",
err
);
}
}
}