25 releases

✓ Uses Rust 2018 edition

0.6.3 Mar 19, 2020
0.6.0 Nov 7, 2019
0.4.3 Jul 24, 2019
0.2.0 Mar 26, 2019

#8 in Rust patterns

Download history 9275/week @ 2019-12-14 3769/week @ 2019-12-21 4346/week @ 2019-12-28 9555/week @ 2020-01-04 7624/week @ 2020-01-11 11814/week @ 2020-01-18 7685/week @ 2020-01-25 7719/week @ 2020-02-01 5406/week @ 2020-02-08 6553/week @ 2020-02-15 5579/week @ 2020-02-22 6188/week @ 2020-02-29 7504/week @ 2020-03-07 7324/week @ 2020-03-14 7360/week @ 2020-03-21 7966/week @ 2020-03-28

29,848 downloads per month
Used in 137 crates (116 directly)

MIT/Apache

56KB
992 lines

SNAFU

Situation Normal: All Fouled Up

crates.io Documentation Build Status

SNAFU is a library to easily assign underlying errors into domain-specific errors while adding context.

use snafu::{ResultExt, Snafu};
use std::{fs, io, path::PathBuf};

#[derive(Debug, Snafu)]
enum Error {
    #[snafu(display("Unable to read configuration from {}: {}", path.display(), source))]
    ReadConfiguration { source: io::Error, path: PathBuf },

    #[snafu(display("Unable to write result to {}: {}", path.display(), source))]
    WriteResult { source: io::Error, path: PathBuf },
}

type Result<T, E = Error> = std::result::Result<T, E>;

fn process_data() -> Result<()> {
    let path = "config.toml";
    let configuration = fs::read_to_string(path).context(ReadConfiguration { path })?;
    let path = unpack_config(&configuration);
    fs::write(&path, b"My complex calculation").context(WriteResult { path })?;
    Ok(())
}

fn unpack_config(data: &str) -> &str {
    "/some/path/that/does/not/exist"
}

Please see the documentation and the user's guide for a full description.

Dependencies

~0.5–0.8MB
~19K SLoC