grep_clone/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
//! grep_clone is a command line search tool that finds occurrences of a pattern in a file.
//!
//! # Features
//!
//! - Search for text patterns in files
//! - Case-sensitive search
//! - Line-by-line output of matches
//!
//! # Examples
//!
//! Basic usage:
//!
//! ```no_run
//! use grep_clone;
//! use std::io;
//!
//! let pattern = "pattern";
//! let content = "some text\nwith pattern\nmore text";
//! grep_clone::find_matches(content, pattern, io::stdout()).unwrap();
//! ```
//!
//! # Error Handling
//!
//! The library uses `anyhow` for error handling, providing detailed error messages
//! when operations fail, such as:
//! - File not found
//! - Permission denied
//! - Invalid UTF-8 in files
//!
//! # Performance
//!
//! The tool reads files line by line to handle large files efficiently without
//! loading the entire file into memory.
pub fn find_matches(
content: &str,
pattern: &str,
mut writer: impl std::io::Write,
) -> std::io::Result<()> {
for line in content.lines() {
if line.contains(pattern) {
writeln!(writer, "{}", line)?;
}
}
Ok(())
}