1
use regex::Regex;
2

            
3
use crate::utils::commands::CLI;
4

            
5
use super::Protontricks;
6

            
7
impl Protontricks {
8
    /// Installs the following components if they aren't already installed
9
    pub async fn install_components(app_id: &str, components: &Vec<String>) -> Result<u32, String> {
10
        let args = [app_id.to_string(), "-q".to_string()];
11
        let args = args.iter().cloned().chain(components.into_iter().cloned());
12

            
13
        let output = Protontricks::exec(args).await?;
14

            
15
        // example: 'gdiplus already installed, skipping'
16
        let re = Regex::new(r"(?m)^(?<name>[\w-]+) already installed, skipping$").unwrap();
17

            
18
        let total_tweaks: u32 = components.len().try_into().unwrap();
19
        let tweaks_already_installed: u32 = re.captures_iter(&output).count().try_into().unwrap();
20

            
21
        Ok(total_tweaks - tweaks_already_installed)
22
    }
23
}