From 348c9f3562524d3931cbaf884fae1fe7763a574c Mon Sep 17 00:00:00 2001
From: Ole Martin Ruud <barskern@outlook.com>
Date: Tue, 27 Nov 2018 04:40:54 +0100
Subject: [PATCH] Enhance SyntaxMapping with impl Trait

---
 src/app.rs            |  2 +-
 src/syntax_mapping.rs | 18 +++++++++---------
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/app.rs b/src/app.rs
index 1c888e21..b0b573cf 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -168,7 +168,7 @@ impl App {
                     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]);
             }
         }
 
diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs
index bf0e7351..2b32224f 100644
--- a/src/syntax_mapping.rs
+++ b/src/syntax_mapping.rs
@@ -9,24 +9,24 @@ impl SyntaxMapping {
         SyntaxMapping(HashMap::new())
     }
 
-    pub fn insert(&mut self, from: String, to: String) -> Option<String> {
-        self.0.insert(from, to)
+    pub fn insert(&mut self, from: impl Into<String>, to: impl Into<String>) -> Option<String> {
+        self.0.insert(from.into(), to.into())
     }
 
-    pub fn replace<'a>(&self, input: &'a str) -> Cow<'a, str> {
-        let mut out = Cow::from(input);
-        if let Some(value) = self.0.get(input) {
-            out = Cow::from(value.clone())
+    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,
         }
-        out
     }
 }
 
 #[test]
 fn basic() {
     let mut map = SyntaxMapping::new();
-    map.insert("Cargo.lock".into(), "toml".into());
-    map.insert(".ignore".into(), ".gitignore".into());
+    map.insert("Cargo.lock", "toml");
+    map.insert(".ignore", ".gitignore");
 
     assert_eq!("toml", map.replace("Cargo.lock"));
     assert_eq!("other.lock", map.replace("other.lock"));