1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
//! # minigrep //! //! `minigrep` is a small CLI implementation of the `grep` command, written in Rust. //! //! This crate follows the tutorial laid out in [Chapter 12 of The Book] //! //! [Chapter 12 of The Book]: https://doc.rust-lang.org/book/ch12-00-an-io-project.html use std::{env, error::Error, fs, result::Result, string::String}; /// A fully valid `minigrep` command. Consists of a `query` to search for, a `filename` to search within, /// and a `case_sensitive` boolean to specify whether the search should ignore case or not pub struct Config { /// the query to search for pub query: String, /// the file to search within pub filename: String, /// whether query should be case sensitive or not pub case_sensitive: bool, } impl Config { /// Attempt to create a new `Config` from given arguments. /// /// # Examples /// /// ``` /// use std::{env, process}; /// use minigrep::Config; /// /// // try to parse CLI arguments into minigrep config; /// let config = Config::new(env::args()).unwrap_or_else(|err| { /// // if fail: convey error and exit /// eprintln!("Problem parsing arguments: {}", err); /// process::exit(1); /// }); /// ``` /// /// # Errors /// /// - return `Err(string)` if a query string is missing from the args /// - return `Err(string)` if a file name is missing from the args pub fn new(mut args: env::Args) -> Result<Config, &'static str> { args.next(); let query = match args.next() { Some(arg) => arg, None => return Err("Didn't get a query string"), }; let filename = match args.next() { Some(arg) => arg, None => return Err("Didn't get a file name"), }; let case_sensitive = env::var("CASE_INSENSITIVE").is_err(); Ok(Config { query, filename, case_sensitive, }) } } /// search through each line in given `contents` and return any lines containing a match to the `query` /// /// # Arguments /// /// - `query`: a query string to search for /// - `contents`: the contents within which we should search for the given query /// /// # Examples /// /// ``` /// use minigrep::search; /// /// let query = "duct"; // expect 'duct' in 'productive' /// /// let contents = "\ /// Rust: /// safe, fast, productive. /// Pick three."; /// /// assert_eq!(vec!["safe, fast, productive."], search(query, contents)); /// ``` pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { contents .lines() .filter(|line| line.contains(query)) .collect() } /// search through each line in given `contents` and return any lines containing /// a case-insensitive match to the `query`. /// /// (i.e., query `"RuSt"` would match line `"rust"`) /// /// # Arguments /// /// - `query`: a query string to search for /// - `contents`: the contents within which we should search for the given query /// /// # Examples /// ``` /// use minigrep::search_case_insensitive; /// /// let query = "DUCT"; // 'DUCT' will match 'productive' b/c case-insensitive /// let contents = "\ /// Rust: /// safe, fast, productive. /// Pick three."; /// /// assert_eq!(vec!["safe, fast, productive."], search_case_insensitive(query, contents)); /// ``` pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { let query = query.to_lowercase(); contents .lines() .filter(|line| line.to_lowercase().contains(&query)) .collect() } /// Given a valid `config` of arguments, run the minigrep application. /// This is the functional entry-point into the application. /// /// # Examples /// /// ``` /// use std::{env, process}; /// use minigrep::Config; /// /// /// run `minigrep` CLI application /// fn main() { /// // try to parse CLI arguments into minigrep config; /// let config = Config::new(env::args()).unwrap_or_else(|err| { /// // if fail: convey error and exit /// eprintln!("Problem parsing arguments: {}", err); /// process::exit(1); /// }); /// /// // try to run minigrep; convey error and exit if fail /// if let Err(e) = minigrep::run(config) { /// eprintln!("Application error: {}", e); /// process::exit(1); /// } /// } /// ``` /// /// # Errors /// /// This function will return an IO error if `config.filename` does not exist. /// Other errors may also be returned according to [`std::fs::read_to_string`] /// /// [`std::fs::read_to_string`]: https://doc.rust-lang.org/std/fs/fn.read_to_string.html pub fn run(config: Config) -> Result<(), Box<dyn Error>> { let contents = fs::read_to_string(&config.filename)?; let results = if config.case_sensitive { search(&config.query, &contents) } else { search_case_insensitive(&config.query, &contents) }; println!( "Searching for '{}' in file '{}'\n", config.query, config.filename ); if results.len() == 0 { println!("No lines matched your query."); } else { for line in results { println!("{}", line); } } Ok(()) } #[cfg(test)] mod tests { use super::*; /// test query with no matching lines #[test] fn case_sensitive_no_result() { let query = "philanthropist"; let contents = "no such thing"; assert_eq!(vec![] as Vec<&str>, search(query, contents)); } /// test query with one matching line #[test] fn case_sensitive_one_result() { let query = "duct"; // expect 'duct' in 'productive' let contents = "Rust:\nsafe, fast, productive.\nPick three."; assert_eq!(vec!["safe, fast, productive."], search(query, contents)); } /// test case-insensitive query with matching result #[test] fn case_insensitive_one_result() { let query = "rUsT"; let contents = "Rust:\nsafe, fast, productive.\nPick three.\nTrust me."; assert_eq!( vec!["Rust:", "Trust me."], search_case_insensitive(query, contents) ); } }