From 341bd1793e6609cae62fffbca070c213d751af6c Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 26 Jul 2020 17:31:57 +0800 Subject: [PATCH] style: code style --- src/main.rs | 169 ++++++++++++++++++++++++--------------------------- src/regex.rs | 2 +- src/token.rs | 1 - 3 files changed, 79 insertions(+), 93 deletions(-) diff --git a/src/main.rs b/src/main.rs index f4f79ce..d6bc0c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,113 +32,41 @@ fn main() -> std::io::Result<()> { .subcommand(SubCommand::with_name("legacy").about("Launch program in legacy mode (supports todo!(), etc...")) .get_matches(); - if let Some(_matches) = matches.subcommand_matches("legacy") { - let mut parsers : Vec = vec![]; - - let mut path = String::from(env::current_dir().unwrap().to_str().unwrap()); - path.push_str("/**/*.rs"); - - //we add a parser looking for the //todo keyword - parsers.push(Parser::new(String::from("//todo"), Box::from(|x : Vec| x.last().unwrap() == &'\n'))); - //we add a parser looking for the todo!() token - let todo_macro_callback = Box::from(|mut text : String, line : usize, file : &str| { - text.retain(|c| c != '\"'); - println!("{} {} {} {} : {}",file,"TODO".green() ,"Line ".green(), line.to_string().green(), text.blue()); - }); - parsers.push(Parser::new_callback(String::from("todo!("), Box::from(|x : Vec| x.last().unwrap() == &')'), todo_macro_callback)); - - //support for unimplemented - let unimplemented_macro_callback = Box::from(|text : String, line : usize, file : &str| { - println!("{} {} {} {} : {}{}{} ", file, "TODO".green(), "Line ".green(), line.to_string().green(), "unimplemented!(".blue(), text.magenta(), ")".blue()); - }); - parsers.push(Parser::new_callback(String::from("unimplemented!("), Box::from(|x : Vec| x.last().unwrap() == &')'), unimplemented_macro_callback)); - - parsers.push(Parser::new(String::from("//fix"), Box::from(|x : Vec| x.last().unwrap() == &'\n'))); - - //loop on every file within the current dir - for entry in match glob(&path) { - Ok(entry) => entry, - Err(e) => { - println!("Couldn't access files. Error {}", e); - Err(e).unwrap() - } - } - - { - let path = entry.unwrap(); - let path = Path::new(&path).strip_prefix(env::current_dir().unwrap().to_str().unwrap()).unwrap(); - if !path.starts_with("target/") { - let path = path.to_str().unwrap(); - //execute each parsers on the current file - for p in &parsers { - p.parse(path); - } - } - } - - Ok(()) - } - else{ + if let Some(_) = matches.subcommand_matches("legacy") { + main_legacy() + } else { let mut tokens : Vec = Vec::new(); - - let mut path = String::from(dirs::home_dir().unwrap().to_str().unwrap()); - path.push_str("/.cargo/todo_config"); - // println!("{}",path); - fn read_lines

(filename: P) -> io::Result>> where P: AsRef { - let file = match File::open(&filename) { - Ok(line) => line, - Err(_) => { - println!("{}", "File '~/.cargo/todo_config' not found, creating it".red()); - let mut f = OpenOptions::new().write(true).read(true).create(true).open(&filename).unwrap(); - f.write_all(b"^s*//s*todo\\b\n").unwrap(); - f.write_all(b"^s*//s*fix\\b\n").unwrap(); - f.write_all(b"^s*//s*fixme\\b\n").unwrap(); - return read_lines(filename); - } - }; - Ok(BufReader::new(file).lines()) - } + let mut togo_config_path = String::from(dirs::home_dir().unwrap().to_str().unwrap()); + togo_config_path.push_str("/.cargo/todo_config"); let mut regex = vec![]; - for line in read_lines(path).unwrap() { + for line in read_lines(togo_config_path).unwrap() { let line = line.unwrap(); - regex.push(line); + if !line.is_empty() { regex.push(line) }; } - let mut path = String::from(env::current_dir().unwrap().to_str().unwrap()); - path.push_str("/**/*.rs"); + let mut all_rs_path = String::from(env::current_dir().unwrap().to_str().unwrap()); + all_rs_path.push_str("/**/*.rs"); - for entry in match glob(&path) { - Ok(entry) => entry, - Err(e) => { + for entry in match glob(&all_rs_path) { + Ok(entry) => entry, Err(e) => { println!("Couldn't access files. Error {}", e); Err(e).unwrap() } - } - - { + } { let path = entry.unwrap(); let path = Path::new(&path).strip_prefix(env::current_dir().unwrap().to_str().unwrap()).unwrap(); // println!("{}", path.to_str().unwrap()); if !path.starts_with("target/") { - let path = path.to_str().unwrap(); + let file_path = path.to_str().unwrap(); let verbose_count = matches.occurrences_of("verbose"); - if verbose_count == 0 || verbose_count == 2 { - match regex_parser(path, regex.clone(), 2) { - Ok(mut t) => { - tokens.append(&mut t); - }, - Err(e) => eprintln!{"{}", e}, - } - } - else { - match regex_parser(path, regex.clone(), 1) { - Ok(mut t) => { - tokens.append(&mut t); - }, - Err(e) => eprintln!{"{}", e}, - } + let verbosity = if verbose_count == 0 || verbose_count == 2 {2} else {1}; + match regex_parser(file_path, ®ex, verbosity) { + Ok(mut t) => { + tokens.append(&mut t); + }, + Err(e) => eprintln!{"{}", e}, } } } @@ -202,6 +130,65 @@ fn main() -> std::io::Result<()> { } } +fn read_lines

(filename: P) -> io::Result>> where P: AsRef { + let file = match File::open(&filename) { + Ok(line) => line, + Err(_) => { + println!("{}", "File '~/.cargo/todo_config' not found, creating it".red()); + let mut f = OpenOptions::new().write(true).read(true).create(true).open(&filename).unwrap(); + f.write_all(b"^s*//s*todo\\b\n").unwrap(); + f.write_all(b"^s*//s*fix\\b\n").unwrap(); + f.write_all(b"^s*//s*fixme\\b\n").unwrap(); + return read_lines(filename); + } + }; + Ok(BufReader::new(file).lines()) +} + +fn main_legacy() -> std::io::Result<()> { + let mut parsers : Vec = vec![]; + + let mut path = String::from(env::current_dir().unwrap().to_str().unwrap()); + path.push_str("/**/*.rs"); + + //we add a parser looking for the //todo keyword + parsers.push(Parser::new(String::from("//todo"), Box::from(|x : Vec| x.last().unwrap() == &'\n'))); + //we add a parser looking for the todo!() token + let todo_macro_callback = Box::from(|mut text : String, line : usize, file : &str| { + text.retain(|c| c != '\"'); + println!("{} {} {} {} : {}",file,"TODO".green() ,"Line ".green(), line.to_string().green(), text.blue()); + }); + parsers.push(Parser::new_callback(String::from("todo!("), Box::from(|x : Vec| x.last().unwrap() == &')'), todo_macro_callback)); + + //support for unimplemented + let unimplemented_macro_callback = Box::from(|text : String, line : usize, file : &str| { + println!("{} {} {} {} : {}{}{} ", file, "TODO".green(), "Line ".green(), line.to_string().green(), "unimplemented!(".blue(), text.magenta(), ")".blue()); + }); + parsers.push(Parser::new_callback(String::from("unimplemented!("), Box::from(|x : Vec| x.last().unwrap() == &')'), unimplemented_macro_callback)); + + parsers.push(Parser::new(String::from("//fix"), Box::from(|x : Vec| x.last().unwrap() == &'\n'))); + + //loop on every file within the current dir + for entry in match glob(&path) { + Ok(entry) => entry, Err(e) => { + println!("Couldn't access files. Error {}", e); + Err(e).unwrap() + } + } { + let path = entry.unwrap(); + let path = Path::new(&path).strip_prefix(env::current_dir().unwrap().to_str().unwrap()).unwrap(); + if !path.starts_with("target/") { + let path = path.to_str().unwrap(); + //execute each parsers on the current file + for p in &parsers { + p.parse(path); + } + } + } + + Ok(()) +} + #[allow(dead_code)] // test zone //TODO refactor diff --git a/src/regex.rs b/src/regex.rs index 058f196..f02211c 100644 --- a/src/regex.rs +++ b/src/regex.rs @@ -11,7 +11,7 @@ fn read_lines

(filename: P) -> io::Result>> where P: AsR Ok(BufReader::new(file).lines()) } -pub fn regex_parser(path: &str, regex: Vec, verbosity: i8) -> Result, io::Error> { +pub fn regex_parser(path: &str, regex: &[String], verbosity: i8) -> Result, io::Error> { let set = RegexSet::new(regex).unwrap(); let mut tokens = vec![]; for (line_cpt, line) in (read_lines(path)?).enumerate() { diff --git a/src/token.rs b/src/token.rs index ef51854..cbc6d5f 100644 --- a/src/token.rs +++ b/src/token.rs @@ -53,7 +53,6 @@ impl Token { t.comment = t.comment.map_or_else(|| Some(field.to_string()), |comment| Some(format!("{} {}", comment, field))); } } - t }