bat/src/syntax_mapping.rs

36 lines
941 B
Rust
Raw Normal View History

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