diff --git a/output.txt b/output.txt new file mode 100644 index 0000000..d40e5a2 --- /dev/null +++ b/output.txt @@ -0,0 +1,2 @@ +Are you nobody, too? +How dreary to be somebody! diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..f23dccc --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,114 @@ +use std::error::Error; +use std::fs; +use std::env; + +pub struct Config { + pub query: String, + pub filename: String, + pub ignore_case: bool, +} + +impl Config { + pub fn build( + mut args: impl Iterator, + ) -> Result { + args.next(); + + let query = match args.next() { + Some(arg) => arg, + None => return Err("Didn't get a query string"), + }; + + let filename = match args.next() { + Some(arg) => arg, + None => return Err("Didn't get a file path"), + }; + + let ignore_case = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { + query, + filename, + ignore_case, + }) + } + + pub fn new(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + let query = args[1].clone(); + let filename = args[2].clone(); + + let ignore_case = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { + query, + filename, + ignore_case, + }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.filename)?; + + let results = if config.ignore_case { + search_case_insensitive(&config.query, &contents) + } else { + search(&config.query, &contents) + }; + + for line in results { + println!("{line}"); + } + + Ok(()) +} + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + contents + .lines() + .filter(|line| line.contains(query)) + .collect() +} + +pub fn search_case_insensitive<'a>( + query: &str, + contents: &'a str, +) -> Vec<&'a str> { + contents + .lines() + .filter(|line| line.contains(&query.to_lowercase())) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Duct tape."; + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } + + #[test] + fn case_insensitive() { + let query = "rUsT"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three. +Trust me."; + assert_eq!( + vec!["Rust:", "Trust me."], + search_case_insensitive(query, contents) + ); + } +} diff --git a/src/main.rs b/src/main.rs index c34444c..3a44bee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,33 +1,17 @@ -use std::{env, fs}; +use std::{env, process}; +use minigrep::Config; fn main() { let args: Vec = env::args().collect(); - let config = Config::new(&args); - - println!("Searching for {}", config.query); - println!("In file {}", config.filename); + let config = Config::build(env::args()).unwrap_or_else(|err| { + eprintln!("Problem parsing arguments: {err}"); + process::exit(1); + }); - let contents = fs::read_to_string(config.filename) - .expect("Something went wrong reading the file"); - - println!("With text:\n{}", contents); -} - -struct Config { - query: String, - filename: String, -} - -impl Config { - fn new(args: &[String]) -> Config { - if args.len() < 3 { - panic!("not anough arguments"); - } - let query = args[1].clone(); - let filename = args[2].clone(); - - Config {query, filename} + if let Err(e) = minigrep::run(config) { + eprintln!("Application error: {e}"); + process::exit(1); } }