45 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

#6 in #snafu

Download history 180264/week @ 2024-01-03 189478/week @ 2024-01-10 210437/week @ 2024-01-17 198961/week @ 2024-01-24 216065/week @ 2024-01-31 207440/week @ 2024-02-07 211228/week @ 2024-02-14 227387/week @ 2024-02-21 239460/week @ 2024-02-28 233442/week @ 2024-03-06 235598/week @ 2024-03-13 231818/week @ 2024-03-20 233546/week @ 2024-03-27 235191/week @ 2024-04-03 240525/week @ 2024-04-10 201714/week @ 2024-04-17

952,251 downloads per month
Used in 1,044 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

~0.4–0.8MB
~19K SLoC