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

#21 in Rust patterns

Download history 455362/week @ 2024-10-22 496853/week @ 2024-10-29 594273/week @ 2024-11-05 645620/week @ 2024-11-12 621906/week @ 2024-11-19 391871/week @ 2024-11-26 541991/week @ 2024-12-03 673306/week @ 2024-12-10 488293/week @ 2024-12-17 180180/week @ 2024-12-24 294986/week @ 2024-12-31 588583/week @ 2025-01-07 554540/week @ 2025-01-14 544780/week @ 2025-01-21 608220/week @ 2025-01-28 529974/week @ 2025-02-04

2,350,066 downloads per month
Used in 1,457 crates (520 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–6MB
~29K SLoC