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 83864/week @ 2023-12-23 148516/week @ 2023-12-30 186897/week @ 2024-01-06 198598/week @ 2024-01-13 207684/week @ 2024-01-20 206398/week @ 2024-01-27 210559/week @ 2024-02-03 224035/week @ 2024-02-10 198119/week @ 2024-02-17 237271/week @ 2024-02-24 236067/week @ 2024-03-02 236692/week @ 2024-03-09 235525/week @ 2024-03-16 234845/week @ 2024-03-23 239652/week @ 2024-03-30 192341/week @ 2024-04-06

942,595 downloads per month
Used in 1,041 crates (428 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.4–1.5MB
~32K SLoC