diff --git a/minigrep/Cargo.toml b/minigrep/Cargo.toml new file mode 100644 index 0000000..b8302ae --- /dev/null +++ b/minigrep/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/minigrep/poems.txt b/minigrep/poems.txt new file mode 100644 index 0000000..7aaea3d --- /dev/null +++ b/minigrep/poems.txt @@ -0,0 +1,10 @@ +I'm nobody! Who are you? +Are you nobody, too? +Then there's a pair of us - don't tell! +They'd banish us, you know. + +How dreary to be somebody! +How public, like a frog +To tell your name the livelong day +To an admiring bog! + diff --git a/minigrep/src/lib.rs b/minigrep/src/lib.rs new file mode 100644 index 0000000..7ea6a32 --- /dev/null +++ b/minigrep/src/lib.rs @@ -0,0 +1,102 @@ +use std::env; +use std::error::Error; +use std::fs; + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + + let mut results; + let casing = (config.ignore_case_arg, config.ignore_case_env); + + // only perform case sensitive search if both + // ignore_case_env and ignore_case_arg are false + match casing { + (false, false) => results = search(&config.query, &contents), + (_, _) => results = search_case_insensitive(&config.query, &contents), + } + + for line in results { + println!("{line}"); + } + + Ok(()) +} + +pub struct Config { + pub query: String, + pub file_path: String, + pub ignore_case_arg: bool, + pub ignore_case_env: bool, +} + +impl Config { + pub fn build(args: &[String]) -> Result { + if args.len() < 4 { + return Err("not enough arguments"); + } + + let query = args[1].clone(); + let file_path = args[2].clone(); + let ignore_case_arg = !args[3].clone().is_empty(); + let ignore_case_env = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { + query, + file_path, + ignore_case_arg, + ignore_case_env, + }) + } +} + +pub 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 +} +pub 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. +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/minigrep/src/main.rs b/minigrep/src/main.rs new file mode 100644 index 0000000..3b45d67 --- /dev/null +++ b/minigrep/src/main.rs @@ -0,0 +1,34 @@ +use minigrep::Config; +use std::env; +use std::process; + +fn main() { + let args: Vec = env::args().collect(); + + let config = Config::build(&args).unwrap_or_else(|error| { + println!("Problem parsing arguments: {error}"); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config) { + println!("Application error: {e}"); + process::exit(1); + } +} + +// end of main +// another way to handle the Config building +// let config = Config::build(&args); +// match config { +// Err(error) => { +// println!("error: {error}"); +// } +// Ok(config) => { +// println!( +// "searching for \"{}\" in file \"{}\"", +// config.query, config.file_path, +// ); +// +// println!("with text:\n{contents}"); +// } +// }