From 2f6814f3590f199ce6755dce0d82cb0f48a73da0 Mon Sep 17 00:00:00 2001 From: shenjianZ Date: Sun, 17 Nov 2024 15:59:50 +0800 Subject: [PATCH] complated all feature mini-grep --- .gitignore | 1 + Cargo.lock | 7 ++++ Cargo.toml | 6 ++++ poem.txt | 9 +++++ src/lib.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 16 +++++++++ 6 files changed, 133 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 poem.txt create mode 100644 src/lib.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..1ec6ded --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..64c2a3f --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/poem.txt b/poem.txt new file mode 100644 index 0000000..3a260bf --- /dev/null +++ b/poem.txt @@ -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! \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..7fb3318 --- /dev/null +++ b/src/lib.rs @@ -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, + // parse config from args 's iter. + pub fn build(mut args: impl Iterator) -> Result { + 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> { + // println!("Query: {}", config.query); + // println!("File path: {}", config.file_path); + // Read the file,if it not exists, return an error(Box) + 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) + ); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..ab93d49 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,16 @@ +use std::env as ENV; +use std::process; +fn main() { + println!("-------------Grep Cli Tool-------------"); + println!("Usage : grep "); + 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); + }; +}