Skip to main content

runex_core/
model.rs

1use serde::Deserialize;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Default)]
4#[serde(rename_all = "kebab-case")]
5pub enum TriggerKey {
6    #[default]
7    Space,
8    Tab,
9    AltSpace,
10}
11
12#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Default)]
13pub struct KeybindConfig {
14    pub trigger: Option<TriggerKey>,
15    pub bash: Option<TriggerKey>,
16    pub zsh: Option<TriggerKey>,
17    pub pwsh: Option<TriggerKey>,
18    pub nu: Option<TriggerKey>,
19}
20
21/// A single abbreviation rule: rune → cast.
22#[derive(Debug, Clone, PartialEq, Deserialize)]
23pub struct Abbr {
24    pub key: String,
25    pub expand: String,
26    pub when_command_exists: Option<Vec<String>>,
27}
28
29/// Top-level configuration.
30#[derive(Debug, Clone, PartialEq, Deserialize)]
31pub struct Config {
32    pub version: u32,
33    #[serde(default)]
34    pub keybind: KeybindConfig,
35    #[serde(default)]
36    pub abbr: Vec<Abbr>,
37}
38
39/// Result of an expand operation.
40#[derive(Debug, Clone, PartialEq)]
41pub enum ExpandResult {
42    Expanded(String),
43    PassThrough(String),
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn abbr_fields() {
52        let a = Abbr {
53            key: "gcm".into(),
54            expand: "git commit -m".into(),
55            when_command_exists: None,
56        };
57        assert_eq!(a.key, "gcm");
58        assert_eq!(a.expand, "git commit -m");
59        assert!(a.when_command_exists.is_none());
60    }
61
62    #[test]
63    fn abbr_with_when_command_exists() {
64        let a = Abbr {
65            key: "ls".into(),
66            expand: "lsd".into(),
67            when_command_exists: Some(vec!["lsd".into()]),
68        };
69        assert_eq!(a.when_command_exists.unwrap(), vec!["lsd".to_string()]);
70    }
71
72    #[test]
73    fn config_fields() {
74        let c = Config {
75            version: 1,
76            keybind: KeybindConfig::default(),
77            abbr: vec![],
78        };
79        assert_eq!(c.version, 1);
80        assert_eq!(c.keybind, KeybindConfig::default());
81        assert!(c.abbr.is_empty());
82    }
83
84    #[test]
85    fn keybind_config_fields() {
86        let k = KeybindConfig {
87            trigger: Some(TriggerKey::Space),
88            bash: Some(TriggerKey::AltSpace),
89            zsh: Some(TriggerKey::Space),
90            pwsh: Some(TriggerKey::Tab),
91            nu: None,
92        };
93        assert_eq!(k.trigger, Some(TriggerKey::Space));
94        assert_eq!(k.bash, Some(TriggerKey::AltSpace));
95        assert_eq!(k.zsh, Some(TriggerKey::Space));
96        assert_eq!(k.pwsh, Some(TriggerKey::Tab));
97        assert_eq!(k.nu, None);
98    }
99
100    #[test]
101    fn expand_result_variants() {
102        let expanded = ExpandResult::Expanded("git commit -m".into());
103        let pass = ExpandResult::PassThrough("unknown".into());
104        assert_eq!(expanded, ExpandResult::Expanded("git commit -m".into()));
105        assert_eq!(pass, ExpandResult::PassThrough("unknown".into()));
106    }
107}