Skip to content

Commit 70c2045

Browse files
committed
refactor: Optimized config build function
1 parent 0c008a3 commit 70c2045

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/lib.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@ pub struct Config {
88
}
99

1010
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+
};
1526

16-
let query = args[1].clone();
17-
let file_path = args[2].clone();
1827
let ignore_case = env::var("IGNORE_CASE").is_ok();
1928

2029
Ok(Config {

src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use std::{env, process};
22
use rust_grep::Config;
33

44
fn main() {
5-
let args: Vec<String> = env::args().collect();
6-
let config = Config::build(&args)
5+
let config = Config::build(env::args())
76
.unwrap_or_else(|err| {
87
eprintln!("Problem parsing arguments: {}", err);
98
process::exit(1);

0 commit comments

Comments
 (0)