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

#18 in Rust patterns

Download history 608220/week @ 2025-01-28 631670/week @ 2025-02-04 714408/week @ 2025-02-11 699214/week @ 2025-02-18 723953/week @ 2025-02-25 1142070/week @ 2025-03-04 1038953/week @ 2025-03-11 1423748/week @ 2025-03-18 1197071/week @ 2025-03-25 894846/week @ 2025-04-01 906913/week @ 2025-04-08 777447/week @ 2025-04-15 723121/week @ 2025-04-22 573345/week @ 2025-04-29 645486/week @ 2025-05-06 521698/week @ 2025-05-13

2,595,364 downloads per month
Used in 1,525 crates (567 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.2–5.5MB
~28K SLoC