mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-07-06 11:07:12 -06:00
almost done with ch13.3
This commit is contained in:
@ -10,17 +10,43 @@ pub struct Config {
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn build(args: &[String]) -> Result<Config, &'static str> {
|
||||
if args.len() < 3 {
|
||||
return Err("not enough arguments");
|
||||
}
|
||||
// refactor 14
|
||||
// pub 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();
|
||||
// let query = args[1].clone();
|
||||
// let file_path = args[2].clone();
|
||||
|
||||
// let ignore_case = env::var("IGNORE_CASE").is_ok();
|
||||
|
||||
// Ok(Config { query, file_path, ignore_case })
|
||||
// }
|
||||
|
||||
pub fn build(mut args: impl Iterator<Item = String>,) -> Result<Config, &'static str> {
|
||||
// --snip-- for now
|
||||
|
||||
// skip first value in iterator, value inside is name of program
|
||||
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 { query, file_path, ignore_case })
|
||||
Ok(Config {
|
||||
query,
|
||||
file_path,
|
||||
ignore_case,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,17 +75,23 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
|
||||
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
||||
// original that only can fail
|
||||
// vec![]
|
||||
let mut results = Vec::new();
|
||||
// refacotr 15
|
||||
// // original that only can fail
|
||||
// // vec![]
|
||||
// let mut results = Vec::new();
|
||||
|
||||
for line in contents.lines() {
|
||||
if line.contains(query) {
|
||||
// do something with line
|
||||
results.push(line);
|
||||
}
|
||||
}
|
||||
results
|
||||
// for line in contents.lines() {
|
||||
// if line.contains(query) {
|
||||
// // do something with line
|
||||
// results.push(line);
|
||||
// }
|
||||
// }
|
||||
// results
|
||||
|
||||
contents
|
||||
.lines()
|
||||
.filter(|line| line.contains(query))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn search_case_insensitive<'a>(
|
||||
|
@ -6,7 +6,8 @@ use std::process;
|
||||
use minigrep::Config;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
// refactor 13
|
||||
// let args: Vec<String> = env::args().collect();
|
||||
|
||||
// dbg!(args);
|
||||
|
||||
@ -22,11 +23,17 @@ fn main() {
|
||||
// refactor 3
|
||||
// let config = Config::new(&args);
|
||||
|
||||
// recfactor 6
|
||||
let config = Config::build(&args).unwrap_or_else(|err| {
|
||||
// refactor 14
|
||||
// println!("Problem parsing arguments: {err}");
|
||||
// process::exit(1);
|
||||
// // recfactor 6
|
||||
// let config = Config::build(&args).unwrap_or_else(|err| {
|
||||
// // refactor 14
|
||||
// // println!("Problem parsing arguments: {err}");
|
||||
// // process::exit(1);
|
||||
// eprintln!("Problem parsing arguments: {err}");
|
||||
// process::exit(1);
|
||||
// });
|
||||
|
||||
// refactor 13
|
||||
let config = Config::build(env::args()).unwrap_or_else(|err| {
|
||||
eprintln!("Problem parsing arguments: {err}");
|
||||
process::exit(1);
|
||||
});
|
||||
|
Reference in New Issue
Block a user