This commit is contained in:
Youwen Wu 2024-09-02 13:22:01 -07:00
parent 99ebda0e56
commit f6975d3236
Signed by: youwen5
GPG key ID: 865658ED1FE61EC3
3 changed files with 76 additions and 47 deletions

39
'
View file

@ -1,39 +0,0 @@
use std::env;
use std::fs;
use std::process;
fn main() {
let args: Vec<String> = 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<dyn Error>> {
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<Config, &'static str> {
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 })
}
}

View file

@ -1,10 +1,18 @@
use std::error::Error; use std::error::Error;
use std::fs; use std::{env, fs};
pub fn run(config: Config) -> Result<(), Box<dyn Error>> { pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?; 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(()) Ok(())
} }
@ -12,6 +20,7 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
pub struct Config { pub struct Config {
pub query: String, pub query: String,
pub file_path: String, pub file_path: String,
pub ignore_case: bool,
} }
impl Config { impl Config {
@ -23,6 +32,68 @@ 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,
})
}
}
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)
);
} }
} }

View file

@ -7,15 +7,12 @@ fn main() {
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
let config = Config::build(&args).unwrap_or_else(|err| { let config = Config::build(&args).unwrap_or_else(|err| {
println!("Problem parsing arguments: {err}"); mprintln!("Problem parsing arguments: {err}");
process::exit(1); process::exit(1);
}); });
println!("Searching for {}", config.query);
println!("In file {}", config.file_path);
if let Err(e) = minigrep::run(config) { if let Err(e) = minigrep::run(config) {
println!("Application error: {e}"); eprintln!("Application error: {e}");
process::exit(1); process::exit(1);
} }
} }