rwxaegrep 0.1.0

A simple grep implemetantation from Rust Book
Documentation
//! # rwxaegrep
//! `rwxaegrep` is a collection of utilities to perform search operations
//! on strings.

/// Search for lines that match the specified pattern
/// # Examples
/// 
/// ```
/// let pattern = "foobar";
/// let contents = "this text\ncontains foobar";
/// let actual: Vec<&str> = rwxaegrep::search(pattern, contents).collect();
/// 
/// assert_eq!(actual, vec!["contains foobar"]);
/// ```
pub fn search<'a>(pattern: &'a str, contents: &'a str) -> impl Iterator<Item = &'a str> {
    contents
        .lines()
        .filter(move |line| line.contains(pattern))
}

/// Search for lines that match the specified pattern (case insensitive)
/// 
pub fn search_case_insensitive<'a>(pattern: &str, contents: &'a str) -> impl Iterator<Item = &'a str> + use<'a> {
    let pattern = pattern.to_lowercase();
    contents
        .lines()
        .filter(move |line| line.to_lowercase().contains(&pattern))
}

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

    #[test]
    fn borrow() {
        let contents = "testtesttest";
        let iter;
        {
            let pattern = String::from("test");
            iter = search_case_insensitive(&pattern, contents);
            for i in iter {
                println!("{i}");
            }
        }
    }

    #[test]
    fn case_sensitive() {
        let pattern = "duct";
        let contents = "\
Rust:
safe, fast, productive.
Pick three.
Duct tape.";
        let expected = vec!["safe, fast, productive."];
        let result: Vec<&str> = search(pattern, contents).collect();
        assert_eq!(expected, result);
    }

    #[test]
    fn case_insensitive() {
        let pattern = "rUsT";
        let contents = "\
Rust:
safe, fast, productive.
Pick three.
Trust me.";
        let expected = vec!["Rust:", "Trust me."];
        let result: Vec<&str> = search_case_insensitive(pattern, contents).collect();
        assert_eq!(expected, result);
    }
}