diff options
author | Ole Martin Ruud <barskern@outlook.com> | 2018-11-27 04:40:54 +0100 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2018-11-27 20:36:23 +0100 |
commit | 348c9f3562524d3931cbaf884fae1fe7763a574c (patch) | |
tree | 435a23755bd89c78a9a9815737d40bb7da05c21a | |
parent | 6b6a8f8e160e3dc0ca887253429623b0360913cf (diff) |
Enhance SyntaxMapping with impl Trait
-rw-r--r-- | src/app.rs | 2 | ||||
-rw-r--r-- | src/syntax_mapping.rs | 18 |
2 files changed, 10 insertions, 10 deletions
@@ -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")); |