refactored code in minigrep to properly use iterators.
This commit is contained in:
parent
6d5c9a730c
commit
eb0612b93b
|
@ -1,5 +1,5 @@
|
||||||
use std::error::Error;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::error::Error;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
||||||
|
@ -22,40 +22,39 @@ pub struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn new(args: &[String]) -> Result<Config, &'static str> {
|
pub fn new(mut args: impl Iterator<Item = String>) -> Result<Config, &'static str> {
|
||||||
if args.len() < 3 {
|
args.next();
|
||||||
return Err("Not enough arguments");
|
|
||||||
}
|
let query = match args.next() {
|
||||||
let query = args[1].clone();
|
Some(arg) => arg,
|
||||||
let filename = args[2].clone();
|
None => return Err("Didn't get a query string."),
|
||||||
|
};
|
||||||
|
let filename = match args.next() {
|
||||||
|
Some(arg) => arg,
|
||||||
|
None => return Err("Didn't get a file name."),
|
||||||
|
};
|
||||||
|
|
||||||
let ignore_case = env::var("IGNORE_CASE").is_ok();
|
let ignore_case = env::var("IGNORE_CASE").is_ok();
|
||||||
Ok(Config { query, filename, ignore_case })
|
Ok(Config {
|
||||||
|
query,
|
||||||
|
filename,
|
||||||
|
ignore_case,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
||||||
for line in contents.lines() {
|
.filter(|line| line.contains(query))
|
||||||
if line.contains(query) {
|
.collect()
|
||||||
results.push(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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> {
|
||||||
let query = query.to_lowercase();
|
contents
|
||||||
let mut results = Vec::new();
|
.lines()
|
||||||
|
.filter(|line| line.to_lowercase().contains(&query.to_lowercase()))
|
||||||
for line in contents.lines() {
|
.collect()
|
||||||
if line.to_lowercase().contains(&query) {
|
|
||||||
results.push(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
results
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -4,9 +4,7 @@ use std::process;
|
||||||
use minigrep::Config;
|
use minigrep::Config;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let config = Config::new(env::args()).unwrap_or_else(|err| {
|
||||||
|
|
||||||
let config = Config::new(&args).unwrap_or_else(|err| {
|
|
||||||
eprintln!("Problem parsing arguments: {}", err);
|
eprintln!("Problem parsing arguments: {}", err);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue