~ruther/sequence-detector

ref: c09cd55bc2030fc0f9cb4ddfb1d963dd0ed5a15e sequence-detector/src/settings.rs -rw-r--r-- 1.2 KiB
c09cd55b — František Boháček feat: add sequence detection behavior 1 year, 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::{process::{Output, Command}, io, path::PathBuf};

use config::{Config, ConfigError};
use serde_derive::Deserialize;

#[derive(Clone, Debug, Deserialize)]
pub struct Group {
    pub group_id: String,
    pub sequences: Vec<Sequence>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct Sequence {
    pub keys: Vec<String>,
    pub action: String,
}

#[derive(Debug, Deserialize)]
pub struct Settings {
    pub debounce_time: u64,
    pub groups: Vec<Group>,
}

impl Sequence {
    pub fn execute(&self) -> io::Result<Output> {
        let action: Vec<&str> = self.action.split(" ").collect();
        Command::new(&action[0]).args(&action[1..]).output()
    }
}

impl Settings {
    pub fn new(config: &Option<PathBuf>, default: &str) -> Result<Self, ConfigError> {
        let settings_path = &config.clone().unwrap_or_else(|| {
            let mut exe_path = std::env::current_exe().expect("Failed to get current executable path");
            exe_path.pop(); // Remove the executable name
            exe_path.push(default); // Append the default configuration file name
            exe_path
        });

        let s = Config::builder()
            .add_source(config::File::from(settings_path.clone()))
            .build()?;

        s.try_deserialize()
    }
}
Do not follow this link