style: code style
This commit is contained in:
132
src/main.rs
132
src/main.rs
@@ -19,54 +19,17 @@ use crate::token::Token;
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
let matches = App::new("Cargo-todo")
|
||||
.author("Clément Guiton <clement.guiton.dev@gmail.com>")
|
||||
.about("cargo tool to find TODOs in your code")
|
||||
.arg(Arg::with_name("inline")
|
||||
.short("i")
|
||||
.long("inline")
|
||||
.value_name("inline")
|
||||
.help("display todos in one line")
|
||||
.takes_value(false))
|
||||
.arg(Arg::with_name("filter")
|
||||
.help("Filter todos to show")
|
||||
.short("f")
|
||||
.long("filter")
|
||||
.takes_value(true)
|
||||
.multiple(true)
|
||||
.min_values(1))
|
||||
.arg(Arg::with_name("verbose")
|
||||
.short("v")
|
||||
.long("verbose")
|
||||
.multiple(true)
|
||||
.help("Sets the level of verbosity"))
|
||||
.arg(Arg::with_name("exclude")
|
||||
.short("x")
|
||||
.long("exclude")
|
||||
.takes_value(true)
|
||||
.multiple(true)
|
||||
.help("Exclude some todos from the list"))
|
||||
.arg(Arg::with_name("list")
|
||||
.short("l")
|
||||
.long("list")
|
||||
.takes_value(true)
|
||||
.help("Number of values to display"))
|
||||
.arg(Arg::with_name("sort")
|
||||
.short("s")
|
||||
.long("sort")
|
||||
.takes_value(true)
|
||||
.possible_values(&["priority", "deadline", "member"])
|
||||
.help("Sort todos"))
|
||||
.arg(Arg::with_name("member")
|
||||
.short("m")
|
||||
.long("member")
|
||||
.takes_value(true)
|
||||
.multiple(true)
|
||||
.min_values(1)
|
||||
.help("Filter from member"))
|
||||
.subcommand(SubCommand::with_name("legacy")
|
||||
.about("Launch program in legacy mode (supports todo!(), etc..."))
|
||||
.get_matches();
|
||||
|
||||
.author("Clément Guiton <clement.guiton.dev@gmail.com>")
|
||||
.about("cargo tool to find TODOs in your code")
|
||||
.arg(Arg::with_name("inline").short("i").long("inline").value_name("inline").takes_value(false).help("display todos in one line"))
|
||||
.arg(Arg::with_name("filter").short("f").long("filter").takes_value(true).multiple(true).min_values(1).help("Filter todos to show"))
|
||||
.arg(Arg::with_name("verbose").short("v").long("verbose").multiple(true).help("Sets the level of verbosity"))
|
||||
.arg(Arg::with_name("exclude").short("x").long("exclude").takes_value(true).multiple(true).help("Exclude some todos from the list"))
|
||||
.arg(Arg::with_name("list").short("l").long("list").takes_value(true).help("Number of values to display"))
|
||||
.arg(Arg::with_name("sort").short("s").long("sort").takes_value(true).possible_values(&["priority", "deadline", "member"]).help("Sort todos"))
|
||||
.arg(Arg::with_name("member").short("m").long("member").takes_value(true).multiple(true).min_values(1).help("Filter from member"))
|
||||
.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<Parser> = vec!();
|
||||
@@ -75,21 +38,21 @@ fn main() -> std::io::Result<()> {
|
||||
path.push_str("/**/*.rs");
|
||||
|
||||
//we add a parser looking for the //todo keyword
|
||||
parsers.push(Parser::new(String::from("//todo"), Box::from(|x : Vec<char>| {if x.last().unwrap() == &'\n' {return true;} else { return false}})));
|
||||
parsers.push(Parser::new(String::from("//todo"), Box::from(|x : Vec<char>| 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<char>| {if x.last().unwrap() == &')' {return true;} else { return false}}), _todo_macro_callback));
|
||||
parsers.push(Parser::new_callback(String::from("todo!("), Box::from(|x : Vec<char>| 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<char>| {if x.last().unwrap() == &')' {return true;} else { return false}}), _unimplemented_macro_callback));
|
||||
parsers.push(Parser::new_callback(String::from("unimplemented!("), Box::from(|x : Vec<char>| x.last().unwrap() == &')'), _unimplemented_macro_callback));
|
||||
|
||||
parsers.push(Parser::new(String::from("//fix"), Box::from(|x : Vec<char>| {if x.last().unwrap() == &'\n' {return true;} else { return false}})));
|
||||
parsers.push(Parser::new(String::from("//fix"), Box::from(|x : Vec<char>| x.last().unwrap() == &'\n')));
|
||||
|
||||
|
||||
//loop on every file within the current dir
|
||||
@@ -176,45 +139,25 @@ fn main() -> std::io::Result<()> {
|
||||
Err(e) => eprintln!{"{}", e},
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if matches.is_present("sort") {
|
||||
if matches.value_of("sort").unwrap() == "priority" {
|
||||
fn token_priority_sort(t : &Token) -> String {
|
||||
|
||||
if t.priority.is_none() {
|
||||
return String::from("z");
|
||||
}
|
||||
else {
|
||||
return t.priority.clone().unwrap()
|
||||
}
|
||||
fn token_priority_sort(t : &Token) -> String {
|
||||
t.priority.clone().unwrap_or_else(|| "z".into())
|
||||
}
|
||||
tokens.sort_unstable_by_key(token_priority_sort);
|
||||
}
|
||||
else if matches.value_of("sort").unwrap() == "deadline" {
|
||||
fn token_deadline_sort(t : &Token) -> NaiveDate {
|
||||
|
||||
if t.date.is_none() {
|
||||
return NaiveDate::from_ymd(3000,01,01);
|
||||
}
|
||||
else {
|
||||
return t.date.clone().unwrap()
|
||||
}
|
||||
fn token_deadline_sort(t : &Token) -> NaiveDate {
|
||||
t.date.clone().unwrap_or_else(|| NaiveDate::from_ymd(3000, 1, 1))
|
||||
}
|
||||
tokens.sort_unstable_by_key(token_deadline_sort);
|
||||
}
|
||||
else if matches.value_of("sort").unwrap() == "member" {
|
||||
fn token_member_sort(t : &Token) -> String {
|
||||
|
||||
if t.priority.is_none() {
|
||||
return String::from("z");
|
||||
}
|
||||
else {
|
||||
return t.priority.clone().unwrap()
|
||||
}
|
||||
fn token_member_sort(t : &Token) -> String {
|
||||
t.priority.clone().unwrap_or_else(|| "z".into())
|
||||
}
|
||||
tokens.sort_unstable_by_key(token_member_sort);
|
||||
}
|
||||
@@ -226,28 +169,29 @@ fn main() -> std::io::Result<()> {
|
||||
Err(_) => {
|
||||
eprintln!("{}", "list argument should be a valid number!".red());
|
||||
panic!()
|
||||
}};
|
||||
|
||||
let mut new_tokens : Vec<Token> = Vec::new();
|
||||
for i in tokens{
|
||||
if new_tokens.len() < lines {
|
||||
&new_tokens.push(i.clone());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let mut new_tokens : Vec<Token> = vec![];
|
||||
for i in tokens {
|
||||
if new_tokens.len() < lines {
|
||||
new_tokens.push(i.clone());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
tokens = new_tokens;
|
||||
}
|
||||
|
||||
if matches.is_present("member") {
|
||||
let filters : Vec<&str> = matches.values_of("member").unwrap().collect();
|
||||
let mut new_tokens : Vec<Token> = Vec::new();
|
||||
let mut new_tokens : Vec<Token> = vec![];
|
||||
for i in tokens{
|
||||
// println!("{}", i);
|
||||
for y in &filters {
|
||||
if i.member.clone().is_some() && i.member.clone().unwrap() == *y.to_string(){
|
||||
println!("pushing");
|
||||
&new_tokens.push(i.clone());
|
||||
new_tokens.push(i.clone());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -257,11 +201,11 @@ fn main() -> std::io::Result<()> {
|
||||
|
||||
if matches.is_present("filter") {
|
||||
let filters : Vec<&str> = matches.values_of("filter").unwrap().collect();
|
||||
let mut new_tokens : Vec<Token> = Vec::new();
|
||||
let mut new_tokens : Vec<Token> = vec![];
|
||||
for i in tokens {
|
||||
for y in &filters {
|
||||
if i.keyword == String::from(*y){
|
||||
&new_tokens.push(i.clone());
|
||||
if i.keyword == *y {
|
||||
new_tokens.push(i.clone());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -275,11 +219,11 @@ fn main() -> std::io::Result<()> {
|
||||
let mut new_tokens : Vec<Token> = vec![];
|
||||
for i in tokens{
|
||||
for y in 0..excludes.len() {
|
||||
if i.keyword == String::from(excludes[y]) {
|
||||
if i.keyword == excludes[y] {
|
||||
break;
|
||||
}
|
||||
else if y == excludes.len() -1{
|
||||
&new_tokens.push(i.clone());
|
||||
new_tokens.push(i.clone());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,11 +25,11 @@ impl Parser {
|
||||
self.keyword.clone()
|
||||
}
|
||||
|
||||
fn get_end_filter(&self) -> &Box<dyn Fn(Vec<char>) -> bool> {
|
||||
fn get_end_filter(&self) -> &dyn Fn(Vec<char>) -> bool {
|
||||
&self.end_filter
|
||||
}
|
||||
|
||||
fn get_callback(&self) -> &Box<dyn Fn(String, usize, &str)> {
|
||||
fn get_callback(&self) -> &dyn Fn(String, usize, &str) {
|
||||
&self.callback
|
||||
}
|
||||
|
||||
|
||||
@@ -14,9 +14,10 @@ fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>> wher
|
||||
pub fn regex_parser(path : &str, regex : Vec<String>, verbosity : i8) -> Result<Vec<Token>, io::Error> {
|
||||
let set = RegexSet::new(regex).unwrap();
|
||||
let mut tokens = vec![];
|
||||
let mut line_cpt = 0;
|
||||
for line in read_lines(path)? {
|
||||
line_cpt +=1;
|
||||
// let mut line_cpt = 0;
|
||||
for (line_cpt, line) in (read_lines(path)?).enumerate() { // ? PLUS 1
|
||||
// for line in read_lines(path)? {
|
||||
// line_cpt +=1;
|
||||
let line = line.unwrap();
|
||||
if set.is_match(line.to_lowercase().as_str()) {
|
||||
tokens.push(Token::new(path.to_string(), line_cpt, line, verbosity));
|
||||
|
||||
@@ -47,7 +47,7 @@ impl Token {
|
||||
} else if number_regex.is_match(fields[i]) {
|
||||
t.priority = Some(fields[i].to_string());
|
||||
} else if date_regex.is_match(fields[i]) {
|
||||
let date : Vec<&str> = fields[i].split("/").collect();
|
||||
let date : Vec<&str> = fields[i].split('/').collect();
|
||||
t.date = NaiveDate::from_ymd_opt(date[0].parse::<i32>().unwrap(), date[1].parse::<u32>().unwrap(), date[2].parse::<u32>().unwrap());
|
||||
// t.date = Some(fields[i].to_string());
|
||||
} else if member_regex.is_match(fields[i]) {
|
||||
|
||||
Reference in New Issue
Block a user