diff --git a/Learning/minigrep/Cargo.toml b/Learning/minigrep/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..0e8112816202b9a9d0cb1fd912f32e7f2efe3e52 --- /dev/null +++ b/Learning/minigrep/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "minigrep" +version = "0.1.0" +authors = ["Mindaugas Sharskus "] +edition = "2018" + +[dependencies] diff --git a/Learning/minigrep/poem.txt b/Learning/minigrep/poem.txt new file mode 100644 index 0000000000000000000000000000000000000000..87075273137fdea2814df14af10a6e6138de5986 --- /dev/null +++ b/Learning/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/Learning/minigrep/src/lib.rs b/Learning/minigrep/src/lib.rs new file mode 100644 index 0000000000000000000000000000000000000000..3f57dee4740ffacc1bc8b3ce7057540e1eaecffb --- /dev/null +++ b/Learning/minigrep/src/lib.rs @@ -0,0 +1,119 @@ +//! +//! Mini grep from tutorial +//! https://doc.rust-lang.org/stable/book/ch12-01-accepting-command-line-arguments.html +//! + +use std::fs; +use std::env; +use std::error::Error; + + +#[derive(Debug)] +pub struct Config { + query: String, + filename: String, + case_sensitive: bool, +} + +impl Config { + /// + /// Create new configuration + /// + pub fn new(args: &[String]) -> Result { + if args.len() < 2 { + return Err("not enough arguments"); // return error if not enough arguments + } + + Ok(Self { + query: args[0].clone(), + filename: args[1].clone(), + case_sensitive: env::var("CASE_INSENSITIVE").is_err(), // check id environment variable exist + }) + } +} + +/// +/// Runs our mini-grep app +/// +pub fn run(config: Config) -> Result<(), Box> { + let contents = fs::read_to_string(&config.filename)?; // panics if can't open file + + let results = if config.case_sensitive { + search_sensitive(&config.query, &contents) + } + else { + search_case_insensitive(&config.query, &contents) + }; + + for line in results { + println!("{}", line); + } + + Ok(()) +} + +/// +/// Case sensitive search. +/// +fn search_sensitive<'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 +} + +/// +/// Case insensitive search +/// +fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { + let query = query.to_lowercase(); + let mut results = Vec::new(); + + for line in contents.lines() { + if line.to_lowercase().contains(&query) { + results.push(line); + } + } + + results +} + + +//! +//! Tests +//! +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn case_sensitive() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick three."; + + assert_eq!(vec!["safe, fast, productive."], search_sensitive(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/Learning/minigrep/src/main.rs b/Learning/minigrep/src/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..90e06e2ffe91b555069facf7792c7df49c4fd4cf --- /dev/null +++ b/Learning/minigrep/src/main.rs @@ -0,0 +1,27 @@ +//! +//! Mini grep from tutorial +//! https://doc.rust-lang.org/stable/book/ch12-01-accepting-command-line-arguments.html +//! +//! to run it: +//! `cargo run ` +//! `CASE_INSENSITIVE cargo run ` +//! + +use std::{env, process}; + +use minigrep; +use minigrep::Config; + +fn main() { + let args: Vec = env::args().skip(1).collect(); + + let config = Config::new(&args).unwrap_or_else(|err|{ + eprintln!("Problem parsing arguments: {}", err); // print to stderr + process::exit(1); + }); + + if let Err(e) = minigrep::run(config){ + eprintln!("Application error: {}", e); // print to stderr + process::exit(1); + } +}