grps 0.1.0

A CLI tool for searching files for patterns
Documentation
use std::io::Write;
use std::fmt::Debug;
use anyhow::{anyhow, Context, Result, Error};

pub fn find_matches(content: &str, pattern: &str, mut writer: impl Write + Debug) -> Result<(), Error> {
    // Check if pattern is empty
    if pattern.is_empty() {
        return Err(anyhow!("Empty pattern is not allowed"));
    }

    let mut matches_found = false;

    for line in content.lines() {
        if line.contains(pattern) {
            writeln!(writer, "{}", line)
                .with_context(|| format!("Could not write line to {:?}", writer))?;
            matches_found = true;
        }
    }

    if matches_found {
        Ok(())
    } else {
        Err(anyhow!("No matches found for pattern: {}", pattern))
    }
}