style: code style

This commit is contained in:
2020-07-26 17:31:57 +08:00
parent 55086b56b9
commit 341bd1793e
3 changed files with 79 additions and 93 deletions

View File

@@ -32,115 +32,43 @@ 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<Parser> = 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<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>| 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>| x.last().unwrap() == &')'), unimplemented_macro_callback));
parsers.push(Parser::new(String::from("//fix"), Box::from(|x : Vec<char>| 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<Token> = 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<P>(filename: P) -> io::Result<Lines<BufReader<File>>> where P: AsRef<Path> {
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) {
let verbosity = if verbose_count == 0 || verbose_count == 2 {2} else {1};
match regex_parser(file_path, &regex, verbosity) {
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},
}
}
}
}
if let Some(sort) = matches.value_of("sort") {
@@ -202,6 +130,65 @@ fn main() -> std::io::Result<()> {
}
}
fn read_lines<P>(filename: P) -> io::Result<Lines<BufReader<File>>> where P: AsRef<Path> {
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<Parser> = 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<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>| 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>| x.last().unwrap() == &')'), unimplemented_macro_callback));
parsers.push(Parser::new(String::from("//fix"), Box::from(|x : Vec<char>| 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

View File

@@ -11,7 +11,7 @@ fn read_lines<P>(filename: P) -> io::Result<Lines<BufReader<File>>> where P: AsR
Ok(BufReader::new(file).lines())
}
pub fn regex_parser(path: &str, regex: Vec<String>, verbosity: i8) -> Result<Vec<Token>, io::Error> {
pub fn regex_parser(path: &str, regex: &[String], verbosity: i8) -> Result<Vec<Token>, io::Error> {
let set = RegexSet::new(regex).unwrap();
let mut tokens = vec![];
for (line_cpt, line) in (read_lines(path)?).enumerate() {

View File

@@ -53,7 +53,6 @@ impl Token {
t.comment = t.comment.map_or_else(|| Some(field.to_string()), |comment| Some(format!("{} {}", comment, field)));
}
}
t
}