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