summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-12-command-line-project/minigrep
diff options
context:
space:
mode:
Diffstat (limited to 'rust/theBook/chapter-12-command-line-project/minigrep')
-rw-r--r--rust/theBook/chapter-12-command-line-project/minigrep/Cargo.lock7
-rw-r--r--rust/theBook/chapter-12-command-line-project/minigrep/poem.txt9
-rw-r--r--rust/theBook/chapter-12-command-line-project/minigrep/src/lib.rs100
-rw-r--r--rust/theBook/chapter-12-command-line-project/minigrep/src/main.rs19
4 files changed, 133 insertions, 2 deletions
diff --git a/rust/theBook/chapter-12-command-line-project/minigrep/Cargo.lock b/rust/theBook/chapter-12-command-line-project/minigrep/Cargo.lock
new file mode 100644
index 0000000..1ec6ded
--- /dev/null
+++ b/rust/theBook/chapter-12-command-line-project/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/rust/theBook/chapter-12-command-line-project/minigrep/poem.txt b/rust/theBook/chapter-12-command-line-project/minigrep/poem.txt
new file mode 100644
index 0000000..8707527
--- /dev/null
+++ b/rust/theBook/chapter-12-command-line-project/minigrep/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!
diff --git a/rust/theBook/chapter-12-command-line-project/minigrep/src/lib.rs b/rust/theBook/chapter-12-command-line-project/minigrep/src/lib.rs
new file mode 100644
index 0000000..60057db
--- /dev/null
+++ b/rust/theBook/chapter-12-command-line-project/minigrep/src/lib.rs
@@ -0,0 +1,100 @@
+use std::{error::Error, fs, env};
+
+pub struct Config {
+ pub query: String,
+ pub file_path: String,
+ pub ignore_case: bool,
+ pub case_option: bool,
+}
+
+impl Config {
+ pub fn build(args: &[String]) -> Result<Config, &'static str> {
+ if args.len() < 4 {
+ return Err("Not enough arguments");
+ }
+
+ let query = args[1].clone();
+ let file_path = args[2].clone(); // String
+ let ignore_case = env::var("IGNORE_CASE").is_ok();
+ let case_option = match args[3].as_str() {
+ "true" => true,
+ &_ => false
+ };
+
+ Ok(Config {query, file_path, ignore_case, case_option})
+ }
+}
+
+// Box<dyn Error> means the function will return a type that implements the Error trait
+// But we don’t have to specify what particular type the return value will be
+// This gives us flexibility to return error values that may be of different types in different error cases
+pub fn run(config: Config) -> Result<(), Box<dyn Error>>{ // dyn dynamic
+ let contents = fs::read_to_string(config.file_path)?;
+
+ let result = if config.ignore_case {
+ search_case_insensitive(&config.query, &contents)
+ } else if config.case_option {
+ search_case_insensitive(&config.query, &contents)
+ } else {
+ search(&config.query, &contents)
+ };
+
+ for line in result {
+ println!("{line}");
+ }
+
+ Ok(())
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn case_sensitive() {
+ let query = "duct";
+ let content = "\
+Rust:
+safe, fast, productive.
+Pick three.
+Duck tape";
+
+ assert_eq!(vec!["safe, fast, productive."], search(query,content));
+ }
+
+ #[test]
+ fn case_insensitive() {
+ let query = "rUsT";
+ let content= "\
+Rust:
+safe, fast, productive.
+Pick three.
+Trust me.";
+
+ assert_eq!(vec!["Rust:", "Trust me."], search_case_insensitive(query, content));
+ }
+}
+
+pub fn search<'a>(query: &str, content: &'a str) -> Vec<&'a str> {
+ let mut result = Vec::new();
+
+ for line in content.lines() {
+ if line.contains(query) {
+ result.push(line);
+ }
+ }
+ result
+}
+
+pub fn search_case_insensitive<'a>(query: &str, content: &'a str) -> Vec<&'a str> {
+ let mut result = Vec::new();
+
+ let query = query.to_lowercase();
+
+ for line in content.lines() {
+ if line.to_lowercase().contains(&query) {
+ result.push(line);
+ }
+ }
+ result
+}
diff --git a/rust/theBook/chapter-12-command-line-project/minigrep/src/main.rs b/rust/theBook/chapter-12-command-line-project/minigrep/src/main.rs
index ae7def5..a166fa2 100644
--- a/rust/theBook/chapter-12-command-line-project/minigrep/src/main.rs
+++ b/rust/theBook/chapter-12-command-line-project/minigrep/src/main.rs
@@ -1,6 +1,21 @@
-use std::env;
+use std::{env, process};
+
+use minigrep::Config;
fn main() {
+ // args() return iterator
+ // collect() extract iterator to collection we defined for args
let args: Vec<String> = env::args().collect();
- dbg!(args);
+
+ let config = Config::build(&args).unwrap_or_else(|err| {
+ eprintln!("Problem parsing arguments: {err}");
+ process::exit(1);
+ });
+
+ // we only care about error code since ok will return ()
+ // so `if let` is a ideal solution
+ if let Err(e) = minigrep::run(config) {
+ eprintln!("Application error: {e}");
+ process::exit(1);
+ }
}