2
0
mirror of https://github.com/sharkdp/bat synced 2024-11-08 19:10:41 +00:00
bat/src/syntax_mapping.rs

36 lines
934 B
Rust
Raw Normal View History

use std::borrow::Cow;
use std::collections::HashMap;
2019-03-08 10:46:49 +00:00
#[derive(Debug, Clone, Default)]
pub struct SyntaxMapping(HashMap<String, String>);
impl SyntaxMapping {
pub fn new() -> SyntaxMapping {
2019-03-08 10:46:49 +00:00
Default::default()
}
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-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,
}
}
}
#[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");
assert_eq!("toml", map.replace("Cargo.lock"));
assert_eq!("other.lock", map.replace("other.lock"));
assert_eq!(".gitignore", map.replace(".ignore"));
}