diff --git a/src/main.rs b/src/main.rs index 852990a..22fcb87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,12 +10,12 @@ fn main() -> Result<(), Box> { // Args returns an iterator of args from cli // Collect turns an iterator into a collection // In this case transform values of args into a vector - let args: Vec = env::args().collect(); + // let args: Vec = env::args().collect(); // let config = Config::build(&args)?; // or - let config = Config::build(&args).unwrap_or_else(|err| { + let config = Config::build(env::args()).unwrap_or_else(|err| { eprintln!("Problem parsing arguments: {err}"); process::exit(1); }); @@ -56,15 +56,18 @@ struct Config { } impl Config { - fn build(args: &[String]) -> Result { - if args.len() > 3 { - return Err("Too many arguments. Usage: minigrep "); - } - if args.len() < 3 { - return Err("Too few arguments. Usage: minigrep "); - } - let query = args[1].clone(); - let file_path = args[2].clone(); + fn build(mut args: impl Iterator) -> Result { + args.next(); // skip first argument because is the name of the program + + let query = match args.next() { + Some(arg) => arg, + None => return Err("No query strings") + }; + + let file_path = match args.next() { + Some(arg) => arg, + None => return Err("No file path") + }; let ignore_case = env::var("IGNORE_CASE").is_ok(); diff --git a/src/main_std.rs b/src/main_std.rs new file mode 100644 index 0000000..852990a --- /dev/null +++ b/src/main_std.rs @@ -0,0 +1,74 @@ +use std::fs; +use std::env; +use std::error::Error; +use std::process; + +use minigrep::{search, search_case_insensitive}; + + +fn main() -> Result<(), Box> { + // Args returns an iterator of args from cli + // Collect turns an iterator into a collection + // In this case transform values of args into a vector + let args: Vec = env::args().collect(); + + + // let config = Config::build(&args)?; + // or + let config = Config::build(&args).unwrap_or_else(|err| { + eprintln!("Problem parsing arguments: {err}"); + process::exit(1); + }); + + println!("Searching for {}", config.query); + println!("In file {}", config.file_path); + + // run(config)?; + // or + if let Err(e) = run(config) { + eprintln!("Application error: {e}"); + process::exit(1); + } + + Ok(()) +} + +fn run (config: Config) -> Result<(), Box> { + let contents: String = fs::read_to_string(config.file_path)?; + + // results can't be used after contents die + let results = if config.ignore_case { + search_case_insensitive(&config.query, &contents) + } else { + search(&config.query, &contents) + }; + + println!("Found!"); + for line in results { println!("{line}"); } + + Ok(()) +} + +struct Config { + pub query: String, + pub file_path: String, + pub ignore_case: bool, +} + +impl Config { + fn build(args: &[String]) -> Result { + if args.len() > 3 { + return Err("Too many arguments. Usage: minigrep "); + } + if args.len() < 3 { + return Err("Too few arguments. Usage: minigrep "); + } + let query = args[1].clone(); + let file_path = args[2].clone(); + + let ignore_case = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { query, file_path, ignore_case }) + } +} +