finish
This commit is contained in:
parent
99ebda0e56
commit
5d78fd6eca
4 changed files with 79 additions and 47 deletions
39
'
39
'
|
@ -1,39 +0,0 @@
|
|||
use std::env;
|
||||
use std::fs;
|
||||
use std::process;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
let config = Config::build(&args).unwrap_or_else(|err| {
|
||||
println!("Problem parsing arguments: {err}");
|
||||
process::exit(1);
|
||||
});
|
||||
|
||||
run(config);
|
||||
}
|
||||
|
||||
fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
||||
let contents =
|
||||
fs::read_to_string(config.file_path).expect("Should have have been able to read the file!");
|
||||
|
||||
println!("With text:\n{contents}");
|
||||
}
|
||||
|
||||
struct Config {
|
||||
query: String,
|
||||
file_path: String,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
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();
|
||||
|
||||
Ok(Config { query, file_path })
|
||||
}
|
||||
}
|
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# minigrep
|
||||
|
||||
from the rust book.
|
77
src/lib.rs
77
src/lib.rs
|
@ -1,10 +1,18 @@
|
|||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::{env, fs};
|
||||
|
||||
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
||||
let contents = fs::read_to_string(config.file_path)?;
|
||||
|
||||
println!("With text:\n{contents}");
|
||||
let results = if config.ignore_case {
|
||||
search_case_insensitive(&config.query, &contents)
|
||||
} else {
|
||||
search(&config.query, &contents)
|
||||
};
|
||||
|
||||
for line in results {
|
||||
println!("{line}");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -12,6 +20,7 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
|||
pub struct Config {
|
||||
pub query: String,
|
||||
pub file_path: String,
|
||||
pub ignore_case: bool,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
|
@ -23,6 +32,68 @@ impl Config {
|
|||
let query = args[1].clone();
|
||||
let file_path = args[2].clone();
|
||||
|
||||
Ok(Config { query, file_path })
|
||||
let ignore_case = env::var("IGNORE_CASE").is_ok();
|
||||
|
||||
Ok(Config {
|
||||
query,
|
||||
file_path,
|
||||
ignore_case,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
||||
let mut results = Vec::new();
|
||||
|
||||
for line in contents.lines() {
|
||||
if line.contains(query) {
|
||||
results.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
results
|
||||
}
|
||||
|
||||
fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
||||
let mut results = Vec::new();
|
||||
let query = query.to_lowercase();
|
||||
|
||||
for line in contents.lines() {
|
||||
if line.to_lowercase().contains(&query) {
|
||||
results.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
results
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn case_sensitive() {
|
||||
let query = "duct";
|
||||
let contents = "\
|
||||
Rust:
|
||||
safe, fast, productive.
|
||||
Pick three.";
|
||||
|
||||
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn case_insensitive() {
|
||||
let query = "rUsT";
|
||||
let contents = "\
|
||||
Rust:
|
||||
safe, fast, productive.
|
||||
Pick three.
|
||||
Trust me.";
|
||||
|
||||
assert_eq!(
|
||||
vec!["Rust:", "Trust me."],
|
||||
search_case_insensitive(query, contents)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,15 +7,12 @@ fn main() {
|
|||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
let config = Config::build(&args).unwrap_or_else(|err| {
|
||||
println!("Problem parsing arguments: {err}");
|
||||
mprintln!("Problem parsing arguments: {err}");
|
||||
process::exit(1);
|
||||
});
|
||||
|
||||
println!("Searching for {}", config.query);
|
||||
println!("In file {}", config.file_path);
|
||||
|
||||
if let Err(e) = minigrep::run(config) {
|
||||
println!("Application error: {e}");
|
||||
eprintln!("Application error: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue