Skip to main content

runex_core/
model.rs

1use serde::Deserialize;
2
3/// A single abbreviation rule: rune → cast.
4#[derive(Debug, Clone, PartialEq, Deserialize)]
5pub struct Abbr {
6    pub key: String,
7    pub expand: String,
8    pub when_command_exists: Option<Vec<String>>,
9}
10
11/// Top-level configuration.
12#[derive(Debug, Clone, PartialEq, Deserialize)]
13pub struct Config {
14    pub version: u32,
15    #[serde(default)]
16    pub abbr: Vec<Abbr>,
17}
18
19/// Result of an expand operation.
20#[derive(Debug, Clone, PartialEq)]
21pub enum ExpandResult {
22    Expanded(String),
23    PassThrough(String),
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    #[test]
31    fn abbr_fields() {
32        let a = Abbr {
33            key: "gcm".into(),
34            expand: "git commit -m".into(),
35            when_command_exists: None,
36        };
37        assert_eq!(a.key, "gcm");
38        assert_eq!(a.expand, "git commit -m");
39        assert!(a.when_command_exists.is_none());
40    }
41
42    #[test]
43    fn abbr_with_when_command_exists() {
44        let a = Abbr {
45            key: "ls".into(),
46            expand: "lsd".into(),
47            when_command_exists: Some(vec!["lsd".into()]),
48        };
49        assert_eq!(a.when_command_exists.unwrap(), vec!["lsd".to_string()]);
50    }
51
52    #[test]
53    fn config_fields() {
54        let c = Config {
55            version: 1,
56            abbr: vec![],
57        };
58        assert_eq!(c.version, 1);
59        assert!(c.abbr.is_empty());
60    }
61
62    #[test]
63    fn expand_result_variants() {
64        let expanded = ExpandResult::Expanded("git commit -m".into());
65        let pass = ExpandResult::PassThrough("unknown".into());
66        assert_eq!(expanded, ExpandResult::Expanded("git commit -m".into()));
67        assert_eq!(pass, ExpandResult::PassThrough("unknown".into()));
68    }
69}