1
- use crate :: commands:: parser;
1
+ use crate :: commands:: { parser, error } ;
2
2
extern crate walkdir;
3
3
use walkdir:: { DirEntry , WalkDir } ;
4
4
use std:: fs:: File ;
@@ -12,7 +12,8 @@ fn is_hidden(entry: &DirEntry) -> bool {
12
12
. unwrap_or ( false )
13
13
}
14
14
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 > {
16
17
let mut todos = Vec :: new ( ) ;
17
18
for file_path in WalkDir :: new ( directory_path)
18
19
. into_iter ( )
@@ -21,32 +22,35 @@ pub fn list_path(directory_path: &str) -> Result<Vec<String>, ()> {
21
22
. filter_map ( |file| file. ok ( ) ) {
22
23
23
24
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 ) ?;
25
28
let file_buffer = io:: BufReader :: new ( file) ;
26
29
27
30
for line in file_buffer. lines ( ) {
28
31
match line. as_deref ( ) {
29
- Ok ( "" ) | Err ( _ ) => ( ) ,
32
+ Ok ( "" ) => ( ) ,
30
33
Ok ( source_code_line) => {
31
34
match parse_line ( source_code_line) {
32
35
Ok ( todo_line) => todos. push ( todo_line. to_string ( ) ) ,
33
36
Err ( _) => ( )
34
37
}
35
38
}
39
+ Err ( _) => todos. push ( "Can't read line." . to_string ( ) )
36
40
}
37
41
}
38
42
}
39
43
}
40
44
Ok ( todos)
41
45
}
42
46
43
- fn parse_line ( line : & str ) -> Result < & str , ( ) > {
47
+ fn parse_line ( line : & str ) -> Result < & str , error :: Errors > {
44
48
45
49
parser:: todo ( ) ( line. as_bytes ( ) )
46
- . map_err ( |_| ( ) )
50
+ . map_err ( |_| error :: Errors :: ParseFail )
47
51
. map ( |( _, y) |
48
52
str:: from_utf8 ( y)
49
- . map_err ( |_| ( ) )
53
+ . map_err ( |_| error :: Errors :: Utf8Error )
50
54
) ?
51
55
}
52
56
0 commit comments