Skip to main content

voicepeak_cli/
env_check.rs

1use std::path::Path;
2use std::process::Command;
3
4const VOICEPEAK_PATH: &str = "/Applications/voicepeak.app/Contents/MacOS/voicepeak";
5
6#[derive(Debug)]
7pub enum EnvironmentError {
8    NotMacOS,
9    VoicepeakNotInstalled,
10    MpvNotInstalled,
11}
12
13impl std::fmt::Display for EnvironmentError {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        match self {
16            EnvironmentError::NotMacOS => {
17                write!(f, "This application is only supported on macOS")
18            }
19            EnvironmentError::VoicepeakNotInstalled => {
20                write!(f, "VOICEPEAK is not installed. Please install VOICEPEAK from the official website.\nExpected path: {}", VOICEPEAK_PATH)
21            }
22            EnvironmentError::MpvNotInstalled => {
23                write!(
24                    f,
25                    "mpv is not installed. Please install mpv using Homebrew:\n  brew install mpv"
26                )
27            }
28        }
29    }
30}
31
32impl std::error::Error for EnvironmentError {}
33
34pub fn check_environment() -> Result<(), EnvironmentError> {
35    check_macos()?;
36    check_voicepeak_installed()?;
37    check_mpv_installed()?;
38    Ok(())
39}
40
41fn check_macos() -> Result<(), EnvironmentError> {
42    if !cfg!(target_os = "macos") {
43        return Err(EnvironmentError::NotMacOS);
44    }
45    Ok(())
46}
47
48fn check_voicepeak_installed() -> Result<(), EnvironmentError> {
49    if !Path::new(VOICEPEAK_PATH).exists() {
50        return Err(EnvironmentError::VoicepeakNotInstalled);
51    }
52    Ok(())
53}
54
55fn check_mpv_installed() -> Result<(), EnvironmentError> {
56    match Command::new("mpv").arg("--version").output() {
57        Ok(output) => {
58            if output.status.success() {
59                Ok(())
60            } else {
61                Err(EnvironmentError::MpvNotInstalled)
62            }
63        }
64        Err(_) => Err(EnvironmentError::MpvNotInstalled),
65    }
66}