40 releases

0.7.4 Dec 19, 2022
0.7.3 Oct 20, 2022
0.7.1 May 3, 2022
0.7.0 Jan 4, 2022
0.2.0 Mar 26, 2019

#16 in Rust patterns

Download history 71193/week @ 2022-11-30 66654/week @ 2022-12-07 63639/week @ 2022-12-14 49357/week @ 2022-12-21 41998/week @ 2022-12-28 68637/week @ 2023-01-04 67066/week @ 2023-01-11 79007/week @ 2023-01-18 87179/week @ 2023-01-25 81628/week @ 2023-02-01 79380/week @ 2023-02-08 77860/week @ 2023-02-15 78420/week @ 2023-02-22 76943/week @ 2023-03-01 86682/week @ 2023-03-08 78104/week @ 2023-03-15

333,894 downloads per month
Used in 647 crates (322 directly)

MIT/Apache

110KB
2K 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::prelude::*;
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.7–1.7MB
~38K SLoC