2018-10-17 20:30:09 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2019-03-08 10:46:49 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2018-10-17 20:30:09 +00:00
|
|
|
pub struct SyntaxMapping(HashMap<String, String>);
|
|
|
|
|
|
|
|
impl SyntaxMapping {
|
|
|
|
pub fn new() -> SyntaxMapping {
|
2019-03-08 10:46:49 +00:00
|
|
|
Default::default()
|
2018-10-17 20:30:09 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 03:40:54 +00:00
|
|
|
pub fn insert(&mut self, from: impl Into<String>, to: impl Into<String>) -> Option<String> {
|
|
|
|
self.0.insert(from.into(), to.into())
|
2018-10-17 20:30:09 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 03:40:54 +00:00
|
|
|
pub fn replace<'a>(&self, input: impl Into<Cow<'a, str>>) -> Cow<'a, str> {
|
|
|
|
let input = input.into();
|
|
|
|
match self.0.get(input.as_ref()) {
|
|
|
|
Some(s) => Cow::from(s.clone()),
|
|
|
|
None => input,
|
2018-10-17 20:30:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
|
|
|
let mut map = SyntaxMapping::new();
|
2018-11-27 03:40:54 +00:00
|
|
|
map.insert("Cargo.lock", "toml");
|
|
|
|
map.insert(".ignore", ".gitignore");
|
2018-10-17 20:30:09 +00:00
|
|
|
|
|
|
|
assert_eq!("toml", map.replace("Cargo.lock"));
|
|
|
|
assert_eq!("other.lock", map.replace("other.lock"));
|
|
|
|
|
|
|
|
assert_eq!(".gitignore", map.replace(".ignore"));
|
|
|
|
}
|