37 releases

0.7.1 May 3, 2022
0.7.0 Jan 4, 2022
0.7.0-beta.2 Nov 15, 2021
0.7.0-beta.0 Jul 5, 2021
0.2.0 Mar 26, 2019

#20 in Rust patterns

Download history 46109/week @ 2022-04-18 45880/week @ 2022-04-25 47119/week @ 2022-05-02 46576/week @ 2022-05-09 44226/week @ 2022-05-16 44370/week @ 2022-05-23 45678/week @ 2022-05-30 54796/week @ 2022-06-06 50542/week @ 2022-06-13 54003/week @ 2022-06-20 50534/week @ 2022-06-27 53353/week @ 2022-07-04 56978/week @ 2022-07-11 59365/week @ 2022-07-18 58753/week @ 2022-07-25 59800/week @ 2022-08-01

236,880 downloads per month
Used in 473 crates (269 directly)

MIT/Apache

93KB
1.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::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.2–1.2MB
~27K SLoC