From 581fcdefeeb87a8564c31c302c8b87f53c528e8d Mon Sep 17 00:00:00 2001 From: Piyush Mishra Date: Sat, 25 Apr 2020 13:11:59 +0530 Subject: [PATCH] initials --- .gitignore | 1 + Cargo.lock | 5 +++ Cargo.toml | 9 ++++ file_to_match.txt | 4 ++ src/lib.rs | 108 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 16 +++++++ 6 files changed, 143 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 file_to_match.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..7432b12 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "minigrep" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f78dfe6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "minigrep" +version = "0.1.0" +authors = ["piyush"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/file_to_match.txt b/file_to_match.txt new file mode 100644 index 0000000..78b11ad --- /dev/null +++ b/file_to_match.txt @@ -0,0 +1,4 @@ +Jai Sri Ram +Jai Hanuman +Ram ka Mandir ban raha hai +Hanuman Ram Bhakt hai diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..93c06ec --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,108 @@ +use std::error::Error; +use std::fs; + +pub struct Configs { + pattern: String, + filename: String, + is_case_sensitive: bool, +} + +impl Configs { + pub fn new(args: &Vec) -> Result { + if args.len() < 3 { + return Err("\ +minigrep [pattern] [filename] case +adding \"case\" is optional to enable case sensitive grep"); + } + + let mut is_sensitive = false; + + if let Some(o) = args.get(3) { + if o == "case" { + is_sensitive = true; + } + } + + Ok(Configs { + pattern: args[1].clone(), + filename: args[2].clone(), + is_case_sensitive: is_sensitive, + }) + } +} + +pub fn run(conf: Configs) -> Result<(), Box> { + let contents: String = fs::read_to_string(conf.filename)?; + + if conf.is_case_sensitive { + print_output(search_case_sensitive(&conf.pattern, &contents)); + } else { + print_output(search_case_insensitive(&conf.pattern, &contents)); + } + + Ok(()) +} + +pub fn search_case_sensitive<'a>(pattern: &str, contents: &'a str) -> Vec<&'a str> { + let mut vec: Vec<&'a str> = vec![]; + + for line in contents.lines() { + if line.contains(pattern) { + vec.push(line); + } + } + + vec +} + +pub fn search_case_insensitive<'a>(pattern: &str, contents: &'a str) -> Vec<&'a str> { + let mut vec: Vec<&'a str> = vec![]; + let pattern = pattern.to_lowercase(); + + for line in contents.lines() { + if line.to_lowercase().contains(&pattern) { + vec.push(line); + } + } + vec +} + +pub fn print_output(out: Vec<&str>) { + println!("matched: {}", out.len()); + for x in out { + println!("{}", x); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let pattern = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!( + vec!["safe, fast, productive."], + search_case_sensitive(pattern, contents) + ); + } + + #[test] + fn case_insensitive() { + let pattern = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!( + vec!["safe, fast, productive."], + search_case_insensitive(pattern, contents) + ); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..ef1fe06 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,16 @@ +use std::env; +use std::process; + +fn main() { + let args: Vec = env::args().collect(); + + let conf = minigrep::Configs::new(&args).unwrap_or_else(|err| { + eprintln!("{}", err); + process::exit(1); + }); + + if let Err(e) = minigrep::run(conf) { + eprintln!("Error: {}", e); + process::exit(0); + } +}