From 8c3c8efa428200c67c1a6fa731faee55adf19678 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 6 Jul 2023 14:05:39 +0200 Subject: cleanup with clippy --- src/dictionary.rs | 9 +++++++-- src/extractors/pdf.rs | 2 +- src/filecache.rs | 2 +- src/index.rs | 13 ++++++------- src/text/pdf.rs | 2 +- src/vector.rs | 10 ++++++++-- 6 files changed, 24 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/dictionary.rs b/src/dictionary.rs index b522c9e..e0a561a 100644 --- a/src/dictionary.rs +++ b/src/dictionary.rs @@ -6,6 +6,11 @@ pub struct Dictionary { data : HashMap, } +impl Default for Dictionary { + fn default() -> Self { + Self::new() + } +} impl Dictionary { pub fn new() -> Self { @@ -25,9 +30,9 @@ impl Dictionary { } pub fn set(&mut self, name : String) { - if !self.data.contains_key(&name) { + if let std::collections::hash_map::Entry::Vacant(e) = self.data.entry(name) { self.last_index += 1; - self.data.insert(name, self.last_index as u64); + e.insert(self.last_index as u64); } } diff --git a/src/extractors/pdf.rs b/src/extractors/pdf.rs index c08c75c..b7f65d5 100644 --- a/src/extractors/pdf.rs +++ b/src/extractors/pdf.rs @@ -127,7 +127,7 @@ fn get_pdf_text(doc: &Document) -> Result { } pub fn pdf2text(path: &str) -> Result { - let doc = load_pdf(&path)?; + let doc = load_pdf(path)?; if doc.is_encrypted() { return Ok("".to_string()); } diff --git a/src/filecache.rs b/src/filecache.rs index 5e9d324..287cea0 100644 --- a/src/filecache.rs +++ b/src/filecache.rs @@ -9,7 +9,7 @@ impl FileCache { pub fn from_line(line : String) -> Self { let ls : Vec = line.split(',').map(|s| s.to_string()).collect(); let v = FileVector::from_string(ls[1].clone()); - let p = ls[0].clone().replace("\0", ","); + let p = ls[0].clone().replace('\0', ","); Self { vector : v, path : p diff --git a/src/index.rs b/src/index.rs index 4fabe32..8c358d3 100644 --- a/src/index.rs +++ b/src/index.rs @@ -52,8 +52,8 @@ impl Index { entry.path() .to_str() .unwrap() - .replace(",", "\0"), - fv.to_string() + .replace(',', "\0"), + fv.stringify() ).ok(); filecache.push(FileCache { @@ -95,8 +95,8 @@ impl Index { for line in reader.lines() { let l = line.unwrap(); - if l.starts_with("#") { - dict = Dictionary::from_line(&l.strip_prefix("#").unwrap()); + if l.starts_with('#') { + dict = Dictionary::from_line(l.strip_prefix('#').unwrap()); } else { filecache.push(FileCache::from_line(l)); } @@ -112,9 +112,8 @@ impl Index { let mut v : FileVector = FileVector::new(); for arg in search_args { - match self.dictionary.get(arg.to_string()) { - Some(value) => { v.insert(*value, 1); } - None => {} + if let Some(value) = self.dictionary.get(arg.to_string()) { + v.insert(*value, 1); } } diff --git a/src/text/pdf.rs b/src/text/pdf.rs index efa441f..4413b78 100644 --- a/src/text/pdf.rs +++ b/src/text/pdf.rs @@ -1,5 +1,5 @@ use crate::extractors::pdf; pub fn get_text(path : &str) -> String { - pdf::pdf2text(path).ok().unwrap_or_else(|| "".to_string()) + pdf::pdf2text(path).ok().unwrap_or_default() } diff --git a/src/vector.rs b/src/vector.rs index 366812f..56d1817 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -18,6 +18,12 @@ impl DerefMut for FileVector { } } +impl Default for FileVector { + fn default() -> Self { + Self::new() + } +} + impl FileVector { pub fn new() -> Self { Self { data : HashMap::new() } @@ -39,7 +45,7 @@ impl FileVector { Self { data } } - pub fn to_string(&self) -> String { + pub fn stringify(&self) -> String { let mut hex = String::new(); for (i, v) in self.data.iter() { @@ -53,7 +59,7 @@ impl FileVector { pub fn scalar_product(a : &FileVector, b : &FileVector) -> u64 { let mut c = 0; for (i, x) in a.iter() { - c += x * (b.get(i).unwrap_or_else(|| &0)); + c += x * (b.get(i).unwrap_or(&0)); } c } -- cgit v1.2.3-70-g09d2