1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-04-14 14:50:40 +01:00

Enhance SyntaxMapping with impl Trait

This commit is contained in:
Ole Martin Ruud 2018-11-27 04:40:54 +01:00 committed by David Peter
parent 6b6a8f8e16
commit 348c9f3562
2 changed files with 10 additions and 10 deletions

View File

@ -168,7 +168,7 @@ impl App {
return Err("Invalid syntax mapping. The format of the -m/--map-syntax option is 'from:to'.".into()); return Err("Invalid syntax mapping. The format of the -m/--map-syntax option is 'from:to'.".into());
} }
syntax_mapping.insert(parts[0].into(), parts[1].into()); syntax_mapping.insert(parts[0], parts[1]);
} }
} }

View File

@ -9,24 +9,24 @@ impl SyntaxMapping {
SyntaxMapping(HashMap::new()) SyntaxMapping(HashMap::new())
} }
pub fn insert(&mut self, from: String, to: String) -> Option<String> { pub fn insert(&mut self, from: impl Into<String>, to: impl Into<String>) -> Option<String> {
self.0.insert(from, to) self.0.insert(from.into(), to.into())
} }
pub fn replace<'a>(&self, input: &'a str) -> Cow<'a, str> { pub fn replace<'a>(&self, input: impl Into<Cow<'a, str>>) -> Cow<'a, str> {
let mut out = Cow::from(input); let input = input.into();
if let Some(value) = self.0.get(input) { match self.0.get(input.as_ref()) {
out = Cow::from(value.clone()) Some(s) => Cow::from(s.clone()),
None => input,
} }
out
} }
} }
#[test] #[test]
fn basic() { fn basic() {
let mut map = SyntaxMapping::new(); let mut map = SyntaxMapping::new();
map.insert("Cargo.lock".into(), "toml".into()); map.insert("Cargo.lock", "toml");
map.insert(".ignore".into(), ".gitignore".into()); map.insert(".ignore", ".gitignore");
assert_eq!("toml", map.replace("Cargo.lock")); assert_eq!("toml", map.replace("Cargo.lock"));
assert_eq!("other.lock", map.replace("other.lock")); assert_eq!("other.lock", map.replace("other.lock"));