From ec27c2371d052ada9b1ffd1fc44463f465666bb0 Mon Sep 17 00:00:00 2001 From: rsgltzyd Date: Sat, 23 Nov 2024 00:03:22 +0800 Subject: [PATCH] add minigrep --- minigrep/Cargo.lock | 7 +++ minigrep/Cargo.toml | 6 +++ minigrep/poem.txt | 16 +++++++ minigrep/src/lib.rs | 103 +++++++++++++++++++++++++++++++++++++++++++ minigrep/src/main.rs | 20 +++++++++ 5 files changed, 152 insertions(+) create mode 100644 minigrep/Cargo.lock create mode 100644 minigrep/Cargo.toml create mode 100644 minigrep/poem.txt create mode 100644 minigrep/src/lib.rs create mode 100644 minigrep/src/main.rs diff --git a/minigrep/Cargo.lock b/minigrep/Cargo.lock new file mode 100644 index 0000000..1ec6ded --- /dev/null +++ b/minigrep/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/minigrep/Cargo.toml b/minigrep/Cargo.toml new file mode 100644 index 0000000..64c2a3f --- /dev/null +++ b/minigrep/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "minigrep" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/minigrep/poem.txt b/minigrep/poem.txt new file mode 100644 index 0000000..3104b9a --- /dev/null +++ b/minigrep/poem.txt @@ -0,0 +1,16 @@ +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! +传遍整个无聊的沼泽! diff --git a/minigrep/src/lib.rs b/minigrep/src/lib.rs new file mode 100644 index 0000000..a5cbc19 --- /dev/null +++ b/minigrep/src/lib.rs @@ -0,0 +1,103 @@ +use std::error::Error; +use std::{env, fs}; + +pub struct Config { + pub query: String, + pub file_path: String, + pub ignore_case: bool, +} + +impl Config { + pub fn new(args: &[String]) -> Result { + if args.len() < 3 { + return Err("not enough arguments"); + } + let query = args[1].clone(); + let file_path = args[2].clone(); + + let ignore_case = env::var("IGNORE_CASE").is_ok(); + + Ok(Config { + query, + file_path, + ignore_case, + }) + } +} + +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(config.file_path)?; + let results = if config.ignore_case { + search_case_insensitive(&config.query, &contents) + } else { + search(&config.query, &contents) + }; + + for line in results { + println!("{line}"); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn one_result() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)); + } + + #[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) + ); + } +} + +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 +} + +pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let query = query.to_lowercase(); + let mut result = Vec::new(); + + for line in contents.lines() { + if line.to_lowercase().contains(&query) { + result.push(line); + } + } + + result +} diff --git a/minigrep/src/main.rs b/minigrep/src/main.rs new file mode 100644 index 0000000..433af04 --- /dev/null +++ b/minigrep/src/main.rs @@ -0,0 +1,20 @@ +use std::env; +use std::process; + +use minigrep::Config; + +fn main() { + let args: Vec = env::args().collect(); + let config = Config::new(&args).unwrap_or_else(|err| { + println!("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); + process::exit(1); + } +}