47 releases

0.8.5 Sep 23, 2024
0.8.4 Jul 8, 2024
0.8.3 May 24, 2024
0.8.2 Mar 11, 2024
0.2.0 Mar 26, 2019

#33 in Rust patterns

Download history 394793/week @ 2024-07-19 387482/week @ 2024-07-26 399092/week @ 2024-08-02 430756/week @ 2024-08-09 427235/week @ 2024-08-16 423146/week @ 2024-08-23 389321/week @ 2024-08-30 425446/week @ 2024-09-06 385757/week @ 2024-09-13 452884/week @ 2024-09-20 451011/week @ 2024-09-27 466045/week @ 2024-10-04 434949/week @ 2024-10-11 453149/week @ 2024-10-18 473509/week @ 2024-10-25 443338/week @ 2024-11-01

1,891,771 downloads per month
Used in 1,329 crates (490 directly)

MIT/Apache

120KB
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–5.5MB
~30K SLoC