43 releases

0.8.0 Dec 30, 2023
0.7.5 Jul 9, 2023
0.7.4 Dec 19, 2022
0.7.3 Oct 20, 2022
0.2.0 Mar 26, 2019

#718 in Procedural macros

Download history 152521/week @ 2023-10-30 143718/week @ 2023-11-06 160606/week @ 2023-11-13 137185/week @ 2023-11-20 151046/week @ 2023-11-27 175356/week @ 2023-12-04 167640/week @ 2023-12-11 167295/week @ 2023-12-18 87708/week @ 2023-12-25 151315/week @ 2024-01-01 187042/week @ 2024-01-08 202199/week @ 2024-01-15 207214/week @ 2024-01-22 209295/week @ 2024-01-29 212102/week @ 2024-02-05 213128/week @ 2024-02-12

851,969 downloads per month
Used in 949 crates (2 directly)

MIT/Apache

140KB
3.5K 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

~350–800KB
~19K SLoC