on v0.8.0 help sign in
bat/src/syntax_mapping.rs
35 lines 907 B history raw
1use std::borrow::Cow;
2use std::collections::HashMap;
4#[derive(Debug, Clone)]
5pub struct SyntaxMapping(HashMap<String, String>);
7impl SyntaxMapping {
8 pub fn new() -> SyntaxMapping {
9 SyntaxMapping(HashMap::new())
10 }
12 pub fn insert(&mut self, from: String, to: String) -> Option<String> {
13 self.0.insert(from, to)
14 }
16 pub fn replace<'a>(&self, input: &'a str) -> Cow<'a, str> {
17 let mut out = Cow::from(input);
18 if let Some(value) = self.0.get(input) {
19 out = Cow::from(value.clone())
20 }
21 out
22 }
25#[test]
26fn basic() {
27 let mut map = SyntaxMapping::new();
28 map.insert("Cargo.lock".into(), "toml".into());
29 map.insert(".ignore".into(), ".gitignore".into());
31 assert_eq!("toml", map.replace("Cargo.lock"));
32 assert_eq!("other.lock", map.replace("other.lock"));
34 assert_eq!(".gitignore", map.replace(".ignore"));