44 releases

0.8.2 Mar 11, 2024
0.8.0 Dec 30, 2023
0.7.5 Jul 9, 2023
0.7.4 Dec 19, 2022
0.2.0 Mar 26, 2019

#23 in Rust patterns

Download history 150310/week @ 2023-11-27 174665/week @ 2023-12-04 167005/week @ 2023-12-11 166566/week @ 2023-12-18 86688/week @ 2023-12-25 150580/week @ 2024-01-01 186314/week @ 2024-01-08 201465/week @ 2024-01-15 206455/week @ 2024-01-22 208544/week @ 2024-01-29 211410/week @ 2024-02-05 222746/week @ 2024-02-12 200944/week @ 2024-02-19 238469/week @ 2024-02-26 238231/week @ 2024-03-04 102888/week @ 2024-03-11

791,211 downloads per month
Used in 1,002 crates (411 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