@@ -5,14 +5,26 @@ use std::fs;
5
5
6
6
/// Runs a search with the provided config
7
7
pub fn run ( config : Config ) -> Result < ( ) , Box < dyn Error > > {
8
+ let results: Vec < & str > ;
8
9
let contents = fs:: read_to_string ( & config. filename ) ?;
9
-
10
- let results = if config. case {
11
- search ( & config. query , & contents)
10
+ if config. verbose {
11
+ println ! ( "Parsing file contents" ) ;
12
+
13
+ println ! ( "Checking case sensitivity" ) ;
14
+ results = if config. case {
15
+ println ! ( "Running case sensitive search" ) ;
16
+ search ( & config. query , & contents)
17
+ } else {
18
+ println ! ( "Running case insensitive search" ) ;
19
+ search_case_insensitive ( & config. query , & contents)
20
+ } ;
12
21
} else {
13
- search_case_insensitive ( & config. query , & contents)
14
- } ;
15
-
22
+ results = if config. case {
23
+ search ( & config. query , & contents)
24
+ } else {
25
+ search_case_insensitive ( & config. query , & contents)
26
+ } ;
27
+ }
16
28
if results. is_empty ( ) {
17
29
println ! ( "Found no line containing {}" , config. query) ;
18
30
} else {
@@ -21,7 +33,6 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
21
33
println ! ( "{}" , line) ;
22
34
}
23
35
}
24
-
25
36
Ok ( ( ) )
26
37
}
27
38
@@ -36,12 +47,17 @@ pub struct Config {
36
47
/// Case sensitivity (optional)
37
48
#[ arg( short, long) ]
38
49
pub case : bool ,
50
+ /// Verbose logging (optional)
51
+ #[ arg( short, long) ]
52
+ pub verbose : bool ,
39
53
}
40
54
41
55
/// Implementation of the Config struct
42
56
impl Config {
43
57
pub fn new ( args : & [ String ] ) -> Result < Config , & str > {
44
- if args. len ( ) < 3 {
58
+ if args. len ( ) < 4 {
59
+ return Err ( "Missing 3 arguments" ) ;
60
+ } else if args. len ( ) < 3 {
45
61
return Err ( "Missing 2 arguments" ) ;
46
62
} else if args. len ( ) < 4 {
47
63
return Err ( "Missing 1 argument" ) ;
@@ -50,11 +66,13 @@ impl Config {
50
66
let query = args[ 1 ] . clone ( ) ;
51
67
let filename = args[ 2 ] . clone ( ) ;
52
68
let case = string_to_bool ( args[ 3 ] . clone ( ) ) ;
69
+ let verbose = string_to_bool ( args[ 4 ] . clone ( ) ) ;
53
70
54
71
Ok ( Config {
55
72
query,
56
73
filename,
57
74
case,
75
+ verbose,
58
76
} )
59
77
}
60
78
}
@@ -94,8 +112,7 @@ pub fn string_to_bool(string: String) -> bool {
94
112
} else if string == "false" {
95
113
is_bool = false ;
96
114
} else {
97
- println ! ( "An error ocurred" ) ;
98
- eprintln ! ( "Internal error : bad usage of searcher_txt::string_to_bool" ) ;
115
+ eprintln ! ( "An error occurred during a string_to_bool operation" ) ;
99
116
}
100
117
101
118
is_bool
0 commit comments