refactor: improve perf by passing todos ref

This commit is contained in:
2021-07-09 19:07:15 -04:00
parent e97bc04922
commit e31f36b8aa

View File

@@ -21,40 +21,39 @@ fn main() {
return return
} }
let todos = traverse_dir(&args[1]).unwrap(); let mut todos = vec!();
traverse_dir(&args[1], &mut todos).unwrap();
for todo in todos { for todo in todos {
println!("TODO: {}", todo.green()); println!("TODO: {}", todo.green());
} }
} }
fn traverse_dir(path: &str) -> Result<Vec<String>, std::io::Error> { fn traverse_dir(path: &str, todos: &mut Vec<String>) -> Result<(), std::io::Error> {
let mut todos = Vec::new(); let objects = fs::read_dir(path.to_owned())?;
let objects = fs::read_dir(path.to_owned()).unwrap();
for result in objects { for result in objects {
let obj_path = result.unwrap().path(); let obj_path = result?.path();
let obj_str = obj_path.to_str().unwrap(); let obj_str = obj_path.to_str().unwrap();
let obj_metadata = fs::metadata(obj_str).unwrap(); let obj_metadata = fs::metadata(obj_str)?;
if obj_metadata.is_dir() { if obj_metadata.is_dir() {
println!("DIRC: {}", obj_str.yellow()); // println!("DIRC: {}", obj_str.yellow());
todos.append(&mut traverse_dir(obj_str).unwrap()); traverse_dir(obj_str, todos)?;
} }
else if obj_metadata.is_file() { else if obj_metadata.is_file() {
if obj_str.ends_with(".py") { if obj_str.ends_with(".py") {
println!("FILE: {}", obj_str.yellow()); // println!("FILE: {}", obj_str.yellow());
todos.append(&mut get_todos(obj_str).unwrap()); get_todos(obj_str, todos)?;
} }
} }
} }
Ok(todos) Ok(())
} }
fn get_todos(path: &str) -> Result<Vec<String>, std::io::Error> { fn get_todos(path: &str, todos: &mut Vec<String>) -> Result<(), std::io::Error> {
let mut todos = Vec::new(); let contents = fs::read_to_string(path)?;
let contents = fs::read_to_string(path).unwrap();
for line in contents.lines() { for line in contents.lines() {
if line.contains("TODO") { if line.contains("TODO") {
let (_, comment) = line.split_once("TODO").unwrap(); let (_, comment) = line.split_once("TODO").unwrap();
@@ -65,5 +64,5 @@ fn get_todos(path: &str) -> Result<Vec<String>, std::io::Error> {
} }
} }
Ok(todos) Ok(())
} }