ch13.03 done
This commit is contained in:
parent
aa4818e9e7
commit
359fc2b0b7
@ -34,15 +34,37 @@ impl Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(args: &[String]) -> Result<Config, &'static str> {
|
// pub fn build(args: &[String]) -> Result<Config, &'static str> {
|
||||||
if args.len() < 3 {
|
// if args.len() < 3 {
|
||||||
return Err("not enough arguments");
|
// return Err("not enough arguments");
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
// Ok(Config {
|
||||||
|
// query: args[1].clone(),
|
||||||
|
// file_path: args[2].clone(),
|
||||||
|
// ignore_case: env::var("IGNORE_CASE").is_ok(),
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
|
||||||
|
pub fn build(mut args: impl Iterator<Item = String>) -> Result<Config, &'static str> {
|
||||||
|
args.next();
|
||||||
|
|
||||||
|
let query = match args.next() {
|
||||||
|
Some(arg) => arg,
|
||||||
|
None => return Err("Didn't get a query string"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let file_path = match args.next() {
|
||||||
|
Some(arg) => arg,
|
||||||
|
None => return Err("Didn't get a file path"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let ignore_case = env::var("IGNORE_CASE").is_ok();
|
||||||
|
|
||||||
Ok(Config {
|
Ok(Config {
|
||||||
query: args[1].clone(),
|
query,
|
||||||
file_path: args[2].clone(),
|
file_path,
|
||||||
ignore_case: env::var("IGNORE_CASE").is_ok(),
|
ignore_case,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,15 +90,19 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
||||||
let mut results = Vec::new();
|
contents
|
||||||
|
.lines()
|
||||||
|
.filter(|line| line.contains(query))
|
||||||
|
.collect()
|
||||||
|
// let mut results = Vec::new();
|
||||||
|
|
||||||
for line in contents.lines() {
|
// for line in contents.lines() {
|
||||||
if line.contains(query) {
|
// if line.contains(query) {
|
||||||
results.push(line);
|
// results.push(line);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
results
|
// results
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
||||||
|
@ -20,8 +20,9 @@ use std::process;
|
|||||||
|
|
||||||
use minigrep::Config;
|
use minigrep::Config;
|
||||||
|
|
||||||
|
// The modifications for ch13 are added in this version
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
// let args: Vec<String> = env::args().collect();
|
||||||
// dbg!(args);
|
// dbg!(args);
|
||||||
|
|
||||||
// let config = Config::new(&args);
|
// let config = Config::new(&args);
|
||||||
@ -29,7 +30,8 @@ fn main() {
|
|||||||
// unwrap_or_else passes an argument to a closure which is an
|
// unwrap_or_else passes an argument to a closure which is an
|
||||||
// anonymous function
|
// anonymous function
|
||||||
// eprintln!() for output to stdout
|
// eprintln!() for output to stdout
|
||||||
let config = Config::build(&args).unwrap_or_else(|err| {
|
// let config = Config::build(&args).unwrap_or_else(|err| {
|
||||||
|
let config = Config::build(env::args()).unwrap_or_else(|err| {
|
||||||
eprintln!("Problem parsing arguments: {err}");
|
eprintln!("Problem parsing arguments: {err}");
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user