diff --git a/src/commands.rs b/src/commands.rs index b252112..3e32c41 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,3 +1,4 @@ pub mod catalog; pub mod list; pub mod parser; +pub mod error; diff --git a/src/commands/error.rs b/src/commands/error.rs new file mode 100644 index 0000000..cf2e0b5 --- /dev/null +++ b/src/commands/error.rs @@ -0,0 +1,8 @@ + +pub enum Errors { + Utf8Error, + ParseFail, + CantReadLine, + MetadataError, + CantOpenFile +} diff --git a/src/commands/list.rs b/src/commands/list.rs index 39fd76d..bc2e529 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,4 +1,4 @@ -use crate::commands::parser; +use crate::commands::{parser, error}; extern crate walkdir; use walkdir::{DirEntry, WalkDir}; use std::fs::File; @@ -12,36 +12,50 @@ fn is_hidden(entry: &DirEntry) -> bool { .unwrap_or(false) } -pub fn list_path(directory_path: &str) -> Result, ()> { - let mut todos = Vec::new(); - for file_path in WalkDir::new(directory_path).into_iter().filter_entry(|e| !is_hidden(e)).filter_map(|file| file.ok()) { - if file_path.metadata().unwrap().is_file() { - let file = File::open(file_path.path()).unwrap(); - let mut file_buffer = io::BufReader::new(file); - let mut content_buffer = String::new(); - loop { - match file_buffer.read_line(&mut content_buffer) { - Ok(0) => break, - Ok(_) => { - content_buffer.pop(); // This is to remove the new line at the end - match parser::todo()(content_buffer.as_str().as_bytes()) { - Ok(x) => { - match str::from_utf8(x.1) { - Ok(v) => { - todos.push(v.to_string()); - }, - Err(_e) => (), - }; - }, - Err(_e) => () - } - content_buffer.clear(); - }, - Err(_) => () - } - } - // println!("Parsed TODOs from file {:?}", file_path.path().display()); - } - } - Ok(todos) +// TODO: return Vec, error::Errors> instead. +pub fn list_path(directory_path: &str) -> Result, error::Errors> { + let mut todos = Vec::new(); + for file_path in WalkDir::new(directory_path) + .into_iter() + // TODO: let the user decide to skip hidden dirs in the config file + .filter_entry(|e| !is_hidden(e)) + .filter_map(|file| file.ok()) { + + if file_path.metadata().unwrap().is_file() { + let file = File::open(file_path.path()) + // TODO: continue instead of returning with ? + .map_err(|_| error::Errors::CantOpenFile)?; + let file_buffer = io::BufReader::new(file); + + for line in file_buffer.lines() { + match line.as_deref() { + Ok("") => (), + Ok(source_code_line) => { + match parse_line(source_code_line) { + Ok(todo_line) => todos.push(todo_line.to_string()), + Err(_) => () + } + } + Err(_) => todos.push("Can't read line.".to_string()) + } + } + } + } + Ok(todos) +} + +fn parse_line(line: &str) -> Result<&str, error::Errors> { + + parser::todo()(line.as_bytes()) + .map_err(|_| error::Errors::ParseFail) + .map(|(_,y)| + str::from_utf8(y) + .map_err(|_| error::Errors::Utf8Error) + )? +} + + +#[test] +fn test_list_path() { + //Criar um /tmp }