Skip to content

Commit

Permalink
lib: use env::Args on Config::new
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkmaia committed Feb 12, 2018
1 parent 99f0d46 commit 6957e26
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
18 changes: 12 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
if args.len() < 3 {
return Err("Insufficient parameters");
}
pub fn new(mut args: env::Args) -> Result<Config, &'static str> {
args.next();

let pattern = match args.next() {
Some(pattern) => pattern,
None => return Err("Missing search pattern"),
};

let filename = match args.next() {
Some(filename) => filename,
None => return Err("Missing filename"),
};

let pattern = args[1].to_string();
let filename = args[2].to_string();
let case_sensitive = env::var("CASE_INSENSITIVE").is_err();

Ok(Config { filename, pattern, case_sensitive })
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::env;
use std::process;

fn main() {
let args: Vec<String> = env::args().collect();
let config = minigrep::Config::new(&args).unwrap_or_else(|err| {
let config = minigrep::Config::new(env::args()).unwrap_or_else(|err| {
eprintln!("Error on initialization\n{}", err);
process::exit(1);
});
Expand Down

0 comments on commit 6957e26

Please sign in to comment.