use require_lifetimes::require_lifetimes;

/// Returns the line from `book` that contains `query`, if any.
/// (3 marks)
#[require_lifetimes]
pub fn get_context(book: &str, query: &str) -> Option<&str> {
    for line in book.lines() {
        if line.contains(query) {
            return Some(line);
        }
    }
    None
}

/// A search result containing the search term and its surrounding context.
/// (2 marks)
pub struct SearchResult {
    pub query: &str,
    pub context: &str,
}

/// Searches for `query` across all books in the library.
/// Returns a SearchResult for each line containing the query.
/// (5 marks)
#[require_lifetimes]
pub fn search_library(library: &Vec<&str>, query: &str) -> Vec<SearchResult> {
    let mut results = vec![];
    for book in library {
        for line in book.lines() {
            if line.contains(query) {
                results.push(SearchResult {
                    query,
                    context: line,
                });
            }
        }
    }
    results
}
