From 8008f27518da25c647282bdc33d6638cd35a65ef Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Tue, 28 Apr 2020 00:03:40 +0800 Subject: [PATCH] fix clippy --- Cargo.lock | 6 +-- Cargo.toml | 2 +- src/main.rs | 124 +++++++++++++++++++++------------------------------- 3 files changed, 54 insertions(+), 78 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b89128c..e33d055 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,7 +130,7 @@ name = "finding" version = "0.1.3" dependencies = [ "argparse 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rust_util 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rust_util 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -237,7 +237,7 @@ dependencies = [ [[package]] name = "rust_util" -version = "0.1.0" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -357,7 +357,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" "checksum redox_users 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ecedbca3bf205f8d8f5c2b44d83cd0690e39ee84b951ed649e9f1841132b66d" "checksum rust-argon2 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ca4eaef519b494d1f2848fc602d18816fed808a981aedf4f1f00ceb7c9d32cf" -"checksum rust_util 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5dc9b9d45cf4e9a8a28c1c622f0a2bba9af88b85ec04c494d80605a717c1c022" +"checksum rust_util 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd9973675144c03fdf6f9a559ea5c254b0008e90a8992b1f317f728edd3512bc" "checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" "checksum syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238" "checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545" diff --git a/Cargo.toml b/Cargo.toml index fbe0863..6262770 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,4 @@ license = "MIT" argparse = "0.2.2" term = "0.5.2" term_size = "0.3.1" -rust_util = "0.1.0" +rust_util = "0.2.2" diff --git a/src/main.rs b/src/main.rs index c8cf2e5..04b8a98 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,13 +14,14 @@ use std::{ use opt::*; use rust_util::{ + iff, XResult, new_box_error, util_file::*, util_size::*, util_msg::*, }; -use local_util::*; +use local_util::read_file_content; const EMPTY: &str = ""; const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -52,7 +53,6 @@ fn find_huge_files(options: &Options, dir_path: &Path) { clear_n_print_message(MessageType::WARN, &format!("Read file {} meta failed: {}", p_str, err)); } } - return; }, Ok(metadata) => { let len = metadata.len(); @@ -91,11 +91,8 @@ struct MatchLine { } impl MatchLine { - fn new(l_no: usize, l_str: String) -> MatchLine { - MatchLine { - line_number: l_no, - line_string: l_str, - } + fn new(line_number: usize, line_string: String) -> MatchLine { + MatchLine { line_number, line_string, } } } @@ -104,10 +101,7 @@ fn match_lines(tag: &str, content: &str, options: &Options) -> bool { let lines = content.lines(); let mut match_lines_vec = vec![]; let mut l_no = 0usize; - let the_search_text = &match options.ignore_case { - true => search_text.to_lowercase(), - false => search_text.to_string(), - }; + let the_search_text = &iff!(options.ignore_case, search_text.to_lowercase(), search_text.to_string()); for ln in lines { if options.filter_large_line && ln.len() as u64 >= options.parsed_large_line_size { if options.verbose { @@ -115,45 +109,39 @@ fn match_lines(tag: &str, content: &str, options: &Options) -> bool { } continue; } - let matches = match options.ignore_case { - true => ln.to_lowercase().contains(the_search_text), - false => ln.contains(the_search_text), + let matches = iff!(options.ignore_case, ln.to_lowercase().contains(the_search_text), ln.contains(the_search_text)); + let matches_line_content = match &options.filter_line_content { + c if c.is_empty() => true, + c => ln.contains(c), }; - let mut matches_line_content = true; - if !options.filter_line_content.is_empty(){ - if ! ln.contains(options.filter_line_content.as_str()) { - matches_line_content = false; - } - } if matches && matches_line_content { match_lines_vec.push(MatchLine::new(l_no, ln.to_string())); } l_no += 1; } - let mut matches_any = false; - if !match_lines_vec.is_empty() { + if match_lines_vec.is_empty() { + false + } else { print_lastline(EMPTY); print_message(MessageType::OK, &format!("Find in {}:", tag)); - for i in 0..match_lines_vec.len() { - print!("{}: ", match_lines_vec[i].line_number + 1); - match options.ignore_case { - true => println!("{}", match_lines_vec[i].line_string), - false => { - let ss: Vec<&str> = match_lines_vec[i].line_string.split(search_text).collect(); - for j in 0..ss.len() { - print!("{}", ss[j]); - if j < ss.len() -1 { - print_color(Some(term::color::RED), true, search_text); - } + for match_line in &match_lines_vec { + print!("{}: ", match_line.line_number + 1); + if options.ignore_case { + println!("{}", match_line.line_string); + } else { + let ss: Vec<&str> = match_line.line_string.split(search_text).collect(); + for j in 0..ss.len() { + print!("{}", ss[j]); + if j < ss.len() -1 { + print_color(Some(term::color::RED), true, search_text); } - println!(); - }, + } + println!(); } } - matches_any = true; + true } - matches_any } fn find_text_files(options: &Options, dir_path: &Path) { @@ -166,9 +154,7 @@ fn find_text_files(options: &Options, dir_path: &Path) { } let file_exts = match options.file_ext.as_str() { "" => vec![], - ext => { - ext.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).map(|s| String::from(".") + s).collect() - }, + ext => ext.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).map(|s| ".".to_owned() + s).collect(), }; let total_file_count_cell = RefCell::new(0u64); let scaned_file_count_cell = RefCell::new(0u64); @@ -178,8 +164,7 @@ fn find_text_files(options: &Options, dir_path: &Path) { walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| { total_file_count_cell.replace_with(|&mut c| c + 1); let p_str = match p.to_str() { - None => return, - Some(s) => s, + Some(s) => s, None => return, }; if !file_exts.is_empty() { let mut file_ext_matches = false; @@ -189,23 +174,20 @@ fn find_text_files(options: &Options, dir_path: &Path) { break; } } - if ! file_ext_matches { + if !file_ext_matches { return; } } - if !options.filter_file_name.is_empty() { - if ! p_str.contains(options.filter_file_name.as_str()) { - return; - } + if !options.filter_file_name.is_empty() && !p_str.contains(options.filter_file_name.as_str()) { + return; } let file_content = match read_file_content(p, options.parsed_large_text_file_size) { - Err(err) => { + Ok(c) => c, Err(err) => { if options.verbose { clear_n_print_message(MessageType::WARN, &format!("Read file {} failed: {}", p_str, err)); } return; }, - Ok(c) => c, }; scaned_file_count_cell.replace_with(|&mut c| c + 1); if match_lines(p_str, &file_content, &options) { @@ -213,27 +195,24 @@ fn find_text_files(options: &Options, dir_path: &Path) { } }, &|p| { total_dir_count_cell.replace_with(|&mut c| c + 1); - match p.to_str() { - None => (), - Some(p_str) => { - if (! options.scan_dot_git) && p_str.ends_with("/.git") { - if options.verbose { - clear_n_print_message(MessageType::INFO, &format!("Skip .git dir: {}", p_str)); - } - return false; + if let Some(p_str) = p.to_str() { + if (!options.scan_dot_git) && p_str.ends_with("/.git") { + if options.verbose { + clear_n_print_message(MessageType::INFO, &format!("Skip .git dir: {}", p_str)); } - if options.skip_dot_dir && p_str.contains("/.") { - return false; + return false; + } + if options.skip_dot_dir && p_str.contains("/.") { + return false; + } + if options.skip_link_dir && is_symlink(p) { + if options.verbose { + clear_n_print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str)); } - if options.skip_link_dir && is_symlink(p) { - if options.verbose { - clear_n_print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str)); - } - return false; - } - scaned_dir_count_cell.replace_with(|&mut c| c + 1); - print_lastline(&get_term_width_message(&format!("Scanning: {}", p_str), 10)) - }, + return false; + } + scaned_dir_count_cell.replace_with(|&mut c| c + 1); + print_lastline(&get_term_width_message(&format!("Scanning: {}", p_str), 10)); } true }).unwrap_or(()); @@ -256,17 +235,14 @@ fn main() -> XResult<()> { } let dir_path = match get_absolute_path(&options.dir) { - Some(path) => path, None => { - return Err(new_box_error(&format!("Cannot find dir: {}", options.dir))); - }, + Some(path) => path, + None => return Err(new_box_error(&format!("Cannot find dir: {}", options.dir))), }; let start = SystemTime::now(); match options.target.as_str() { "huge" | "hugefile" => find_huge_files(&options, &dir_path), "text" => find_text_files(&options, &dir_path), - unknown => { - return Err(new_box_error(&format!("Unknown command: {}", unknown))); - }, + others => return Err(new_box_error(&format!("Unknown command: {}", others))), } let cost_millis = SystemTime::now().duration_since(start.clone()).unwrap().as_millis(); print_message(MessageType::OK, &format!("Finding finished, cost {} ms", cost_millis));