Below is a professional and detailed README.md file for your Rust crate. It includes an overview of the functionality, installation instructions, usage examples, and additional notes.
Search Crate
A flexible and efficient Rust library for searching text within files. Supports multiple search modes (substring and regex), optional case-insensitive searches, and writing results to an output file or printing them to the console.
Features
- Substring Search: Perform simple substring searches in a file.
- Regex Search: Use regular expressions for advanced pattern matching.
- Case-Insensitive Searches: Optionally ignore case during searches.
- Output Flexibility: Write results to an output file or print them to the console.
- Efficient File Handling: Reads large files line by line using buffered I/O for optimal performance.
Installation
Prerequisites
- Rust 1.60 or higher.
- Cargo package manager.
Add as a Dependency
If you want to use this crate in your project, add it to your Cargo.toml:
[]
= { = "path_to_search_crate" }
Alternatively, if the crate is published to crates.io, you can include it as:
[]
= "0.2.0"
Usage
Command-Line Interface
The crate provides a command-line interface for performing searches. Below are examples of how to use it.
Substring Search
Perform a case-sensitive substring search and print results to the console:
Case-Insensitive Regex Search
Perform a case-insensitive regex search and write results to an output file:
Help Message
If insufficient arguments are provided, the program will display a usage message:
Programmatic Usage
You can also use the crate programmatically in your Rust code. Here's an example:
use search_in_file;
Arguments
The program accepts the following arguments:
| Argument | Description |
|---|---|
<mode> |
The search mode (search for substring, regex for regular expressions). |
<search_string> |
The string or regex pattern to search for in the file. |
<file_path> |
The path to the file to search. |
[output_file] |
(Optional) The path to the file where results will be written. |
[--ignore-case] |
(Optional) If provided, the search will ignore case. |
Examples
Example 1: Substring Search
Search for the word "Error" in large_log_file.txt and print results to the console:
Example 2: Case-Insensitive Regex Search
Search for lines matching the regex pattern "error\d+" in large_log_file.txt, ignoring case, and write results to output.txt:
Example 3: Invalid Regex Pattern
If the regex pattern is invalid, the program will notify you:
Error Handling
The program gracefully handles errors such as:
- Missing or invalid arguments.
- File opening or reading errors.
- Invalid regex patterns.
Example error messages:
)
Notes
- Performance: The crate reads files line by line using buffered I/O, making it suitable for large files.
- Dependencies: The crate uses the
regexcrate for regex support. - Extensibility: Additional search modes or features can be added in the future.
Contributing
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature). - Commit your changes (
git commit -am 'Add feature'). - Push to the branch (
git push origin feature/your-feature). - Open a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Authors
- zero
This README.md provides a comprehensive overview of your crate, making it easy for users to understand its functionality and integrate it into their projects. Let me know if you'd like to customize it further!