diff --git a/output.txt b/output.txt new file mode 100644 index 0000000..7028ba1 --- /dev/null +++ b/output.txt @@ -0,0 +1,5 @@ +Searching for to +In file poem.txt +Found! +Are you nobody, too? +How dreary to be somebody! diff --git a/src/lib.rs b/src/lib.rs index 19e8e50..dfb9722 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,53 @@ -//TODO implement search function -pub fn search() { - unimplemented!(); +/* + contents: "Shall I compare thee\nto a summer's day" + ↑ + Vec[0]: &"Shall I compare thee" ← punta qui dentro, non è una copia +*/ +pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let query = query.to_lowercase(); + let results = contents + .lines() // lines gives slice that points to memory of contents + .filter(|line| line.to_lowercase().contains(&query)) + .collect(); + + results +} + +pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + contents + .lines() // lines gives slice that points to memory of contents + .filter(|line| line.contains(query)) + .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) + ); + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6fc4d3e..852990a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use std::env; use std::error::Error; use std::process; -use minigrep::search; +use minigrep::{search, search_case_insensitive}; fn main() -> Result<(), Box> { @@ -16,7 +16,7 @@ fn main() -> Result<(), Box> { // let config = Config::build(&args)?; // or let config = Config::build(&args).unwrap_or_else(|err| { - println!("Problem parsing arguments: {err}"); + eprintln!("Problem parsing arguments: {err}"); process::exit(1); }); @@ -26,7 +26,7 @@ fn main() -> Result<(), Box> { // run(config)?; // or if let Err(e) = run(config) { - println!("Application error: {e}"); + eprintln!("Application error: {e}"); process::exit(1); } @@ -35,16 +35,24 @@ fn main() -> Result<(), Box> { fn run (config: Config) -> Result<(), Box> { let contents: String = fs::read_to_string(config.file_path)?; - println!("With text:\n{contents}"); - search(); + // 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 { - query: String, - file_path: String, + pub query: String, + pub file_path: String, + pub ignore_case: bool, } impl Config { @@ -58,7 +66,9 @@ 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 }) } }