minigrep_mxcln 0.1.0

A simple command line tool to search for a string in a file.
Documentation
//! # Config Crate
//! 
//! `Config` is a struct that can parse env arguments from consoler.

use std::fs;
use std::error::Error;

#[derive(Default)]
pub struct Config {
    query: String,
    file_path: String,
    ignore_case: bool,
}

impl Config {

    /// Build a config using environment arguments from `std::env::args()`
    /// 
    /// # Example
    /// 
    /// ```
    /// let config = Config::build(std::env::args()).unwrap_or_else(|err| {
    /// eprintln!("Error: {err}\n");
    /// process::exit(1);
    /// });
    /// ```
    pub fn build(mut args: impl Iterator<Item = String>) -> Result<Config, &'static str> {
        args.next();

        let query = match args.next() {
            Some(arg) => arg,
            None => return Err("Didn't get a query string"),
        };
        let file_path = match args.next() {
            Some(arg) => arg,
            None => return Err("Didn't get a file path"),
        };
        let ignore_case = match args.next() {
            Some(arg) => arg.to_string() == "--ignore-case",
            _ => false,
        };

        Ok(Config {
            query,
            file_path,
            ignore_case,
        })
    }

    /// Getter for query.
    pub fn query(&self) -> &str {
        &self.query
    }
    /// Getter for file_path
    pub fn file_path(&self) -> &str {
        &self.file_path
    }
    /// Getter for ignore_case
    pub fn ignore_case(&self) -> bool {
        self.ignore_case
    }
}

/// Run the `minigrep`.
/// 
/// # Example
/// 
/// ```
/// if let Err(e) = minigrep::run(config) {
///     eprintln!("Application error: {e}");
///     process::exit(1);
/// } 
/// ```
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(config.file_path())?;

    println!("Found \"{}\" at:\n", config.query());

    if config.ignore_case() {
        search_case_insensitive(config.query(), &contents)
    } else {
        search(config.query(), &contents)
    }.into_iter().for_each(|x| {println!("{x}");});

    Ok(())
}

fn search<'a>(query: &'a str, contents: &'a str) ->Vec<&'a str> {
    contents.lines().into_iter().filter(|x| x.contains(query)).collect()
}

fn search_case_insensitive<'a>(query: &'a str, contents: &'a str) ->Vec<&'a str> {
    contents.lines().into_iter().filter(|x| x.to_lowercase().contains(&query.to_lowercase())).collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn one_result() {
        let query = "duct";
        let contents = "\
Rust:
safe, fast, productive.
Pick three.";

        assert_eq!(vec!["safe, fast, productive."], search(query, contents));
    }

    #[test]
    fn case_sensitive() {
        let query = "duct";
        let contents = "\
Rust:
safe, fast, productive.
Pick three.
Duct tape.";

        assert_eq!(vec!["safe, fast, productive."], search(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)
        );
    }
}