Added test and case_insensitive
This commit is contained in:
5
output.txt
Normal file
5
output.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
Searching for to
|
||||||
|
In file poem.txt
|
||||||
|
Found!
|
||||||
|
Are you nobody, too?
|
||||||
|
How dreary to be somebody!
|
||||||
55
src/lib.rs
55
src/lib.rs
@@ -1,4 +1,53 @@
|
|||||||
//TODO implement search function
|
/*
|
||||||
pub fn search() {
|
contents: "Shall I compare thee\nto a summer's day"
|
||||||
unimplemented!();
|
↑
|
||||||
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
26
src/main.rs
26
src/main.rs
@@ -3,7 +3,7 @@ use std::env;
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
|
||||||
use minigrep::search;
|
use minigrep::{search, search_case_insensitive};
|
||||||
|
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
@@ -16,7 +16,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
// let config = Config::build(&args)?;
|
// let config = Config::build(&args)?;
|
||||||
// or
|
// or
|
||||||
let config = Config::build(&args).unwrap_or_else(|err| {
|
let config = Config::build(&args).unwrap_or_else(|err| {
|
||||||
println!("Problem parsing arguments: {err}");
|
eprintln!("Problem parsing arguments: {err}");
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
// run(config)?;
|
// run(config)?;
|
||||||
// or
|
// or
|
||||||
if let Err(e) = run(config) {
|
if let Err(e) = run(config) {
|
||||||
println!("Application error: {e}");
|
eprintln!("Application error: {e}");
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,16 +35,24 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
fn run (config: Config) -> Result<(), Box<dyn Error>> {
|
fn run (config: Config) -> Result<(), Box<dyn Error>> {
|
||||||
let contents: String = fs::read_to_string(config.file_path)?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Config {
|
struct Config {
|
||||||
query: String,
|
pub query: String,
|
||||||
file_path: String,
|
pub file_path: String,
|
||||||
|
pub ignore_case: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
@@ -58,7 +66,9 @@ impl Config {
|
|||||||
let query = args[1].clone();
|
let query = args[1].clone();
|
||||||
let file_path = args[2].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 })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user