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

#24 in Rust patterns

Download history 213597/week @ 2024-01-19 202303/week @ 2024-01-26 206553/week @ 2024-02-02 226518/week @ 2024-02-09 194467/week @ 2024-02-16 239005/week @ 2024-02-23 237617/week @ 2024-03-01 232629/week @ 2024-03-08 240876/week @ 2024-03-15 230714/week @ 2024-03-22 242213/week @ 2024-03-29 227811/week @ 2024-04-05 246345/week @ 2024-04-12 245820/week @ 2024-04-19 239768/week @ 2024-04-26 233418/week @ 2024-05-03

1,013,157 downloads per month
Used in 1,065 crates (434 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