Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major refactoring #1

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod catalog;
pub mod list;
pub mod parser;
pub mod error;
8 changes: 8 additions & 0 deletions src/commands/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

pub enum Errors {
Utf8Error,
ParseFail,
CantReadLine,
MetadataError,
CantOpenFile
}
80 changes: 47 additions & 33 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::commands::parser;
use crate::commands::{parser, error};
extern crate walkdir;
use walkdir::{DirEntry, WalkDir};
use std::fs::File;
Expand All @@ -12,36 +12,50 @@ fn is_hidden(entry: &DirEntry) -> bool {
.unwrap_or(false)
}

pub fn list_path(directory_path: &str) -> Result<Vec<String>, ()> {
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<Result<String>, error::Errors> instead.
pub fn list_path(directory_path: &str) -> Result<Vec<String>, 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
}
Loading