45 releases

new 0.8.3 May 24, 2024
0.8.2 Mar 11, 2024
0.8.1 Feb 26, 2024
0.8.0 Dec 30, 2023
0.2.0 Mar 26, 2019

#24 in Rust patterns

Download history 213403/week @ 2024-02-06 212199/week @ 2024-02-13 218502/week @ 2024-02-20 236800/week @ 2024-02-27 236197/week @ 2024-03-05 234429/week @ 2024-03-12 236899/week @ 2024-03-19 228674/week @ 2024-03-26 241191/week @ 2024-04-02 231714/week @ 2024-04-09 249665/week @ 2024-04-16 249528/week @ 2024-04-23 230203/week @ 2024-04-30 240295/week @ 2024-05-07 259540/week @ 2024-05-14 239413/week @ 2024-05-21

1,015,609 downloads per month
Used in 1,091 crates (438 directly)

MIT/Apache

115KB
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()))]
    ReadConfiguration { source: io::Error, path: PathBuf },

    #[snafu(display("Unable to write result to {}", path.display()))]
    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.5MB
~32K SLoC