refactor: improve perf using Path and PathBuf

This commit is contained in:
2021-07-10 01:47:57 -04:00
parent e01e63767e
commit c84619f331

View File

@@ -1,5 +1,8 @@
use std::ffi::OsString;
use std::fs; use std::fs;
use std::env; use std::env;
use std::path::Path;
use std::path::PathBuf;
use colored::Colorize; use colored::Colorize;
use gitignore; use gitignore;
use path_absolutize::*; use path_absolutize::*;
@@ -11,58 +14,59 @@ fn main() {
return return
} }
let is_ok = fs::metadata(args[1].to_owned()).is_ok(); let path = Path::new(&args[1]);
if !is_ok { if !path.exists() {
println!("{}", "ERR: Path provided does not exist".red()); println!("{}", "ERR: Path provided does not exist".red());
return return
} }
let is_dir = fs::metadata(args[1].to_owned()).unwrap().is_dir(); if !path.is_dir() {
if !is_dir {
println!("{}", "ERR: Path provided is not a directory".red()); println!("{}", "ERR: Path provided is not a directory".red());
return return
} }
// TODO find safer way to check for gitignore (windows compatible) // TODO find safer way to check for gitignore (windows compatible)
let gi_exists = fs::metadata(args[1].to_owned() + "/.gitignore").is_ok(); let gi_exists = path.join(".gitignore").exists();
if !gi_exists { if !gi_exists {
println!("{}", "WRN: .gitignore not found, may output TODOs in dependencies".yellow()) println!("{}", "WRN: .gitignore not found, may output TODOs in dependencies".yellow())
} }
let mut todos = vec!(); let mut todos = vec!();
if gi_exists { if gi_exists {
let gi_str = args[1].to_owned() + "/.gitignore"; let gi_path = path.join(".gitignore");
let _p = std::path::Path::new(&gi_str).absolutize().unwrap(); let gi_path_abs = gi_path.absolutize().unwrap();
let gi_str_abs = _p.to_str().unwrap(); let gi= gitignore::File::new(&gi_path_abs).unwrap();
let gi_path = std::path::Path::new(&gi_str_abs);
let gi= gitignore::File::new(gi_path).unwrap();
iterate_included_files(&mut todos, &gi).unwrap(); iterate_included_files(&mut todos, &gi).unwrap();
} }
else { else {
traverse_dir(&args[1], &mut todos).unwrap(); traverse_dir(&path, &mut todos).unwrap();
} }
// TODO eventually add option to export to file // TODO eventually add option to export to file
println!("{:4} {:05} {:20} {:50}", "", "LINE".bold(), "FILE".bold(), "COMMENT".bold()); println!("{:4} {:05} {:20} {:50}", "", "LINE".bold(), "FILE".bold(), "COMMENT".bold());
for todo in todos { for todo in todos {
println!("{:4} {:05} {:20} {:50}", "TODO".bold(), todo.line_number.to_string().yellow(), todo.file_name.green(), todo.comment); println!("{:4} {:05} {:20} {:50}", "TODO".bold(), todo.line_number.to_string().yellow(), todo.file_name.into_string().unwrap().green(), todo.comment);
} }
} }
fn traverse_dir(path: &str, todos: &mut Vec<Todo>) -> Result<(), std::io::Error> { fn traverse_dir(path: &Path, todos: &mut Vec<Todo>) -> Result<(), std::io::Error> {
let objects = fs::read_dir(path.to_owned())?; let objects = fs::read_dir(path)?;
for result in objects { for result in objects {
let obj_path = result?.path(); let obj_path = result?.path();
let obj_str = obj_path.to_str().unwrap(); if obj_path.is_dir() {
let obj_metadata = fs::metadata(obj_str)?; traverse_dir(&obj_path, todos)?;
}
if obj_metadata.is_dir() { else if obj_path.is_file() {
traverse_dir(obj_str, todos)?; let extension = match obj_path.extension() {
Some(ext) => { ext.to_owned().into_string().unwrap() }
None => { String::from("") }
};
// TODO support other files besides .py
if extension == "py" {
get_todos(&obj_path, todos)?;
} }
else if obj_metadata.is_file() && obj_str.ends_with(".py") { // TODO support other files besides .py
get_todos(obj_str, todos)?;
} }
} }
@@ -70,26 +74,31 @@ fn traverse_dir(path: &str, todos: &mut Vec<Todo>) -> Result<(), std::io::Error>
} }
fn iterate_included_files(todos: &mut Vec<Todo>, gi: &gitignore::File) -> Result<(), std::io::Error> { fn iterate_included_files(todos: &mut Vec<Todo>, gi: &gitignore::File) -> Result<(), std::io::Error> {
for file_path in gi.included_files().unwrap() { for obj_path in gi.included_files().unwrap() {
let file_str = file_path.to_str().unwrap(); let extension = match obj_path.extension() {
if file_str.ends_with(".py") { // TODO support other files besides .py Some(ext) => { ext.to_owned().into_string().unwrap() }
get_todos(file_str, todos)?; None => { String::from("") }
};
// TODO support other files besides .py
if !obj_path.is_dir() && extension == "py" {
get_todos(&obj_path, todos)?;
} }
} }
Ok(()) Ok(())
} }
fn get_todos(path: &str, todos: &mut Vec<Todo>) -> Result<(), std::io::Error> { fn get_todos(path: &PathBuf, todos: &mut Vec<Todo>) -> Result<(), std::io::Error> {
let contents = fs::read_to_string(path)?; let file_name = path.file_name().to_owned().unwrap();
let mut line_number = 0; let mut line_number = 0;
let file_name = path.split("/").last().unwrap();
for line in contents.lines() { for line in fs::read_to_string(path)?.lines() {
line_number += 1; line_number += 1;
if line.contains("TODO") { if line.contains("TODO") {
let (_, comment) = line.split_once("TODO").unwrap(); let (_, comment) = line.split_once("TODO").unwrap();
let cleaned_comment = comment.replace(":", "").trim().to_owned(); let cleaned_comment = comment.replace(":", "").trim().to_owned();
todos.push(Todo::new(cleaned_comment, file_name.to_owned(), line_number)) todos.push(Todo::new(cleaned_comment, file_name.to_owned(), line_number))
} }
} }
@@ -99,12 +108,12 @@ fn get_todos(path: &str, todos: &mut Vec<Todo>) -> Result<(), std::io::Error> {
struct Todo { struct Todo {
comment: String, comment: String,
file_name: String, file_name: OsString,
line_number: u32 line_number: u32
} }
impl Todo { impl Todo {
fn new(comment: String, file_name: String, line_number: u32) -> Todo { fn new(comment: String, file_name: OsString, line_number: u32) -> Todo {
Todo { Todo {
comment: comment, comment: comment,
file_name: file_name, file_name: file_name,