From 7d2315501f7f34a39d1760d8152818fe862a377e Mon Sep 17 00:00:00 2001 From: Mindaugas Sharskus Date: Mon, 18 Mar 2019 23:42:53 +0000 Subject: [PATCH 1/2] First two pages done --- Learning/minigrep/Cargo.toml | 7 +++++++ Learning/minigrep/poem.txt | 9 +++++++++ Learning/minigrep/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ Learning/minigrep/src/main.rs | 28 ++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 Learning/minigrep/Cargo.toml create mode 100644 Learning/minigrep/poem.txt create mode 100644 Learning/minigrep/src/lib.rs create mode 100644 Learning/minigrep/src/main.rs diff --git a/Learning/minigrep/Cargo.toml b/Learning/minigrep/Cargo.toml new file mode 100644 index 0000000..0e81128 --- /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 0000000..8707527 --- /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 0000000..65aaf95 --- /dev/null +++ b/Learning/minigrep/src/lib.rs @@ -0,0 +1,34 @@ +/// +/// Mini grep from tutorial +/// https://doc.rust-lang.org/stable/book/ch12-01-accepting-command-line-arguments.html +/// + +use std::fs; +use std::error::Error; + +#[derive(Debug)] +pub struct Config { + query: String, + filename: String, +} + +impl Config { + pub fn new(args: &[String]) -> Result { + if args.len() < 2 { + return Err("not enough arguments"); + } + + Ok(Self { + query: args[0].clone(), + filename: args[1].clone(), + }) + } +} + +pub fn run(config: Config) -> Result<(), Box>{ + let contents = fs::read_to_string(&config.filename)?; + println!("Arguments: {:?}", config); + println!("With text:\n{}", contents); + + Ok(()) +} \ No newline at end of file diff --git a/Learning/minigrep/src/main.rs b/Learning/minigrep/src/main.rs new file mode 100644 index 0000000..c0c647d --- /dev/null +++ b/Learning/minigrep/src/main.rs @@ -0,0 +1,28 @@ +/// +/// Mini grep from tutorial +/// https://doc.rust-lang.org/stable/book/ch12-01-accepting-command-line-arguments.html +/// + +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|{ + println!("Problem parsing arguments: {}", err); + process::exit(1); + }); + + if let Err(e) = minigrep::run(config){ + println!("Application error: {}", e); + process::exit(1); + } +} + + + + -- GitLab From 8cc7155fd055e797c9ad9fe4c5e7dbb0035426f7 Mon Sep 17 00:00:00 2001 From: Mindaugas Sharskus Date: Thu, 21 Mar 2019 23:50:13 +0000 Subject: [PATCH 2/2] Finished CLI tutorial --- Learning/minigrep/src/lib.rs | 105 ++++++++++++++++++++++++++++++---- Learning/minigrep/src/main.rs | 21 ++++--- 2 files changed, 105 insertions(+), 21 deletions(-) diff --git a/Learning/minigrep/src/lib.rs b/Learning/minigrep/src/lib.rs index 65aaf95..3f57dee 100644 --- a/Learning/minigrep/src/lib.rs +++ b/Learning/minigrep/src/lib.rs @@ -1,34 +1,119 @@ -/// -/// Mini grep from tutorial -/// https://doc.rust-lang.org/stable/book/ch12-01-accepting-command-line-arguments.html -/// +//! +//! 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 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 }) } } -pub fn run(config: Config) -> Result<(), Box>{ - let contents = fs::read_to_string(&config.filename)?; - println!("Arguments: {:?}", config); - println!("With text:\n{}", contents); +/// +/// 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(()) -} \ No newline at end of file +} + +/// +/// 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 index c0c647d..90e06e2 100644 --- a/Learning/minigrep/src/main.rs +++ b/Learning/minigrep/src/main.rs @@ -1,28 +1,27 @@ -/// -/// Mini grep from tutorial -/// https://doc.rust-lang.org/stable/book/ch12-01-accepting-command-line-arguments.html -/// +//! +//! 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|{ - println!("Problem parsing arguments: {}", err); + eprintln!("Problem parsing arguments: {}", err); // print to stderr process::exit(1); }); if let Err(e) = minigrep::run(config){ - println!("Application error: {}", e); + eprintln!("Application error: {}", e); // print to stderr process::exit(1); } } - - - - -- GitLab