-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig_alias.rs
113 lines (80 loc) · 3.15 KB
/
config_alias.rs
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::collections::HashMap;
use anyhow::Result;
pub struct AliasConfig<'a> {
pub map: crate::config_map::ConfigMap,
pub parent: &'a mut (dyn crate::config::Config + 'a),
}
impl AliasConfig<'_> {
pub fn get(&self, alias: &str) -> (String, bool) {
if self.map.is_empty() {
return ("".to_string(), false);
}
let value = match self.map.get_string_value(alias) {
Ok(value) => value,
Err(_) => "".to_string(),
};
(value.to_string(), !value.is_empty())
}
pub fn add(&mut self, alias: &str, expansion: &str) -> Result<()> {
self.map.set_string_value(alias, Some(expansion))?;
self.parent.save_aliases(&self.map)?;
// Update the parent config.
self.parent.write()
}
pub fn delete(&mut self, alias: &str) -> Result<()> {
self.map.remove_entry(alias)?;
self.parent.save_aliases(&self.map)?;
// Update the parent config.
self.parent.write()
}
pub fn list(&self) -> HashMap<String, String> {
let mut list: HashMap<String, String> = HashMap::new();
for (key, value) in self.map.root.iter() {
list.insert(key.to_string(), value.to_string());
}
list
}
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;
use crate::config::Config;
#[test]
fn test_aliases() {
let mut c = crate::config::new_blank_config().unwrap();
let mut aliases = c.aliases().unwrap();
let alias_list = aliases.list();
assert!(alias_list.is_empty());
assert_eq!(aliases.get("empty"), ("".to_string(), false));
// Add some aliases.
aliases.add("alias1", "value1 thing foo").unwrap();
aliases.add("alias2", "value2 single").unwrap();
let alias_list = aliases.list();
assert_eq!(alias_list.len(), 2);
assert_eq!(aliases.get("alias1"), ("value1 thing foo".to_string(), true));
assert_eq!(aliases.get("alias2"), ("value2 single".to_string(), true));
assert_eq!(aliases.get("not_existing"), ("".to_string(), false));
aliases.add("alias_3", "things hi there").unwrap();
assert_eq!(aliases.get("alias_3"), ("things hi there".to_string(), true));
assert_eq!(aliases.list().len(), 3);
aliases.delete("alias_3").unwrap();
assert_eq!(aliases.get("alias_3"), ("".to_string(), false));
// Print the config.
let expected = r#"# What editor zoo should run when creating text, etc. If blank, will refer to environment.
editor = ""
# When to interactively prompt. This is a global config that cannot be overridden by hostname.
# Supported values: enabled, disabled
prompt = "enabled"
# A pager program to send command output to, e.g. "less". Set the value to "cat" to disable the pager.
pager = ""
# What web browser zoo should use when opening URLs. If blank, will refer to environment.
browser = ""
# What formatting zoo should use when printing text.
# Supported values: table, json, yaml
format = "table"
[aliases]
alias1 = "value1 thing foo"
alias2 = "value2 single""#;
assert_eq!(c.config_to_string().unwrap(), expected);
}
}