24 releases (5 breaking)

✓ Uses Rust 2018 edition

0.6.2 Jan 17, 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 3508/week @ 2019-11-01 4264/week @ 2019-11-08 6525/week @ 2019-11-15 7054/week @ 2019-11-22 6570/week @ 2019-11-29 8149/week @ 2019-12-06 10149/week @ 2019-12-13 4023/week @ 2019-12-20 3881/week @ 2019-12-27 9102/week @ 2020-01-03 8361/week @ 2020-01-10 11280/week @ 2020-01-17 7571/week @ 2020-01-24 8189/week @ 2020-01-31 5151/week @ 2020-02-07

26,237 downloads per month
Used in 120 crates (104 directly)

MIT/Apache

56KB
993 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.9MB
~20K SLoC