forked from rcoh/angle-grinder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalias.rs
More file actions
50 lines (41 loc) · 1.37 KB
/
alias.rs
File metadata and controls
50 lines (41 loc) · 1.37 KB
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
//! Instructions on adding a new alias:
//! 1. Create a new file for the alias in `aliases`.
//! 1a. The filename is the string to be replaced.
//! 1b. The string inside the file is the replacement.
//! 2. Create a new test config inside `tests/structured_tests/aliases`.
//! 3. Add the test config to the `test_aliases()` test.
use lazy_static::lazy_static;
use include_dir::Dir;
use serde::Deserialize;
const ALIASES_DIR: Dir = include_dir!("aliases");
lazy_static! {
pub static ref LOADED_ALIASES: Vec<AliasConfig> = ALIASES_DIR
.files()
.iter()
.map(|file| {
toml::from_str(file.contents_utf8().expect("load string")).expect("toml valid")
})
.collect();
pub static ref LOADED_KEYWORDS: Vec<&'static str> =
LOADED_ALIASES.iter().map(|a| a.keyword.as_str()).collect();
}
#[derive(Debug, Deserialize, PartialEq)]
pub struct AliasConfig {
keyword: String,
template: String,
}
impl AliasConfig {
pub fn matching_string(s: String) -> Result<&'static AliasConfig, ()> {
for alias in LOADED_ALIASES.iter() {
if alias.keyword != s {
continue;
}
return Ok(alias);
}
Err(())
}
/// Render the alias as a string that should parse into a valid operator.
pub fn render(&self) -> String {
self.template.clone()
}
}