2 releases

0.1.1 May 18, 2024
0.1.0 Apr 12, 2024

#9 in #displaying

Download history 41/week @ 2024-07-19 79/week @ 2024-07-26 27/week @ 2024-08-02 36/week @ 2024-08-09 38/week @ 2024-08-16 44/week @ 2024-08-23 49/week @ 2024-08-30 51/week @ 2024-09-06 33/week @ 2024-09-13 64/week @ 2024-09-20 41/week @ 2024-09-27 48/week @ 2024-10-04 29/week @ 2024-10-11 35/week @ 2024-10-18 37/week @ 2024-10-25 33/week @ 2024-11-01

136 downloads per month

MIT license

5KB
56 lines

ERDP

Crates.io Version

ERDP is a very small Rust crate to help with error displaying. If you use std::fmt::Display to display a std::error::Error like the following code:

use std::fs::File;
use std::path::{Path, PathBuf};
use thiserror::Error;

fn main() {
    let path = PathBuf::from("config.json");

    if let Err(e) = load_config(&path) {
        eprintln!("Failed to load {}: {}.", path.display(), e);
    }
}

fn load_config(path: &Path) -> Result<(), MyError> {
    let file = match File::open(path) {
        Ok(v) => v,
        Err(e) => return Err(MyError::OpenFileFailed(e)),
    };

    Ok(())
}

#[derive(Debug, Error)]
enum MyError {
    #[error("couldn't open the specified file")]
    OpenFileFailed(#[source] std::io::Error),
}

What you get is just a message from a top-level error:

Failed to load config.json: couldn't open the specified file.

With this crate you can use display method on the error value like:

use erdp::ErrorDisplay;

eprintln!("Failed to load {}: {}.", path.display(), e.display());

Then the output will change to something like:

Failed to load config.json: couldn't open the specified file -> No such file or directory.

License

MIT

No runtime deps