This Rust project demonstrates file reading and writing operations with custom error handling using the thiserror crate.
- Read content from a specified file
- Then Writes its content to a new file
- Custom error handling for various file operations
- Input validation for file paths
color-eyre is a Rust library that provides an enhanced error reporting and handling system. It's designed to make error messages more informative and visually appealing, especially in terminal environments.
Here's what color-eyre does:
-
Colorized Error Messages: It adds color to error messages, making them easier to read and distinguish in the terminal output.
-
Error Tracing: It provides detailed error traces, showing the chain of errors that led to the final error.
-
Source Code Snippets: For errors that occur in your code, it can display relevant source code snippets, helping you quickly locate the issue.
-
Integration with eyre: It's built on top of the eyre crate, which is an error handling library that provides a Report type (similar to anyhow::Error).
-
Custom Sections: Allows you to add custom sections to error reports, providing more context about the error.
-
Panic Handling: It can be set up to handle panics, providing colorized and informative panic messages.
- Rust (edition 2021 or later)
- Cargo (Rust's package manager)
- Clone the repository:
git clone https://github.com/glennin-codes/simple-rust-projects.gitcd simple-rust-projects- Build the project:
cargo buildRun the program with the following command:
cargo run -- <input_file> <output_file>- Replace
<input_file>with the path to the file you want to read, and<output_file>with the path where you want to write the content.
Example:
cargo run -- home/usr/Documents/input.txt output.txtcargo run .- defaults to the
file.txtin the current directory as the input file and will write tonew.txtfile as the output file.
This project uses custom error types defined with thiserror. The main error types include:
FileError::Io: Wraps standard I/O errorsFileError::InvalidInput: Used for invalid file paths or namesFileError::EmptyFile: Indicates when the input file is empty
main.rs: Entry point of the applicationcustom_errors.rs: Defines custom error types usingthiserrorcontrolers: Contains functions for reading and writing files -file_reader.rs-write_file.rs
Contributions are welcome! Please feel free to submit a Pull Request.