//! # my minigrep
//!
//! `minigrep` is a terminal util to allow you to grep in a file.

pub fn search<'a>(query: &str, contents: &'a str) -> impl Iterator<Item = &'a str> {
    contents.lines().filter(move |line| line.contains(query))
}

/// Returns an iterator over the matches of `query` in `contents`.
///
/// # Examples
///
/// ```
/// let query = "the";
/// let text = "\
/// the dog
/// is over
/// there.";
/// let results: Vec<_> = eytan_minigrep::search_case_insensitive(query, text).collect();
/// assert_eq!(vec!["the dog", "there."], results)
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> impl Iterator<Item = &'a str> {
    contents.lines().filter(move |line| line.to_lowercase().contains(&query.to_lowercase()))
    // let mut results = vec![];
    // let query = query.to_lowercase();
    // for line in contents.lines() {
    //     if line.to_lowercase().contains(&query) {
    //         results.push(line);
    //     }
    // }
    // results
}

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

    #[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).collect::<Vec<&str>>())
    }

    #[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).collect::<Vec<&str>>())
    }
}