Skip to content

Commit 047b0c6

Browse files
committed
Add Error enum
Error will model all possible errors we find during the execution to create better messages to the user.
1 parent f84bfa7 commit 047b0c6

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/commands.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod catalog;
22
pub mod list;
33
pub mod parser;
4+
pub mod error;

src/commands/error.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
pub enum Errors {
3+
Utf8Error,
4+
ParseFail,
5+
CantReadLine,
6+
MetadataError,
7+
CantOpenFile
8+
}

src/commands/list.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::commands::parser;
1+
use crate::commands::{parser, error};
22
extern crate walkdir;
33
use walkdir::{DirEntry, WalkDir};
44
use std::fs::File;
@@ -12,7 +12,8 @@ fn is_hidden(entry: &DirEntry) -> bool {
1212
.unwrap_or(false)
1313
}
1414

15-
pub fn list_path(directory_path: &str) -> Result<Vec<String>, ()> {
15+
// TODO: return Vec<Result<String>, error::Errors> instead.
16+
pub fn list_path(directory_path: &str) -> Result<Vec<String>, error::Errors> {
1617
let mut todos = Vec::new();
1718
for file_path in WalkDir::new(directory_path)
1819
.into_iter()
@@ -21,32 +22,35 @@ pub fn list_path(directory_path: &str) -> Result<Vec<String>, ()> {
2122
.filter_map(|file| file.ok()) {
2223

2324
if file_path.metadata().unwrap().is_file() {
24-
let file = File::open(file_path.path()).unwrap();
25+
let file = File::open(file_path.path())
26+
// TODO: continue instead of returning with ?
27+
.map_err(|_| error::Errors::CantOpenFile)?;
2528
let file_buffer = io::BufReader::new(file);
2629

2730
for line in file_buffer.lines() {
2831
match line.as_deref() {
29-
Ok("") | Err(_) => (),
32+
Ok("") => (),
3033
Ok(source_code_line) => {
3134
match parse_line(source_code_line) {
3235
Ok(todo_line) => todos.push(todo_line.to_string()),
3336
Err(_) => ()
3437
}
3538
}
39+
Err(_) => todos.push("Can't read line.".to_string())
3640
}
3741
}
3842
}
3943
}
4044
Ok(todos)
4145
}
4246

43-
fn parse_line(line: &str) -> Result<&str, ()> {
47+
fn parse_line(line: &str) -> Result<&str, error::Errors> {
4448

4549
parser::todo()(line.as_bytes())
46-
.map_err(|_| ())
50+
.map_err(|_| error::Errors::ParseFail)
4751
.map(|(_,y)|
4852
str::from_utf8(y)
49-
.map_err(|_| ())
53+
.map_err(|_| error::Errors::Utf8Error)
5054
)?
5155
}
5256

0 commit comments

Comments
 (0)