on master help sign in
Enhance SyntaxMapping with impl Trait
commit348c9f3562524d3931cbaf884fae1fe7763a574c authorOle Martin Ruud <barskern@outlook.com> dateTue 27 Nov 2018, 04:40 (8 years ago) parent6b6a8f8
2 files changed+10−10
src/app.rs+1−1 src/syntax_mapping.rs+9−9
src/app.rs+1−1
@@ -168,7 +168,7 @@ impl App {
168168 return Err("Invalid syntax mapping. The format of the -m/--map-syntax option is 'from:to'.".into());
169169 }
170170
171− syntax_mapping.insert(parts[0].into(), parts[1].into());
171+ syntax_mapping.insert(parts[0], parts[1]);
172172 }
173173 }
174174
src/syntax_mapping.rs+9−9
@@ -9,24 +9,24 @@ impl SyntaxMapping {
99 SyntaxMapping(HashMap::new())
1010 }
1111
12− pub fn insert(&mut self, from: String, to: String) -> Option<String> {
13− self.0.insert(from, to)
12+ pub fn insert(&mut self, from: impl Into<String>, to: impl Into<String>) -> Option<String> {
13+ self.0.insert(from.into(), to.into())
1414 }
1515
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())
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,
2021 }
21− out
2222 }
2323}
2424
2525#[test]
2626fn basic() {
2727 let mut map = SyntaxMapping::new();
28− map.insert("Cargo.lock".into(), "toml".into());
29− map.insert(".ignore".into(), ".gitignore".into());
28+ map.insert("Cargo.lock", "toml");
29+ map.insert(".ignore", ".gitignore");
3030
3131 assert_eq!("toml", map.replace("Cargo.lock"));
3232 assert_eq!("other.lock", map.replace("other.lock"));