<file_sep>use std::fs;
use std::error::Error;
use std::env;

pub struct Config {
    pub query: String,
    pub filename: String,
    pub case_sensitive: bool,
}

impl Config {
    pub fn new(mut args: std::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 filename"),
        };

        let case_sensitive = env::var("CASE_SENSITIVE").is_err();

        Ok(Config{query, filename, case_sensitive})

        // if args.len() < 3 {
        //     return Err("not enough arguments")
        // }

        // // args 1 is search string
        // let query = args[1].clone();

        // //args 2 is filename
        // let filename = args[2].clone();

        // let case_sensitive = env::var("CASE_SENSITIVE").is_err();

        // Ok(Config{query, filename, case_sensitive})
    }
}

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_sensitive(&config.query, &contents)
    };

    for line in results {
        println!("{}", line);
    }

    Ok(())
}

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

    #[test]
    fn true_result() {
        let query = "con duong";
        let content = "\
Hen mot mai:
Co con duong nao buoc qua
Ta den mang em mon qua
        ";

        assert_eq!(
            vec!["Co con duong nao buoc qua"], 
            search(query, content)
        );
    }

    #[test]
    fn error_result() {
        let query = "meo con xinh xinh";
        let content = "\
Hen mot mai:
Co con duong nao buoc qua
Ta den mang em mon qua
        ";

        assert_ne!(
            vec!["meo con xinh xinh"], 
            search(query, content)
        );
    }

    #[test]
    fn sensitive_case() {
        let query = "<NAME>";
        let content = "\
Hen mot mai:
Co con duong nao buoc qua
Ta den mang em mon qua
        ";

        assert_ne!(
            vec!["Hen mot mai:"], 
            search(query, content)
        );
    }

    #[test]
    fn search_case_sensitive_test() {
        let contents = "Lili was a little girl";
        let query = "Was A";

        assert_eq!(
            vec!["Lili was a little girl"],
            search_case_sensitive(query, contents)
        );
    }
}

pub fn search<'a>(query: &str, content: &'a str) -> Vec<&'a str> {
    content.lines().filter(|line| line.contains(query)).collect()

    // let mut results = Vec::new();

    // for line in content.lines() {
    //     if line.contains(query) {
    //         results.push(line);
    //     }
    // }

    // results
}


pub fn search_case_sensitive<'a>(query: &str, content: &'a str) -> Vec<&'a str> {
    let mut results = Vec::new();
    let query = query.to_lowercase();

    for line in content.lines() {
        if line.to_lowercase().contains(&query) {
            results.push(line);
        }
    }

    results
}
<file_sep># Minigrep
Minigrep is a simple project when I begin learning Rust.
