1
0
mirror of https://github.com/jht5945/finding.git synced 2025-12-27 13:20:03 +08:00

fix clippy

This commit is contained in:
2020-04-28 00:03:40 +08:00
parent be4a25f0c7
commit 8008f27518
3 changed files with 54 additions and 78 deletions

6
Cargo.lock generated
View File

@@ -130,7 +130,7 @@ name = "finding"
version = "0.1.3" version = "0.1.3"
dependencies = [ dependencies = [
"argparse 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "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 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)", "term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@@ -237,7 +237,7 @@ dependencies = [
[[package]] [[package]]
name = "rust_util" name = "rust_util"
version = "0.1.0" version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "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_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 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-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 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 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" "checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545"

View File

@@ -12,4 +12,4 @@ license = "MIT"
argparse = "0.2.2" argparse = "0.2.2"
term = "0.5.2" term = "0.5.2"
term_size = "0.3.1" term_size = "0.3.1"
rust_util = "0.1.0" rust_util = "0.2.2"

View File

@@ -14,13 +14,14 @@ use std::{
use opt::*; use opt::*;
use rust_util::{ use rust_util::{
iff,
XResult, XResult,
new_box_error, new_box_error,
util_file::*, util_file::*,
util_size::*, util_size::*,
util_msg::*, util_msg::*,
}; };
use local_util::*; use local_util::read_file_content;
const EMPTY: &str = ""; const EMPTY: &str = "";
const VERSION: &str = env!("CARGO_PKG_VERSION"); 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)); clear_n_print_message(MessageType::WARN, &format!("Read file {} meta failed: {}", p_str, err));
} }
} }
return;
}, },
Ok(metadata) => { Ok(metadata) => {
let len = metadata.len(); let len = metadata.len();
@@ -91,11 +91,8 @@ struct MatchLine {
} }
impl MatchLine { impl MatchLine {
fn new(l_no: usize, l_str: String) -> MatchLine { fn new(line_number: usize, line_string: String) -> MatchLine {
MatchLine { MatchLine { line_number, line_string, }
line_number: l_no,
line_string: l_str,
}
} }
} }
@@ -104,10 +101,7 @@ fn match_lines(tag: &str, content: &str, options: &Options) -> bool {
let lines = content.lines(); let lines = content.lines();
let mut match_lines_vec = vec![]; let mut match_lines_vec = vec![];
let mut l_no = 0usize; let mut l_no = 0usize;
let the_search_text = &match options.ignore_case { let the_search_text = &iff!(options.ignore_case, search_text.to_lowercase(), search_text.to_string());
true => search_text.to_lowercase(),
false => search_text.to_string(),
};
for ln in lines { for ln in lines {
if options.filter_large_line && ln.len() as u64 >= options.parsed_large_line_size { if options.filter_large_line && ln.len() as u64 >= options.parsed_large_line_size {
if options.verbose { if options.verbose {
@@ -115,45 +109,39 @@ fn match_lines(tag: &str, content: &str, options: &Options) -> bool {
} }
continue; continue;
} }
let matches = match options.ignore_case { let matches = iff!(options.ignore_case, ln.to_lowercase().contains(the_search_text), ln.contains(the_search_text));
true => ln.to_lowercase().contains(the_search_text), let matches_line_content = match &options.filter_line_content {
false => ln.contains(the_search_text), 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 { if matches && matches_line_content {
match_lines_vec.push(MatchLine::new(l_no, ln.to_string())); match_lines_vec.push(MatchLine::new(l_no, ln.to_string()));
} }
l_no += 1; 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_lastline(EMPTY);
print_message(MessageType::OK, &format!("Find in {}:", tag)); print_message(MessageType::OK, &format!("Find in {}:", tag));
for i in 0..match_lines_vec.len() { for match_line in &match_lines_vec {
print!("{}: ", match_lines_vec[i].line_number + 1); print!("{}: ", match_line.line_number + 1);
match options.ignore_case { if options.ignore_case {
true => println!("{}", match_lines_vec[i].line_string), println!("{}", match_line.line_string);
false => { } else {
let ss: Vec<&str> = match_lines_vec[i].line_string.split(search_text).collect(); let ss: Vec<&str> = match_line.line_string.split(search_text).collect();
for j in 0..ss.len() { for j in 0..ss.len() {
print!("{}", ss[j]); print!("{}", ss[j]);
if j < ss.len() -1 { if j < ss.len() -1 {
print_color(Some(term::color::RED), true, search_text); 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) { 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() { let file_exts = match options.file_ext.as_str() {
"" => vec![], "" => vec![],
ext => { ext => ext.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).map(|s| ".".to_owned() + s).collect(),
ext.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).map(|s| String::from(".") + s).collect()
},
}; };
let total_file_count_cell = RefCell::new(0u64); let total_file_count_cell = RefCell::new(0u64);
let scaned_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| { walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| {
total_file_count_cell.replace_with(|&mut c| c + 1); total_file_count_cell.replace_with(|&mut c| c + 1);
let p_str = match p.to_str() { let p_str = match p.to_str() {
None => return, Some(s) => s, None => return,
Some(s) => s,
}; };
if !file_exts.is_empty() { if !file_exts.is_empty() {
let mut file_ext_matches = false; let mut file_ext_matches = false;
@@ -189,23 +174,20 @@ fn find_text_files(options: &Options, dir_path: &Path) {
break; break;
} }
} }
if ! file_ext_matches { if !file_ext_matches {
return; return;
} }
} }
if !options.filter_file_name.is_empty() { if !options.filter_file_name.is_empty() && !p_str.contains(options.filter_file_name.as_str()) {
if ! p_str.contains(options.filter_file_name.as_str()) { return;
return;
}
} }
let file_content = match read_file_content(p, options.parsed_large_text_file_size) { let file_content = match read_file_content(p, options.parsed_large_text_file_size) {
Err(err) => { Ok(c) => c, Err(err) => {
if options.verbose { if options.verbose {
clear_n_print_message(MessageType::WARN, &format!("Read file {} failed: {}", p_str, err)); clear_n_print_message(MessageType::WARN, &format!("Read file {} failed: {}", p_str, err));
} }
return; return;
}, },
Ok(c) => c,
}; };
scaned_file_count_cell.replace_with(|&mut c| c + 1); scaned_file_count_cell.replace_with(|&mut c| c + 1);
if match_lines(p_str, &file_content, &options) { if match_lines(p_str, &file_content, &options) {
@@ -213,27 +195,24 @@ fn find_text_files(options: &Options, dir_path: &Path) {
} }
}, &|p| { }, &|p| {
total_dir_count_cell.replace_with(|&mut c| c + 1); total_dir_count_cell.replace_with(|&mut c| c + 1);
match p.to_str() { if let Some(p_str) = p.to_str() {
None => (), if (!options.scan_dot_git) && p_str.ends_with("/.git") {
Some(p_str) => { if options.verbose {
if (! options.scan_dot_git) && p_str.ends_with("/.git") { clear_n_print_message(MessageType::INFO, &format!("Skip .git dir: {}", p_str));
if options.verbose {
clear_n_print_message(MessageType::INFO, &format!("Skip .git dir: {}", p_str));
}
return false;
} }
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) { return false;
if options.verbose { }
clear_n_print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str)); 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 true
}).unwrap_or(()); }).unwrap_or(());
@@ -256,17 +235,14 @@ fn main() -> XResult<()> {
} }
let dir_path = match get_absolute_path(&options.dir) { let dir_path = match get_absolute_path(&options.dir) {
Some(path) => path, None => { Some(path) => path,
return Err(new_box_error(&format!("Cannot find dir: {}", options.dir))); None => return Err(new_box_error(&format!("Cannot find dir: {}", options.dir))),
},
}; };
let start = SystemTime::now(); let start = SystemTime::now();
match options.target.as_str() { match options.target.as_str() {
"huge" | "hugefile" => find_huge_files(&options, &dir_path), "huge" | "hugefile" => find_huge_files(&options, &dir_path),
"text" => find_text_files(&options, &dir_path), "text" => find_text_files(&options, &dir_path),
unknown => { others => return Err(new_box_error(&format!("Unknown command: {}", others))),
return Err(new_box_error(&format!("Unknown command: {}", unknown)));
},
} }
let cost_millis = SystemTime::now().duration_since(start.clone()).unwrap().as_millis(); let cost_millis = SystemTime::now().duration_since(start.clone()).unwrap().as_millis();
print_message(MessageType::OK, &format!("Finding finished, cost {} ms", cost_millis)); print_message(MessageType::OK, &format!("Finding finished, cost {} ms", cost_millis));