mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-08-05 01:30:53 -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>(
|
||||
|
Reference in New Issue
Block a user