nfminigrep 0.1.0

Testing cargo publishing and learning Rust with the minigrep tutorial (https://doc.rust-lang.org/book/ch14-02-publishing-to-crates-io.html)
Documentation
use std::fs;
use std::error::Error;
use std::env;
use std::process;


pub struct Config {
    pub query: String,
    pub fname: String,
    pub insensitive: bool,
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(&config.fname)?;
    let results = if config.insensitive {
        search_insensitive(&config.query, &contents)
    } else {
        search(&config.query, &contents)
    };
    for line in results {
        println!("{}", line);
    }
    Ok(())
}


impl Config {
    pub fn new(mut args: std::env::Args) -> Result<Config, &'static str> {

        args.next();

        let query = match args.next() {
            Some(s) => s,
            None => return Err("ERROR: No query argument"),
        };

        let fname = match args.next() {
            Some(s) => s,
            None => return Err("ERROR: No filename argument"),
        };

        let mut insensitive = false;
        while let Some(arg) = args.next() {
            if arg == "-h" {
                printHelp();
                process::exit(0);
            }
            if arg == "-i" {
                insensitive = true;
            }
        }
        
        let c = Config { query, fname, insensitive };
        Ok(c)
    }
}


fn printHelp(){
    println!("
USAGE: minigrep pattern file [options]
pattern         string to search for
file            path to a file to search through

[options]
    -h          displays this help message
    -i          makes search case-insensitive. Default off.
");
}

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

fn search_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
    let found = contents.lines().filter(|x| x.to_lowercase().contains(&query)).collect();
    found
}


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

    #[test]
    fn getone() {
        let q = "duct";
        let c = "\
Rust:
safe, fast, productive.
Pick three.
        ";

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

    #[test]
    fn getNone() {
        let q = "ayyy";
        let c = "\
Rust:
safe, fast, productive.
Pick three.
        ";

        let base: Vec<&str> = Vec::new();

        assert_eq!(
            base,
            search(q,c)
        );
    }

    #[test]
    fn getMultiple() {
        let q = "ayy";
        let c = "\
ayy lmao:
safe, fast, productive.
waayy lmao lol.
        ";

        let base: Vec<&str> = vec!["ayy lmao:", "waayy lmao lol."];
        assert_eq!(
            base,
            search(q,c)
        );
    }

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

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

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

        assert_eq!(vec!["safe, fast, productive.", "Duct tape."], search_insensitive(q, c));

    }
}