From 5d78fd6eca4b4469bf39afa45f9a650dbfcf35fd Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Mon, 2 Sep 2024 13:22:01 -0700 Subject: [PATCH] finish --- ' | 39 --------------------------- README.md | 3 +++ src/lib.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++--- src/main.rs | 7 ++--- 4 files changed, 79 insertions(+), 47 deletions(-) delete mode 100644 ' create mode 100644 README.md diff --git a/' b/' deleted file mode 100644 index ddef96d..0000000 --- a/' +++ /dev/null @@ -1,39 +0,0 @@ -use std::env; -use std::fs; -use std::process; - -fn main() { - let args: Vec = env::args().collect(); - - let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {err}"); - process::exit(1); - }); - - run(config); -} - -fn run(config: Config) -> Result<(), Box> { - let contents = - fs::read_to_string(config.file_path).expect("Should have have been able to read the file!"); - - println!("With text:\n{contents}"); -} - -struct Config { - query: String, - file_path: String, -} - -impl Config { - fn build(args: &[String]) -> Result { - if args.len() < 3 { - return Err("not enough arguments!"); - } - - let query = args[1].clone(); - let file_path = args[2].clone(); - - Ok(Config { query, file_path }) - } -} diff --git a/README.md b/README.md new file mode 100644 index 0000000..b41b15e --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# minigrep + +from the rust book. diff --git a/src/lib.rs b/src/lib.rs index 5fde8ee..670ddb8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,18 @@ use std::error::Error; -use std::fs; +use std::{env, fs}; pub fn run(config: Config) -> Result<(), Box> { let contents = fs::read_to_string(config.file_path)?; - println!("With text:\n{contents}"); + let results = if config.ignore_case { + search_case_insensitive(&config.query, &contents) + } else { + search(&config.query, &contents) + }; + + for line in results { + println!("{line}"); + } Ok(()) } @@ -12,6 +20,7 @@ pub fn run(config: Config) -> Result<(), Box> { pub struct Config { pub query: String, pub file_path: String, + pub ignore_case: bool, } impl Config { @@ -23,6 +32,68 @@ impl Config { let query = args[1].clone(); let file_path = args[2].clone(); - Ok(Config { query, file_path }) + let ignore_case = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { + query, + file_path, + ignore_case, + }) + } +} + +fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + + for line in contents.lines() { + if line.contains(query) { + results.push(line); + } + } + + results +} + +fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let mut results = Vec::new(); + let query = query.to_lowercase(); + + for line in contents.lines() { + if line.to_lowercase().contains(&query) { + results.push(line); + } + } + + results +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + 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 881abb4..5224b85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,15 +7,12 @@ fn main() { let args: Vec = env::args().collect(); let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {err}"); + mprintln!("Problem parsing arguments: {err}"); process::exit(1); }); - println!("Searching for {}", config.query); - println!("In file {}", config.file_path); - if let Err(e) = minigrep::run(config) { - println!("Application error: {e}"); + eprintln!("Application error: {e}"); process::exit(1); } }