style: code style
This commit is contained in:
169
src/main.rs
169
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..."))
|
.subcommand(SubCommand::with_name("legacy").about("Launch program in legacy mode (supports todo!(), etc..."))
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
if let Some(_matches) = matches.subcommand_matches("legacy") {
|
if let Some(_) = matches.subcommand_matches("legacy") {
|
||||||
let mut parsers : Vec<Parser> = vec![];
|
main_legacy()
|
||||||
|
} else {
|
||||||
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{
|
|
||||||
let mut tokens : Vec<Token> = Vec::new();
|
let mut tokens : Vec<Token> = Vec::new();
|
||||||
|
let mut togo_config_path = String::from(dirs::home_dir().unwrap().to_str().unwrap());
|
||||||
let mut path = String::from(dirs::home_dir().unwrap().to_str().unwrap());
|
togo_config_path.push_str("/.cargo/todo_config");
|
||||||
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 regex = vec![];
|
let mut regex = vec![];
|
||||||
for line in read_lines(path).unwrap() {
|
for line in read_lines(togo_config_path).unwrap() {
|
||||||
let line = line.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());
|
let mut all_rs_path = String::from(env::current_dir().unwrap().to_str().unwrap());
|
||||||
path.push_str("/**/*.rs");
|
all_rs_path.push_str("/**/*.rs");
|
||||||
|
|
||||||
for entry in match glob(&path) {
|
for entry in match glob(&all_rs_path) {
|
||||||
Ok(entry) => entry,
|
Ok(entry) => entry, Err(e) => {
|
||||||
Err(e) => {
|
|
||||||
println!("Couldn't access files. Error {}", e);
|
println!("Couldn't access files. Error {}", e);
|
||||||
Err(e).unwrap()
|
Err(e).unwrap()
|
||||||
}
|
}
|
||||||
}
|
} {
|
||||||
|
|
||||||
{
|
|
||||||
let path = entry.unwrap();
|
let path = entry.unwrap();
|
||||||
let path = Path::new(&path).strip_prefix(env::current_dir().unwrap().to_str().unwrap()).unwrap();
|
let path = Path::new(&path).strip_prefix(env::current_dir().unwrap().to_str().unwrap()).unwrap();
|
||||||
// println!("{}", path.to_str().unwrap());
|
// println!("{}", path.to_str().unwrap());
|
||||||
if !path.starts_with("target/") {
|
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");
|
let verbose_count = matches.occurrences_of("verbose");
|
||||||
if verbose_count == 0 || verbose_count == 2 {
|
let verbosity = if verbose_count == 0 || verbose_count == 2 {2} else {1};
|
||||||
match regex_parser(path, regex.clone(), 2) {
|
match regex_parser(file_path, ®ex, verbosity) {
|
||||||
Ok(mut t) => {
|
Ok(mut t) => {
|
||||||
tokens.append(&mut t);
|
tokens.append(&mut t);
|
||||||
},
|
},
|
||||||
Err(e) => eprintln!{"{}", e},
|
Err(e) => eprintln!{"{}", e},
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
match regex_parser(path, regex.clone(), 1) {
|
|
||||||
Ok(mut t) => {
|
|
||||||
tokens.append(&mut t);
|
|
||||||
},
|
|
||||||
Err(e) => eprintln!{"{}", e},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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)]
|
#[allow(dead_code)]
|
||||||
// test zone
|
// test zone
|
||||||
//TODO refactor
|
//TODO refactor
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ fn read_lines<P>(filename: P) -> io::Result<Lines<BufReader<File>>> where P: AsR
|
|||||||
Ok(BufReader::new(file).lines())
|
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 set = RegexSet::new(regex).unwrap();
|
||||||
let mut tokens = vec![];
|
let mut tokens = vec![];
|
||||||
for (line_cpt, line) in (read_lines(path)?).enumerate() {
|
for (line_cpt, line) in (read_lines(path)?).enumerate() {
|
||||||
|
|||||||
@@ -53,7 +53,6 @@ impl Token {
|
|||||||
t.comment = t.comment.map_or_else(|| Some(field.to_string()), |comment| Some(format!("{} {}", comment, field)));
|
t.comment = t.comment.map_or_else(|| Some(field.to_string()), |comment| Some(format!("{} {}", comment, field)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t
|
t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user