errortools
Tired of writing this in every project?
Because returning Result from main uses Debug, which gives you this:
Error: Outer(Inner(Io(Os { code: 2, kind: NotFound, message: "No such file or directory" })))
We have a solution: MainResult.
Example
use MainResult;
use ;
Output:
Error: failed to load config: No such file or directory (os error 2)
The error and its full source chain are joined with ": " — no boilerplate, no run() wrapper, no manual loop.
Tree format
Prefer a multi-line view? Swap the format strategy:
use ;
use ;
Error: failed to load config
└── No such file or directory (os error 2)
Logging in place
Sometimes you cannot return and need to log the full source chain right where
the error happens. The FormatError extension trait works on any error:
use FormatError;
if let Err = do_thing
For ad-hoc strategies, pick the format inline with formatted::<F>():
use ;
if let Err = do_thing
Custom formats
Implement the Format trait on a unit type:
use ;
use ;
use Itertools;
;
println!; // outer -> middle -> inner
How it works
MainResult<E, F> is a type alias:
use ;
pub type MainResult<E, F = OneLine> = ;
DisplaySwapDebug swaps the Debug and Display impls of its inner type, so when main prints the error via Debug, you actually get its Display output — formatted by the chosen strategy. ? converts your error automatically via the blanket From impl.
Examples
Runnable examples in examples/:
| Example | What it shows |
|---|---|
one_line |
MainResult with default OneLine format |
tree |
MainResult<E, Tree> for indented multi-line output |
format_error |
FormatError trait for ad-hoc formatting |
custom_format |
A custom Format strategy |
transparent |
#[error(transparent)] pass-through with #[from] |
Run with: cargo run --example <name>.
Features
| Feature | Default | Effect |
|---|---|---|
std |
yes | Enables itertools/use_std. Disable for no_std. |