https://github.com/pro-2684/disperror
`Display`ing errors instead of `Debug`ging them when returned from `main`.
https://github.com/pro-2684/disperror
debug display error main rust
Last synced: 4 months ago
JSON representation
`Display`ing errors instead of `Debug`ging them when returned from `main`.
- Host: GitHub
- URL: https://github.com/pro-2684/disperror
- Owner: PRO-2684
- License: mit
- Created: 2025-01-21T11:19:18.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-10T02:52:50.000Z (over 1 year ago)
- Last Synced: 2025-11-13T14:16:02.887Z (7 months ago)
- Topics: debug, display, error, main, rust
- Language: Rust
- Homepage: https://docs.rs/disperror/
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# disperror
`Display`ing errors instead of `Debug`ging them when returned from `main`.
## Usage
Simply wrap your error type `MyError` in a `DispError`:
```diff
- fn main() -> Result<(), MyError> {
+ use disperror::DispError;
+ fn main() -> Result<(), DispError> {
```
Note that `MyError` must implement `std::error::Error`.
## Example
```rust should_panic
use disperror::DispError;
fn main() -> Result<(), DispError> {
let contents = std::fs::read_to_string("nonexistent_file.txt")?;
println!("{}", contents);
Ok(())
}
```
Should `Display` the following error message if that file does not exist:
```text
Error: No such file or directory (os error 2)
```
Instead of the usual `Debug` output:
```text
Error: Os { code: 2, kind: NotFound, message: "No such file or directory" }
```
## Implementation
The `DispError` type is a simple wrapper around an error type `E` that implements `std::error::Error`:
```rust
use std::error::Error;
pub struct DispError(E);
```
The `Debug` implementation of `DispError` forwards to the `Display` implementation of the inner error:
```rust
use std::{error::Error, fmt::Debug};
#
# pub struct DispError(E);
impl Debug for DispError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
```
In addition, `DispError` implements `From` for implicit conversion:
```rust
# use std::{error::Error, fmt::Debug};
#
# pub struct DispError(E);
#
impl From for DispError {
fn from(error: E) -> Self {
Self(error)
}
}
```
In this way, when an error of type `E` is returned from `main`, it is automatically converted to a `DispError`. When the `Err` variant of a `Result` is returned from `main`, the `Debug` implementation is used to print the error message, thus forwarding to the `Display` implementation of the inner error.
## Notes
This project is heavily inspired by [`main_error`](https://docs.rs/main_error). If you're working with `Box` in your `main` function, use [`main_error`](https://docs.rs/main_error) instead. Here's a quick comparison:
| | `disperror` | `main_error` |
| ------------------ | -------------- | ------------ |
| Library size | Tiny | Small |
| Overhead | None | Negligible |
| Dynamic dispatch | ✘ | ✔ |
| Usage | `DispError` | `MainError` |