实现 Config::build 方法以解析命令行参数并更新主函数以使用新方法
This commit is contained in:
parent
21970c04d5
commit
5490f28e43
@ -8,6 +8,27 @@ pub struct Config {
|
||||
}
|
||||
|
||||
impl Config {
|
||||
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("query not provided"),
|
||||
};
|
||||
|
||||
let file_path = match args.next() {
|
||||
Some(arg) => arg,
|
||||
None => return Err("file path not provided"),
|
||||
};
|
||||
|
||||
let ignore_case = env::var("IGNORE_CASE").is_ok();
|
||||
Ok(Config {
|
||||
query,
|
||||
file_path,
|
||||
ignore_case,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn new(args: &[String]) -> Result<Config, &'static str> {
|
||||
if args.len() < 3 {
|
||||
return Err("not enough arguments");
|
||||
@ -28,8 +49,10 @@ impl Config {
|
||||
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
||||
let contents = fs::read_to_string(config.file_path)?;
|
||||
let results = if config.ignore_case {
|
||||
println!("Ignoring case");
|
||||
search_case_insensitive(&config.query, &contents)
|
||||
} else {
|
||||
println!("Case sensitive");
|
||||
search(&config.query, &contents)
|
||||
};
|
||||
|
||||
@ -80,24 +103,32 @@ Trust me.";
|
||||
}
|
||||
|
||||
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
||||
let mut result = Vec::new();
|
||||
for line in contents.lines() {
|
||||
if line.contains(&query) {
|
||||
result.push(line);
|
||||
}
|
||||
}
|
||||
result
|
||||
// let mut result = Vec::new();
|
||||
// for line in contents.lines() {
|
||||
// if line.contains(&query) {
|
||||
// result.push(line);
|
||||
// }
|
||||
// }
|
||||
// result
|
||||
contents
|
||||
.lines()
|
||||
.filter(|line| line.contains(&query))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
||||
let query = query.to_lowercase();
|
||||
let mut result = Vec::new();
|
||||
// let query = query.to_lowercase();
|
||||
// let mut result = Vec::new();
|
||||
|
||||
for line in contents.lines() {
|
||||
if line.to_lowercase().contains(&query) {
|
||||
result.push(line);
|
||||
}
|
||||
}
|
||||
// for line in contents.lines() {
|
||||
// if line.to_lowercase().contains(&query) {
|
||||
// result.push(line);
|
||||
// }
|
||||
// }
|
||||
|
||||
result
|
||||
// result
|
||||
contents
|
||||
.lines()
|
||||
.filter(|line| line.to_lowercase().contains(&query.to_lowercase()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
@ -4,9 +4,13 @@ use std::process;
|
||||
use minigrep::Config;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let config = Config::new(&args).unwrap_or_else(|err| {
|
||||
println!("Problem parsing arguments: {}", err);
|
||||
// let args: Vec<String> = env::args().collect();
|
||||
// let config = Config::new(&args).unwrap_or_else(|err| {
|
||||
// eprintln!("Problem parsing arguments: {}", err);
|
||||
// process::exit(1);
|
||||
// });
|
||||
let config = Config::build(env::args()).unwrap_or_else(|err| {
|
||||
eprintln!("Problem parsing arguments: {}", err);
|
||||
process::exit(1);
|
||||
});
|
||||
|
||||
@ -14,7 +18,7 @@ fn main() {
|
||||
println!("In file {}", config.file_path);
|
||||
|
||||
if let Err(e) = minigrep::run(config) {
|
||||
println!("Application error: {}", e);
|
||||
eprintln!("Application error: {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user