File tree Expand file tree Collapse file tree 2 files changed +16
-8
lines changed Expand file tree Collapse file tree 2 files changed +16
-8
lines changed Original file line number Diff line number Diff line change @@ -8,13 +8,22 @@ pub struct Config {
8
8
}
9
9
10
10
impl Config {
11
- pub fn build ( args : & [ String ] ) -> Result < Config , & ' static str > {
12
- if args. len ( ) < 3 {
13
- return Err ( "Not enough arguments, expected format: [search_text] [file_path]" ) ;
14
- }
11
+ pub fn build < T > ( mut args : T ) -> Result < Config , & ' static str >
12
+ where
13
+ T : Iterator < Item = String >
14
+ {
15
+ args. next ( ) ;
16
+
17
+ let query = match args. next ( ) {
18
+ Some ( arg) => arg,
19
+ None => return Err ( "Didn't get a query string" )
20
+ } ;
21
+
22
+ let file_path = match args. next ( ) {
23
+ Some ( arg) => arg,
24
+ None => return Err ( "Didn't get a file path" )
25
+ } ;
15
26
16
- let query = args[ 1 ] . clone ( ) ;
17
- let file_path = args[ 2 ] . clone ( ) ;
18
27
let ignore_case = env:: var ( "IGNORE_CASE" ) . is_ok ( ) ;
19
28
20
29
Ok ( Config {
Original file line number Diff line number Diff line change @@ -2,8 +2,7 @@ use std::{env, process};
2
2
use rust_grep:: Config ;
3
3
4
4
fn main ( ) {
5
- let args: Vec < String > = env:: args ( ) . collect ( ) ;
6
- let config = Config :: build ( & args)
5
+ let config = Config :: build ( env:: args ( ) )
7
6
. unwrap_or_else ( |err| {
8
7
eprintln ! ( "Problem parsing arguments: {}" , err) ;
9
8
process:: exit ( 1 ) ;
You can’t perform that action at this time.
0 commit comments