33 releases

0.7.0-beta.0 Jul 5, 2021
0.6.10 Dec 3, 2020
0.6.9 Sep 22, 2020
0.6.8 May 12, 2020
0.2.0 Mar 26, 2019

#11 in Rust patterns

Download history 23153/week @ 2021-04-05 22281/week @ 2021-04-12 22037/week @ 2021-04-19 31031/week @ 2021-04-26 23544/week @ 2021-05-03 27969/week @ 2021-05-10 31109/week @ 2021-05-17 33918/week @ 2021-05-24 26207/week @ 2021-05-31 29384/week @ 2021-06-07 32452/week @ 2021-06-14 30196/week @ 2021-06-21 28548/week @ 2021-06-28 29514/week @ 2021-07-05 29654/week @ 2021-07-12 29354/week @ 2021-07-19

118,570 downloads per month
Used in 311 crates (198 directly)

MIT/Apache

175KB
3.5K SLoC

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(ReadConfigurationSnafu { path })?;
    let path = unpack_config(&configuration);
    fs::write(&path, b"My complex calculation").context(WriteResultSnafu { 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.3–1.3MB
~29K SLoC