steam_tui/interface/
executable.rs

1use crate::util::{error::STError, log::log, parser::*, paths::executable_join};
2
3use std::cmp::Ordering;
4
5use std::collections::HashMap;
6
7use serde::{Deserialize, Serialize};
8
9#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
10pub enum Platform {
11    Linux,
12    Mac,
13    Windows,
14    Unknown,
15}
16
17#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
18pub struct Executable {
19    pub platform: Platform,
20    pub executable: String,
21    pub arguments: String,
22}
23impl Executable {
24    pub fn new(config: &HashMap<String, Datum>, installdir: &str) -> Result<Executable, STError> {
25        let platform = match config.get("config") {
26            Some(Datum::Nest(config)) => match config.get("oslist") {
27                Some(Datum::Value(ref platform)) => match platform.as_str() {
28                    "linux" => Platform::Linux,
29                    "windows" => Platform::Windows,
30                    "macos" => Platform::Mac,
31                    _ => Platform::Unknown,
32                },
33                _ => Platform::Unknown,
34            },
35            _ => Platform::Unknown,
36        };
37        Ok(Executable {
38            platform,
39            executable: executable_join(
40                &config
41                    .get("executable")
42                    .unwrap_or(&Datum::Value("".into()))
43                    .maybe_value()?,
44                installdir,
45            )?
46            .to_str()
47            .unwrap_or("")
48            .to_string(),
49            arguments: config
50                .get("arguments")
51                .unwrap_or(&Datum::Value("".into()))
52                .maybe_value()?,
53        })
54    }
55    // executables sorted by platform preference
56    pub fn get_executables(
57        config: &Option<Datum>,
58        installdir: String,
59    ) -> Result<Vec<Executable>, STError> {
60        let mut executables = vec![];
61        log!(config);
62        if let Some(Datum::Nest(config)) = config {
63            let mut keys = config
64                .keys()
65                .map(|k| k.parse::<i32>().unwrap_or(-1))
66                .filter(|k| k >= &0)
67                .collect::<Vec<i32>>();
68            // UNstable sort recommended by clippy for primatives
69            keys.sort_unstable();
70            for key in keys {
71                if let Some(Datum::Nest(config)) = config.get(&format!("{}", key)) {
72                    executables.push(Executable::new(config, &installdir)?);
73                }
74            }
75            executables.sort_by(|a, b| match (&a.platform, &b.platform) {
76                (&Platform::Linux, _) => Ordering::Less,
77                (_, &Platform::Linux) => Ordering::Greater,
78                (&Platform::Windows, _) => Ordering::Less,
79                (_, &Platform::Windows) => Ordering::Greater,
80                _ => Ordering::Equal,
81            });
82        }
83        Ok(executables)
84    }
85}