complated all feature mini-grep
This commit is contained in:
commit
2f6814f359
|
|
@ -0,0 +1 @@
|
||||||
|
/target
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "minigrep"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "minigrep"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
I'm nobody! Who are you?
|
||||||
|
Are you nobody, too?
|
||||||
|
Then there's a pair of us - don't tell!
|
||||||
|
They'd banish us, you know.
|
||||||
|
|
||||||
|
How dreary to be somebody!
|
||||||
|
How public, like a frog
|
||||||
|
To tell your name the livelong day
|
||||||
|
To an admiring bog!
|
||||||
|
|
@ -0,0 +1,94 @@
|
||||||
|
use std::error::Error;
|
||||||
|
use std::{env, fs};
|
||||||
|
pub struct Config {
|
||||||
|
query: String,
|
||||||
|
file_path: String,
|
||||||
|
ignore_case: bool,
|
||||||
|
}
|
||||||
|
impl Config {
|
||||||
|
// Create a new Result<Config>,
|
||||||
|
// parse config from args 's iter.
|
||||||
|
pub fn build(mut args: impl Iterator<Item = String>) -> Result<Config, &'static str> {
|
||||||
|
args.next();
|
||||||
|
let query = match args.next() {
|
||||||
|
Some(args) => args,
|
||||||
|
None => return Err("Please provide a query string"),
|
||||||
|
};
|
||||||
|
let file_path = match args.next() {
|
||||||
|
Some(args) => args,
|
||||||
|
None => return Err("Please provide a file path")
|
||||||
|
};
|
||||||
|
let ignore_case = env::var("IGNORE_CASE").is_ok();
|
||||||
|
Ok(Config {
|
||||||
|
query,
|
||||||
|
file_path,
|
||||||
|
ignore_case,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
||||||
|
// println!("Query: {}", config.query);
|
||||||
|
// println!("File path: {}", config.file_path);
|
||||||
|
// Read the file,if it not exists, return an error(Box<dyn Error>)
|
||||||
|
let contents = fs::read_to_string(config.file_path)?;
|
||||||
|
let result = if config.ignore_case {
|
||||||
|
search_case_insensitive(&config.query, &contents)
|
||||||
|
} else {
|
||||||
|
search(&config.query, &contents)
|
||||||
|
};
|
||||||
|
for line in result {
|
||||||
|
println!("{line}")
|
||||||
|
}
|
||||||
|
// print!("File contents: \n{}", contents);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
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
|
||||||
|
contents.lines()
|
||||||
|
.filter(|line| line.contains(query))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
||||||
|
|
||||||
|
let query = query.to_lowercase();
|
||||||
|
contents.lines()
|
||||||
|
.filter(|line| line.to_lowercase().contains(&query))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn case_sensitive() {
|
||||||
|
let query = "duct";
|
||||||
|
let contents = "\
|
||||||
|
Rust:
|
||||||
|
safe, fast, productive.
|
||||||
|
Pick three.
|
||||||
|
Duct tape.";
|
||||||
|
|
||||||
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
use std::env as ENV;
|
||||||
|
use std::process;
|
||||||
|
fn main() {
|
||||||
|
println!("-------------Grep Cli Tool-------------");
|
||||||
|
println!("Usage : grep <query> <file_path>");
|
||||||
|
println!("------------------End------------------");
|
||||||
|
// let config = minigrep::Config::new(ENV::args());
|
||||||
|
let config = minigrep::Config::build(ENV::args()).unwrap_or_else(|err| {
|
||||||
|
eprintln!("Problem parsing arguments: {err}");
|
||||||
|
process::exit(1);
|
||||||
|
});
|
||||||
|
if let Err(e) = minigrep::run(config) {
|
||||||
|
println!("Application error: {}", e);
|
||||||
|
process::exit(1);
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue